@@ -33,8 +33,11 @@ import (
33
33
)
34
34
35
35
const (
36
- testURL = "https://test-db.firebaseio.com"
37
- defaultMaxRetries = 1
36
+ testURL = "https://test-db.firebaseio.com"
37
+ testEmulatorNamespace = "test-db"
38
+ testEmulatorBaseURL = "http://localhost:9000"
39
+ testEmulatorURL = "localhost:9000?ns=test-db"
40
+ defaultMaxRetries = 1
38
41
)
39
42
40
43
var (
@@ -87,52 +90,96 @@ func TestMain(m *testing.M) {
87
90
}
88
91
89
92
func TestNewClient (t * testing.T ) {
90
- c , err := NewClient ( context . Background (), & internal. DatabaseConfig {
91
- Opts : testOpts ,
92
- URL : testURL ,
93
- AuthOverride : make ( map [ string ] interface {}),
94
- })
95
- if err != nil {
96
- t . Fatal ( err )
97
- }
98
- if c . url != testURL {
99
- t . Errorf ( "NewClient().url = %q; want = %q" , c . url , testURL )
100
- }
101
- if c . hc == nil {
102
- t . Errorf ( "NewClient().hc = nil; want non-nil" )
93
+ cases := [] * struct {
94
+ Name string
95
+ URL string
96
+ EnvURL string
97
+ ExpectedBaseURL string
98
+ ExpectedNamespace string
99
+ ExpectError bool
100
+ }{
101
+ { Name : "production url" , URL : testURL , ExpectedBaseURL : testURL , ExpectedNamespace : "" },
102
+ { Name : "emulator - success" , URL : testEmulatorURL , ExpectedBaseURL : testEmulatorBaseURL , ExpectedNamespace : testEmulatorNamespace },
103
+ { Name : "emulator - missing namespace should error" , URL : "localhost:9000" , ExpectError : true },
104
+ { Name : "emulator - if url contains hostname it uses the primary domain" , URL : "rtdb-go.emulator:9000" , ExpectedBaseURL : "http://rtdb-go.emulator:9000" , ExpectedNamespace : "rtdb-go" },
105
+ { Name : "emulator env - success" , EnvURL : testEmulatorURL , ExpectedBaseURL : testEmulatorBaseURL , ExpectedNamespace : testEmulatorNamespace },
103
106
}
104
- if c .authOverride != "" {
105
- t .Errorf ("NewClient().ao = %q; want = %q" , c .authOverride , "" )
107
+ for _ , tc := range cases {
108
+ t .Run (tc .Name , func (t * testing.T ) {
109
+ t .Setenv (emulatorDatabaseEnvVar , tc .EnvURL )
110
+ fromEnv := os .Getenv (emulatorDatabaseEnvVar )
111
+ fmt .Printf (fromEnv )
112
+ c , err := NewClient (context .Background (), & internal.DatabaseConfig {
113
+ Opts : testOpts ,
114
+ URL : tc .URL ,
115
+ AuthOverride : make (map [string ]interface {}),
116
+ })
117
+ if err != nil && tc .ExpectError {
118
+ return
119
+ }
120
+ if err != nil && ! tc .ExpectError {
121
+ t .Fatal (err )
122
+ }
123
+ if err == nil && tc .ExpectError {
124
+ t .Fatal ("expected error" )
125
+ }
126
+ if c .dbURLConfig .BaseURL != tc .ExpectedBaseURL {
127
+ t .Errorf ("NewClient().dbURLConfig.BaseURL = %q; want = %q" , c .dbURLConfig .BaseURL , tc .ExpectedBaseURL )
128
+ }
129
+ if c .dbURLConfig .Namespace != tc .ExpectedNamespace {
130
+ t .Errorf ("NewClient(%v).Namespace = %q; want = %q" , tc , c .dbURLConfig .Namespace , tc .ExpectedNamespace )
131
+ }
132
+ if c .hc == nil {
133
+ t .Errorf ("NewClient().hc = nil; want non-nil" )
134
+ }
135
+ if c .authOverride != "" {
136
+ t .Errorf ("NewClient().ao = %q; want = %q" , c .authOverride , "" )
137
+ }
138
+ })
106
139
}
107
140
}
108
141
109
142
func TestNewClientAuthOverrides (t * testing.T ) {
110
- cases := []map [string ]interface {}{
111
- nil ,
112
- {"uid" : "user1" },
143
+ cases := []* struct {
144
+ Name string
145
+ Params map [string ]interface {}
146
+ URL string
147
+ ExpectedBaseURL string
148
+ ExpectedNamespace string
149
+ }{
150
+ {Name : "production - without override" , Params : nil , URL : testURL , ExpectedBaseURL : testURL , ExpectedNamespace : "" },
151
+ {Name : "production - with override" , Params : map [string ]interface {}{"uid" : "user1" }, URL : testURL , ExpectedBaseURL : testURL , ExpectedNamespace : "" },
152
+
153
+ {Name : "emulator - with no query params" , Params : nil , URL : testEmulatorURL , ExpectedBaseURL : testEmulatorBaseURL , ExpectedNamespace : testEmulatorNamespace },
154
+ {Name : "emulator - with override" , Params : map [string ]interface {}{"uid" : "user1" }, URL : testEmulatorURL , ExpectedBaseURL : testEmulatorBaseURL , ExpectedNamespace : testEmulatorNamespace },
113
155
}
114
156
for _ , tc := range cases {
115
- c , err := NewClient (context .Background (), & internal.DatabaseConfig {
116
- Opts : testOpts ,
117
- URL : testURL ,
118
- AuthOverride : tc ,
157
+ t .Run (tc .Name , func (t * testing.T ) {
158
+ c , err := NewClient (context .Background (), & internal.DatabaseConfig {
159
+ Opts : testOpts ,
160
+ URL : tc .URL ,
161
+ AuthOverride : tc .Params ,
162
+ })
163
+ if err != nil {
164
+ t .Fatal (err )
165
+ }
166
+ if c .dbURLConfig .BaseURL != tc .ExpectedBaseURL {
167
+ t .Errorf ("NewClient(%v).baseURL = %q; want = %q" , tc , c .dbURLConfig .BaseURL , tc .ExpectedBaseURL )
168
+ }
169
+ if c .dbURLConfig .Namespace != tc .ExpectedNamespace {
170
+ t .Errorf ("NewClient(%v).Namespace = %q; want = %q" , tc , c .dbURLConfig .Namespace , tc .ExpectedNamespace )
171
+ }
172
+ if c .hc == nil {
173
+ t .Errorf ("NewClient(%v).hc = nil; want non-nil" , tc )
174
+ }
175
+ b , err := json .Marshal (tc .Params )
176
+ if err != nil {
177
+ t .Fatal (err )
178
+ }
179
+ if c .authOverride != string (b ) {
180
+ t .Errorf ("NewClient(%v).ao = %q; want = %q" , tc , c .authOverride , string (b ))
181
+ }
119
182
})
120
- if err != nil {
121
- t .Fatal (err )
122
- }
123
- if c .url != testURL {
124
- t .Errorf ("NewClient(%v).url = %q; want = %q" , tc , c .url , testURL )
125
- }
126
- if c .hc == nil {
127
- t .Errorf ("NewClient(%v).hc = nil; want non-nil" , tc )
128
- }
129
- b , err := json .Marshal (tc )
130
- if err != nil {
131
- t .Fatal (err )
132
- }
133
- if c .authOverride != string (b ) {
134
- t .Errorf ("NewClient(%v).ao = %q; want = %q" , tc , c .authOverride , string (b ))
135
- }
136
183
}
137
184
}
138
185
@@ -149,8 +196,8 @@ func TestValidURLS(t *testing.T) {
149
196
if err != nil {
150
197
t .Fatal (err )
151
198
}
152
- if c .url != tc {
153
- t .Errorf ("NewClient(%v).url = %q; want = %q" , tc , c .url , testURL )
199
+ if c .dbURLConfig . BaseURL != tc {
200
+ t .Errorf ("NewClient(%v).url = %q; want = %q" , tc , c .dbURLConfig . BaseURL , testURL )
154
201
}
155
202
}
156
203
}
@@ -161,6 +208,7 @@ func TestInvalidURL(t *testing.T) {
161
208
"foo" ,
162
209
"http://db.firebaseio.com" ,
163
210
"http://firebase.google.com" ,
211
+ "http://localhost:9000" ,
164
212
}
165
213
for _ , tc := range cases {
166
214
c , err := NewClient (context .Background (), & internal.DatabaseConfig {
@@ -402,7 +450,7 @@ func (s *mockServer) Start(c *Client) *httptest.Server {
402
450
w .Write (b )
403
451
})
404
452
s .srv = httptest .NewServer (handler )
405
- c .url = s .srv .URL
453
+ c .dbURLConfig . BaseURL = s .srv .URL
406
454
return s .srv
407
455
}
408
456
0 commit comments