@@ -4,11 +4,15 @@ import (
4
4
"database/sql"
5
5
sqldriver "database/sql/driver"
6
6
"fmt"
7
- // "io/ioutil"
8
- // "log"
7
+ "net/url"
9
8
"testing"
9
+ )
10
10
11
+ import (
11
12
"github.com/go-sql-driver/mysql"
13
+ )
14
+
15
+ import (
12
16
dt "github.com/golang-migrate/migrate/database/testing"
13
17
mt "github.com/golang-migrate/migrate/testing"
14
18
)
@@ -97,3 +101,55 @@ func TestLockWorks(t *testing.T) {
97
101
}
98
102
})
99
103
}
104
+
105
+ func TestURLToMySQLConfig (t * testing.T ) {
106
+ testcases := []struct {
107
+ name string
108
+ urlStr string
109
+ expectedDSN string // empty string signifies that an error is expected
110
+ }{
111
+ {name : "no user/password" , urlStr : "mysql://tcp(127.0.0.1:3306)/myDB?multiStatements=true" ,
112
+ expectedDSN : "tcp(127.0.0.1:3306)/myDB?multiStatements=true" },
113
+ {name : "only user" , urlStr : "mysql://username@tcp(127.0.0.1:3306)/myDB?multiStatements=true" ,
114
+ expectedDSN : "username@tcp(127.0.0.1:3306)/myDB?multiStatements=true" },
115
+ {name : "only user - with encoded :" ,
116
+ urlStr : "mysql://username%3A@tcp(127.0.0.1:3306)/myDB?multiStatements=true" ,
117
+ expectedDSN : "username:@tcp(127.0.0.1:3306)/myDB?multiStatements=true" },
118
+ {name : "only user - with encoded @" ,
119
+ urlStr : "mysql://username%40@tcp(127.0.0.1:3306)/myDB?multiStatements=true" ,
120
+ expectedDSN : "username@@tcp(127.0.0.1:3306)/myDB?multiStatements=true" },
121
+ {name : "user/password" , urlStr : "mysql://username:password@tcp(127.0.0.1:3306)/myDB?multiStatements=true" ,
122
+ expectedDSN : "username:password@tcp(127.0.0.1:3306)/myDB?multiStatements=true" },
123
+ // Not supported yet: https://github.com/go-sql-driver/mysql/issues/591
124
+ // {name: "user/password - user with encoded :",
125
+ // urlStr: "mysql://username%3A:password@tcp(127.0.0.1:3306)/myDB?multiStatements=true",
126
+ // expectedDSN: "username::pasword@tcp(127.0.0.1:3306)/myDB?multiStatements=true"},
127
+ {name : "user/password - user with encoded @" ,
128
+ urlStr : "mysql://username%40:password@tcp(127.0.0.1:3306)/myDB?multiStatements=true" ,
129
+ expectedDSN : "username@:password@tcp(127.0.0.1:3306)/myDB?multiStatements=true" },
130
+ {name : "user/password - password with encoded :" ,
131
+ urlStr : "mysql://username:password%3A@tcp(127.0.0.1:3306)/myDB?multiStatements=true" ,
132
+ expectedDSN : "username:password:@tcp(127.0.0.1:3306)/myDB?multiStatements=true" },
133
+ {name : "user/password - password with encoded @" ,
134
+ urlStr : "mysql://username:password%40@tcp(127.0.0.1:3306)/myDB?multiStatements=true" ,
135
+ expectedDSN : "username:password@@tcp(127.0.0.1:3306)/myDB?multiStatements=true" },
136
+ }
137
+ for _ , tc := range testcases {
138
+ t .Run (tc .name , func (t * testing.T ) {
139
+ u , err := url .Parse (tc .urlStr )
140
+ if err != nil {
141
+ t .Fatal ("Failed to parse url string:" , tc .urlStr , "error:" , err )
142
+ }
143
+ if config , err := urlToMySQLConfig (* u ); err == nil {
144
+ dsn := config .FormatDSN ()
145
+ if dsn != tc .expectedDSN {
146
+ t .Error ("Got unexpected DSN:" , dsn , "!=" , tc .expectedDSN )
147
+ }
148
+ } else {
149
+ if tc .expectedDSN != "" {
150
+ t .Error ("Got unexpected error:" , err , "urlStr:" , tc .urlStr )
151
+ }
152
+ }
153
+ })
154
+ }
155
+ }
0 commit comments