From c200644dc06d40249c0db685748bbf0b42c021ef Mon Sep 17 00:00:00 2001 From: Divya Darshana Date: Wed, 8 Jan 2025 12:12:57 +0530 Subject: [PATCH] review suggestions --- pkg/gofr/datasource/mongo/mongo.go | 2 +- pkg/gofr/datasource/mongo/mongo_test.go | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/gofr/datasource/mongo/mongo.go b/pkg/gofr/datasource/mongo/mongo.go index cd6ddda88..bfdf6fba4 100644 --- a/pkg/gofr/datasource/mongo/mongo.go +++ b/pkg/gofr/datasource/mongo/mongo.go @@ -166,7 +166,7 @@ func generateMongoURI(config *Config) (uri, host string, err error) { } func getDBHost(uri string) (host string, err error) { - parsedURL, err := url.Parse(uri) + parsedURL, err := url.ParseRequestURI(uri) if err != nil { return "", err } diff --git a/pkg/gofr/datasource/mongo/mongo_test.go b/pkg/gofr/datasource/mongo/mongo_test.go index f1ebfa268..85195783e 100644 --- a/pkg/gofr/datasource/mongo/mongo_test.go +++ b/pkg/gofr/datasource/mongo/mongo_test.go @@ -48,12 +48,23 @@ func TestGenerateMongoURI(t *testing.T) { name: "Valid Config", config: Config{ User: "admin", - Password: "password", + Password: "p@##word:", Host: "localhost", Port: 27017, Database: "mydb", }, - expectedURI: "mongodb://admin:password@localhost:27017/mydb?authSource=admin", + expectedURI: "mongodb://admin:p%2540%2523%2523word%253A@localhost:27017/mydb?authSource=admin", + expectedHost: "localhost", + expectedError: "", + }, + { + name: "Valid Config without authentication", + config: Config{ + Host: "localhost", + Port: 27017, + Database: "mydb", + }, + expectedURI: "mongodb://localhost:27017/mydb?authSource=admin", expectedHost: "localhost", expectedError: "", }, @@ -150,7 +161,7 @@ func TestGetDBHost(t *testing.T) { name: "Empty URI", uri: "", expected: "", - expectedErr: "incorrect URI for MongoDB", + expectedErr: "parse \"\": empty url", }, { name: "Malformed URI",