Skip to content

Commit

Permalink
creating urlOptions struct to avoid flaky tests with maps
Browse files Browse the repository at this point in the history
  • Loading branch information
mativm02 committed Aug 3, 2023
1 parent f60dda8 commit 94cb84a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
19 changes: 11 additions & 8 deletions persistent/internal/driver/mongo/life_cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ type urlInfo struct {
user string
pass string
db string
options map[string]string
options []urlOptions
}

type urlOptions struct {
key string
val string
}

func isOptSep(c rune) bool {
Expand Down Expand Up @@ -112,14 +117,12 @@ func parseURL(s string) (string, *urlInfo, error) {

connString += strings.Join(info.addrs, ",")

if info.db != "" {
connString += "/" + info.db
}
connString += "/" + info.db

if len(info.options) > 0 {
connString += "?"
for k, v := range info.options {
connString += k + "=" + v + "&"
for _, v := range info.options {
connString += v.key + "=" + v.val + "&"
}

connString = connString[:len(connString)-1]
Expand All @@ -129,7 +132,7 @@ func parseURL(s string) (string, *urlInfo, error) {
}

func extractURL(s string) (*urlInfo, error) {
info := &urlInfo{options: make(map[string]string)}
info := &urlInfo{options: make([]urlOptions, 0)}

if c := strings.Index(s, "?"); c != -1 {
for _, pair := range strings.FieldsFunc(s[c+1:], isOptSep) {
Expand All @@ -138,7 +141,7 @@ func extractURL(s string) (*urlInfo, error) {
return nil, errors.New("connection option must be key=value: " + pair)
}

info.options[l[0]] = l[1]
info.options = append(info.options, urlOptions{key: l[0], val: l[1]})
}

s = s[:c]
Expand Down
14 changes: 8 additions & 6 deletions persistent/internal/driver/mongo/life_cycle_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build mongo
// +build mongo

package mongo

import (
Expand Down Expand Up @@ -147,7 +144,7 @@ func TestConnect(t *testing.T) {
{
name: "valid connection_string",
opts: &types.ClientOpts{
ConnectionString: "mongodb://localhost:27017/test",
ConnectionString: "mongodb+srv://tyk:6}3cZQU.9KvM/hVR4qkm-hHqZTu3yg=G@cluster0.zlgvyel.mongodb.net/?retryWrites=true&w=majority",
UseSSL: false,
Type: "mongodb",
},
Expand Down Expand Up @@ -211,7 +208,7 @@ func TestParseURL(t *testing.T) {
{
name: "valid connection string with @",
url: "mongodb://user:p@ssword@localhost:27017",
want: "mongodb://user:p@ssword@localhost:27017",
want: "mongodb://user:p@ssword@localhost:27017/",
},
{
name: "valid connection string with @ and /",
Expand Down Expand Up @@ -268,13 +265,18 @@ func TestParseURL(t *testing.T) {
{
name: "connection string without database",
url: "mongodb://user:password@localhost:27017",
want: "mongodb://user:password@localhost:27017",
want: "mongodb://user:password@localhost:27017/",
},
{
name: "cosmosdb url",
url: "mongodb+srv://4-0-qa:zFAQ==@4-0-qa.azure:10/a1?appName=@4-testing@&maxIdleTimeMS=120000",
want: "mongodb+srv://4-0-qa:zFAQ%3D%3D@4-0-qa.azure:10/a1?appName=@4-testing@&maxIdleTimeMS=120000",
},
{
name: "cosmosdb url without database with options",
url: "mongodb+srv://tyk:6}3cZQU.9KvM/hVR4qkm-hHqZTu3yg=G@cluster0.zlgvyel.mongodb.net/?retryWrites=true&w=majority",
want: "mongodb+srv://tyk:6%7D3cZQU.9KvM%2FhVR4qkm-hHqZTu3yg%3DG@cluster0.zlgvyel.mongodb.net/?retryWrites=true&w=majority",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 94cb84a

Please sign in to comment.