@@ -2345,86 +2345,103 @@ func TestLoadAPIParams_APIKeyFromEnv_ConfigTakesPrecedence(t *testing.T) {
2345
2345
assert .Equal (t , "00000000-0000-4000-8000-000000000000" , params .Key )
2346
2346
}
2347
2347
2348
- func TestLoadAPIParams_APIUrl (t * testing.T ) {
2348
+ func TestLoadAPIParams_APIUrl_Sanitize (t * testing.T ) {
2349
2349
ctx := context .Background ()
2350
2350
2351
2351
tests := map [string ]struct {
2352
- ViperAPIUrl string
2353
- ViperAPIUrlConfig string
2354
- ViperAPIUrlOld string
2355
- Expected cmdparams.API
2352
+ URL string
2353
+ Expected string
2356
2354
}{
2357
- "api url flag takes precedence" : {
2358
- ViperAPIUrl : "http://localhost:8080" ,
2359
- ViperAPIUrlConfig : "http://localhost:8081" ,
2360
- ViperAPIUrlOld : "http://localhost:8082" ,
2361
- Expected : cmdparams.API {
2362
- Key : "00000000-0000-4000-8000-000000000000" ,
2363
- URL : "http://localhost:8080" ,
2364
- Hostname : "my-computer" ,
2365
- },
2366
- },
2367
- "api url deprecated flag takes precedence" : {
2368
- ViperAPIUrlConfig : "http://localhost:8081" ,
2369
- ViperAPIUrlOld : "http://localhost:8082" ,
2370
- Expected : cmdparams.API {
2371
- Key : "00000000-0000-4000-8000-000000000000" ,
2372
- URL : "http://localhost:8082" ,
2373
- Hostname : "my-computer" ,
2374
- },
2375
- },
2376
- "api url from config" : {
2377
- ViperAPIUrlConfig : "http://localhost:8081" ,
2378
- Expected : cmdparams.API {
2379
- Key : "00000000-0000-4000-8000-000000000000" ,
2380
- URL : "http://localhost:8081" ,
2381
- Hostname : "my-computer" ,
2382
- },
2383
- },
2384
2355
"api url with legacy heartbeats endpoint" : {
2385
- ViperAPIUrl : "http://localhost:8080/api/v1/heartbeats.bulk" ,
2386
- Expected : cmdparams. API {
2387
- Key : "00000000-0000-4000-8000-000000000000" ,
2388
- URL : "http://localhost:8080/api/v1" ,
2389
- Hostname : "my-computer " ,
2390
- } ,
2356
+ URL : "http://localhost:8080/api/v1/heartbeats.bulk" ,
2357
+ Expected : "http://localhost:8080/api/v1" ,
2358
+ } ,
2359
+ "api url with users heartbeats endpoint" : {
2360
+ URL : "http://localhost:8080/users/current/heartbeats " ,
2361
+ Expected : "http://localhost:8080" ,
2391
2362
},
2392
2363
"api url with trailing slash" : {
2393
- ViperAPIUrl : "http://localhost:8080/api/" ,
2394
- Expected : cmdparams.API {
2395
- Key : "00000000-0000-4000-8000-000000000000" ,
2396
- URL : "http://localhost:8080/api" ,
2397
- Hostname : "my-computer" ,
2398
- },
2364
+ URL : "http://localhost:8080/api/" ,
2365
+ Expected : "http://localhost:8080/api" ,
2399
2366
},
2400
2367
"api url with wakapi style endpoint" : {
2401
- ViperAPIUrl : "http://localhost:8080/api/heartbeat" ,
2402
- Expected : cmdparams.API {
2403
- Key : "00000000-0000-4000-8000-000000000000" ,
2404
- URL : "http://localhost:8080/api" ,
2405
- Hostname : "my-computer" ,
2406
- },
2368
+ URL : "http://localhost:8080/api/heartbeat" ,
2369
+ Expected : "http://localhost:8080/api" ,
2407
2370
},
2408
2371
}
2409
2372
2410
2373
for name , test := range tests {
2411
2374
t .Run (name , func (t * testing.T ) {
2412
2375
v := viper .New ()
2413
- v .Set ("hostname" , "my-computer" )
2414
- v .Set ("timeout" , 0 )
2415
2376
v .Set ("key" , "00000000-0000-4000-8000-000000000000" )
2416
- v .Set ("api-url" , test .ViperAPIUrl )
2417
- v .Set ("apiurl" , test .ViperAPIUrlOld )
2418
- v .Set ("settings.api_url" , test .ViperAPIUrlConfig )
2377
+ v .Set ("api-url" , test .URL )
2419
2378
2420
2379
params , err := cmdparams .LoadAPIParams (ctx , v )
2421
2380
require .NoError (t , err )
2422
2381
2423
- assert .Equal (t , test .Expected , params )
2382
+ assert .Equal (t , test .Expected , params . URL )
2424
2383
})
2425
2384
}
2426
2385
}
2427
2386
2387
+ func TestLoadAPIParams_Url (t * testing.T ) {
2388
+ ctx := context .Background ()
2389
+
2390
+ v := viper .New ()
2391
+
2392
+ v .Set ("key" , "00000000-0000-4000-8000-000000000000" )
2393
+ v .Set ("api-url" , "http://localhost:8080" )
2394
+
2395
+ params , err := cmdparams .LoadAPIParams (ctx , v )
2396
+ require .NoError (t , err )
2397
+
2398
+ assert .Equal (t , "http://localhost:8080" , params .URL )
2399
+ }
2400
+
2401
+ func TestLoadAPIParams_Url_FlagTakesPrecedence (t * testing.T ) {
2402
+ ctx := context .Background ()
2403
+
2404
+ v := viper .New ()
2405
+
2406
+ v .Set ("key" , "00000000-0000-4000-8000-000000000000" )
2407
+ v .Set ("api-url" , "http://localhost:8080" )
2408
+ v .Set ("settings.api_url" , "http://localhost:8081" )
2409
+
2410
+ params , err := cmdparams .LoadAPIParams (ctx , v )
2411
+ require .NoError (t , err )
2412
+
2413
+ assert .Equal (t , "http://localhost:8080" , params .URL )
2414
+ }
2415
+
2416
+ func TestLoadAPIParams_Url_FlagDeprecatedTakesPrecedence (t * testing.T ) {
2417
+ ctx := context .Background ()
2418
+
2419
+ v := viper .New ()
2420
+
2421
+ v .Set ("key" , "00000000-0000-4000-8000-000000000000" )
2422
+ v .Set ("apiurl" , "http://localhost:8080" )
2423
+ v .Set ("settings.api_url" , "http://localhost:8081" )
2424
+
2425
+ params , err := cmdparams .LoadAPIParams (ctx , v )
2426
+ require .NoError (t , err )
2427
+
2428
+ assert .Equal (t , "http://localhost:8080" , params .URL )
2429
+ }
2430
+
2431
+ func TestLoadAPIParams_Url_FromConfig (t * testing.T ) {
2432
+ ctx := context .Background ()
2433
+
2434
+ v := viper .New ()
2435
+
2436
+ v .Set ("key" , "00000000-0000-4000-8000-000000000000" )
2437
+ v .Set ("settings.api_url" , "http://localhost:8081" )
2438
+
2439
+ params , err := cmdparams .LoadAPIParams (ctx , v )
2440
+ require .NoError (t , err )
2441
+
2442
+ assert .Equal (t , "http://localhost:8081" , params .URL )
2443
+ }
2444
+
2428
2445
func TestLoadAPIParams_Url_Default (t * testing.T ) {
2429
2446
v := viper .New ()
2430
2447
v .Set ("key" , "00000000-0000-4000-8000-000000000000" )
@@ -2444,7 +2461,7 @@ func TestLoadAPIParams_Url_InvalidFormat(t *testing.T) {
2444
2461
2445
2462
var errauth api.ErrAuth
2446
2463
2447
- assert .ErrorAs (t , err , & errauth )
2464
+ require .ErrorAs (t , err , & errauth )
2448
2465
assert .EqualError (t , errauth , `invalid api url: parse "http://in valid": invalid character " " in host name` )
2449
2466
}
2450
2467
0 commit comments