9
9
"net/url"
10
10
"os"
11
11
"reflect"
12
+ "strconv"
12
13
"strings"
13
14
"testing"
14
15
@@ -139,7 +140,7 @@ func TestRegisterService(t *testing.T) {
139
140
140
141
assert .Equal (t , http .StatusBadRequest , w .Code )
141
142
142
- // register with invalid service.Address
143
+ // register with invalid service.Address(bad uri)
143
144
data := url.Values {}
144
145
data .Set ("name" , "aaa" )
145
146
data .Set ("address" , "&%%" )
@@ -151,22 +152,104 @@ func TestRegisterService(t *testing.T) {
151
152
assert .Equal (t , http .StatusBadRequest , w .Code )
152
153
assert .Contains (t , w .Body .String (), "invalid URL escape" )
153
154
154
- // register with host but no port
155
+ // regist with invalid service.Address(schema)
155
156
data = url.Values {}
156
157
data .Set ("name" , "aaa" )
157
- data .Set ("address" , "http ://127.0.0.1" )
158
+ data .Set ("address" , "fpt ://127.0.0.1:21 " )
158
159
w = httptest .NewRecorder ()
159
160
req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
160
161
req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
161
162
router .ServeHTTP (w , req )
163
+ assert .Equal (t , http .StatusBadRequest , w .Code )
164
+ assert .Contains (t , w .Body .String (), "unsupport schema" )
162
165
166
+ // regist with unempty path
167
+ data = url.Values {}
168
+ data .Set ("name" , "aaa" )
169
+ data .Set ("address" , "http://127.0.0.1:21/" )
170
+ w = httptest .NewRecorder ()
171
+ req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
172
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
173
+ router .ServeHTTP (w , req )
163
174
assert .Equal (t , http .StatusBadRequest , w .Code )
164
- assert .Contains (t , w .Body .String (), "missing port in address" )
175
+ assert .Contains (t , w .Body .String (), "uri path must empty" )
176
+
177
+ // regist with empty host(in direct mode,empty must fail,default is success)
178
+ // in default,use request realhost as host,use 80 as port
179
+ server .IPRevise = false
180
+ data = url.Values {}
181
+ data .Set ("name" , "aaa" )
182
+ data .Set ("address" , "http://" )
183
+ w = httptest .NewRecorder ()
184
+ req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
185
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
186
+ router .ServeHTTP (w , req )
187
+ assert .Equal (t , http .StatusBadRequest , w .Code )
188
+ assert .Contains (t , w .Body .String (), "address is empty" )
189
+
190
+ data = url.Values {}
191
+ data .Set ("name" , "aaa" )
192
+ data .Set ("address" , "http://:8080" )
193
+ w = httptest .NewRecorder ()
194
+ req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
195
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
196
+ router .ServeHTTP (w , req )
197
+ assert .Equal (t , http .StatusBadRequest , w .Code )
198
+ assert .Contains (t , w .Body .String (), "empty host name" )
199
+
200
+ data = url.Values {}
201
+ data .Set ("name" , "aaa" )
202
+ data .Set ("address" , "http://127.0.0.1" )
203
+ w = httptest .NewRecorder ()
204
+ req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
205
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
206
+ router .ServeHTTP (w , req )
207
+ assert .Equal (t , http .StatusOK , w .Code )
208
+
209
+ //change network type
210
+ data = url.Values {}
211
+ data .Set ("name" , "aaa" )
212
+ data .Set ("address" , "http://" )
213
+ data .Set ("iprevise" , "true" )
214
+ w = httptest .NewRecorder ()
215
+ req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
216
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
217
+ router .ServeHTTP (w , req )
218
+ assert .Equal (t , http .StatusOK , w .Code )
219
+
220
+ server .IPRevise = true //use clientip and default port(80)
221
+ data = url.Values {}
222
+ data .Set ("name" , "aaa" )
223
+ data .Set ("address" , "http://" )
224
+ w = httptest .NewRecorder ()
225
+ req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
226
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
227
+ router .ServeHTTP (w , req )
228
+ assert .Equal (t , http .StatusOK , w .Code )
229
+
230
+ data = url.Values {}
231
+ data .Set ("name" , "aaa" )
232
+ data .Set ("address" , "http://:8080" )
233
+ w = httptest .NewRecorder ()
234
+ req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
235
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
236
+ router .ServeHTTP (w , req )
237
+ assert .Equal (t , http .StatusOK , w .Code )
238
+
239
+ data = url.Values {}
240
+ data .Set ("name" , "aaa" )
241
+ data .Set ("address" , "http://127.0.0.1" )
242
+ w = httptest .NewRecorder ()
243
+ req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
244
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
245
+ router .ServeHTTP (w , req )
246
+ assert .Equal (t , http .StatusOK , w .Code )
165
247
166
248
// register with store failure
167
249
expectedS := ServiceUnderTest {
168
- Name : "foo" ,
169
- Address : "http://:64444" , // the real IP is empty in unittest, so server will get a empty one
250
+ Name : "foo" ,
251
+ Address : "http://:64444" , // the real IP is empty in unittest, so server will get a empty one
252
+ IPRevise : strconv .FormatBool (server .IPRevise ),
170
253
}
171
254
testObj := new (MockStore )
172
255
testObj .On ("Get" , "foo" ).Return ([]string {"http://127.0.0.1:66666" })
@@ -177,6 +260,7 @@ func TestRegisterService(t *testing.T) {
177
260
w = httptest .NewRecorder ()
178
261
data .Set ("name" , expectedS .Name )
179
262
data .Set ("address" , expectedS .Address )
263
+ data .Set ("network" , "" )
180
264
req , _ = http .NewRequest ("POST" , "/v1/cover/register" , strings .NewReader (data .Encode ()))
181
265
req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
182
266
router .ServeHTTP (w , req )
0 commit comments