@@ -338,6 +338,206 @@ func TestResultIntoWithNoBodyReturnsErr(t *testing.T) {
338
338
}
339
339
}
340
340
341
+ func TestURLTemplate (t * testing.T ) {
342
+ uri , _ := url .Parse ("http://localhost/some/base/url/path" )
343
+ uriSingleSlash , _ := url .Parse ("http://localhost/" )
344
+ testCases := []struct {
345
+ Request * Request
346
+ ExpectedFullURL string
347
+ ExpectedFinalURL string
348
+ }{
349
+ {
350
+ // non dynamic client
351
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("POST" ).
352
+ Prefix ("api" , "v1" ).Resource ("r1" ).Namespace ("ns" ).Name ("nm" ).Param ("p0" , "v0" ),
353
+ ExpectedFullURL : "http://localhost/some/base/url/path/api/v1/namespaces/ns/r1/nm?p0=v0" ,
354
+ ExpectedFinalURL : "http://localhost/some/base/url/path/api/v1/namespaces/%7Bnamespace%7D/r1/%7Bname%7D?p0=%7Bvalue%7D" ,
355
+ },
356
+ {
357
+ // non dynamic client with wrong api group
358
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("POST" ).
359
+ Prefix ("pre1" , "v1" ).Resource ("r1" ).Namespace ("ns" ).Name ("nm" ).Param ("p0" , "v0" ),
360
+ ExpectedFullURL : "http://localhost/some/base/url/path/pre1/v1/namespaces/ns/r1/nm?p0=v0" ,
361
+ ExpectedFinalURL : "http://localhost/%7Bprefix%7D" ,
362
+ },
363
+ {
364
+ // dynamic client with core group + namespace + resourceResource (with name)
365
+ // /api/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME
366
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
367
+ Prefix ("/api/v1/namespaces/ns/r1/name1" ),
368
+ ExpectedFullURL : "http://localhost/some/base/url/path/api/v1/namespaces/ns/r1/name1" ,
369
+ ExpectedFinalURL : "http://localhost/some/base/url/path/api/v1/namespaces/%7Bnamespace%7D/r1/%7Bname%7D" ,
370
+ },
371
+ {
372
+ // dynamic client with named group + namespace + resourceResource (with name)
373
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME
374
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
375
+ Prefix ("/apis/g1/v1/namespaces/ns/r1/name1" ),
376
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/g1/v1/namespaces/ns/r1/name1" ,
377
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/g1/v1/namespaces/%7Bnamespace%7D/r1/%7Bname%7D" ,
378
+ },
379
+ {
380
+ // dynamic client with core group + namespace + resourceResource (with NO name)
381
+ // /api/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE
382
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
383
+ Prefix ("/api/v1/namespaces/ns/r1" ),
384
+ ExpectedFullURL : "http://localhost/some/base/url/path/api/v1/namespaces/ns/r1" ,
385
+ ExpectedFinalURL : "http://localhost/some/base/url/path/api/v1/namespaces/%7Bnamespace%7D/r1" ,
386
+ },
387
+ {
388
+ // dynamic client with named group + namespace + resourceResource (with NO name)
389
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE
390
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
391
+ Prefix ("/apis/g1/v1/namespaces/ns/r1" ),
392
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/g1/v1/namespaces/ns/r1" ,
393
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/g1/v1/namespaces/%7Bnamespace%7D/r1" ,
394
+ },
395
+ {
396
+ // dynamic client with core group + resourceResource (with name)
397
+ // /api/$RESOURCEVERSION/$RESOURCE/%NAME
398
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
399
+ Prefix ("/api/v1/r1/name1" ),
400
+ ExpectedFullURL : "http://localhost/some/base/url/path/api/v1/r1/name1" ,
401
+ ExpectedFinalURL : "http://localhost/some/base/url/path/api/v1/r1/%7Bname%7D" ,
402
+ },
403
+ {
404
+ // dynamic client with named group + resourceResource (with name)
405
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/$RESOURCE/%NAME
406
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
407
+ Prefix ("/apis/g1/v1/r1/name1" ),
408
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/g1/v1/r1/name1" ,
409
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/g1/v1/r1/%7Bname%7D" ,
410
+ },
411
+ {
412
+ // dynamic client with named group + namespace + resourceResource (with name) + subresource
413
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME/$SUBRESOURCE
414
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
415
+ Prefix ("/apis/namespaces/namespaces/namespaces/namespaces/namespaces/namespaces/finalize" ),
416
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/namespaces/namespaces/namespaces/finalize" ,
417
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/%7Bnamespace%7D/namespaces/%7Bname%7D/finalize" ,
418
+ },
419
+ {
420
+ // dynamic client with named group + namespace + resourceResource (with name)
421
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME
422
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
423
+ Prefix ("/apis/namespaces/namespaces/namespaces/namespaces/namespaces/namespaces" ),
424
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/namespaces/namespaces/namespaces" ,
425
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/%7Bnamespace%7D/namespaces/%7Bname%7D" ,
426
+ },
427
+ {
428
+ // dynamic client with named group + namespace + resourceResource (with NO name) + subresource
429
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%SUBRESOURCE
430
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
431
+ Prefix ("/apis/namespaces/namespaces/namespaces/namespaces/namespaces/finalize" ),
432
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/namespaces/namespaces/finalize" ,
433
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/%7Bnamespace%7D/namespaces/finalize" ,
434
+ },
435
+ {
436
+ // dynamic client with named group + namespace + resourceResource (with NO name) + subresource
437
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%SUBRESOURCE
438
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
439
+ Prefix ("/apis/namespaces/namespaces/namespaces/namespaces/namespaces/status" ),
440
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/namespaces/namespaces/status" ,
441
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/%7Bnamespace%7D/namespaces/status" ,
442
+ },
443
+ {
444
+ // dynamic client with named group + namespace + resourceResource (with no name)
445
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME
446
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
447
+ Prefix ("/apis/namespaces/namespaces/namespaces/namespaces/namespaces" ),
448
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/namespaces/namespaces" ,
449
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/%7Bnamespace%7D/namespaces" ,
450
+ },
451
+ {
452
+ // dynamic client with named group + resourceResource (with name) + subresource
453
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME
454
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
455
+ Prefix ("/apis/namespaces/namespaces/namespaces/namespaces/finalize" ),
456
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/namespaces/finalize" ,
457
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/%7Bname%7D/finalize" ,
458
+ },
459
+ {
460
+ // dynamic client with named group + resourceResource (with name) + subresource
461
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME
462
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
463
+ Prefix ("/apis/namespaces/namespaces/namespaces/namespaces/status" ),
464
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/namespaces/status" ,
465
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/%7Bname%7D/status" ,
466
+ },
467
+ {
468
+ // dynamic client with named group + resourceResource (with name)
469
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/$RESOURCE/%NAME
470
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
471
+ Prefix ("/apis/namespaces/namespaces/namespaces/namespaces" ),
472
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/namespaces" ,
473
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces/%7Bname%7D" ,
474
+ },
475
+ {
476
+ // dynamic client with named group + resourceResource (with no name)
477
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/$RESOURCE/%NAME
478
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
479
+ Prefix ("/apis/namespaces/namespaces/namespaces" ),
480
+ ExpectedFullURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces" ,
481
+ ExpectedFinalURL : "http://localhost/some/base/url/path/apis/namespaces/namespaces/namespaces" ,
482
+ },
483
+ {
484
+ // dynamic client with wrong api group + namespace + resourceResource (with name) + subresource
485
+ // /apis/$NAMEDGROUPNAME/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME/$SUBRESOURCE
486
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
487
+ Prefix ("/pre1/namespaces/namespaces/namespaces/namespaces/namespaces/namespaces/finalize" ),
488
+ ExpectedFullURL : "http://localhost/some/base/url/path/pre1/namespaces/namespaces/namespaces/namespaces/namespaces/namespaces/finalize" ,
489
+ ExpectedFinalURL : "http://localhost/%7Bprefix%7D" ,
490
+ },
491
+ {
492
+ // dynamic client with core group + namespace + resourceResource (with name) where baseURL is a single /
493
+ // /api/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME
494
+ Request : NewRequestWithClient (uriSingleSlash , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
495
+ Prefix ("/api/v1/namespaces/ns/r2/name1" ),
496
+ ExpectedFullURL : "http://localhost/api/v1/namespaces/ns/r2/name1" ,
497
+ ExpectedFinalURL : "http://localhost/api/v1/namespaces/%7Bnamespace%7D/r2/%7Bname%7D" ,
498
+ },
499
+ {
500
+ // dynamic client with core group + namespace + resourceResource (with name) where baseURL is 'some/base/url/path'
501
+ // /api/$RESOURCEVERSION/namespaces/$NAMESPACE/$RESOURCE/%NAME
502
+ Request : NewRequestWithClient (uri , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
503
+ Prefix ("/api/v1/namespaces/ns/r3/name1" ),
504
+ ExpectedFullURL : "http://localhost/some/base/url/path/api/v1/namespaces/ns/r3/name1" ,
505
+ ExpectedFinalURL : "http://localhost/some/base/url/path/api/v1/namespaces/%7Bnamespace%7D/r3/%7Bname%7D" ,
506
+ },
507
+ {
508
+ // dynamic client where baseURL is a single /
509
+ // /
510
+ Request : NewRequestWithClient (uriSingleSlash , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
511
+ Prefix ("/" ),
512
+ ExpectedFullURL : "http://localhost/" ,
513
+ ExpectedFinalURL : "http://localhost/" ,
514
+ },
515
+ {
516
+ // dynamic client where baseURL is a single /
517
+ // /version
518
+ Request : NewRequestWithClient (uriSingleSlash , "" , ClientContentConfig {GroupVersion : schema.GroupVersion {Group : "test" }}, nil ).Verb ("DELETE" ).
519
+ Prefix ("/version" ),
520
+ ExpectedFullURL : "http://localhost/version" ,
521
+ ExpectedFinalURL : "http://localhost/version" ,
522
+ },
523
+ }
524
+ for i , testCase := range testCases {
525
+ r := testCase .Request
526
+ full := r .URL ()
527
+ if full .String () != testCase .ExpectedFullURL {
528
+ t .Errorf ("%d: unexpected initial URL: %s %s" , i , full , testCase .ExpectedFullURL )
529
+ }
530
+ actualURL := r .finalURLTemplate ()
531
+ actual := actualURL .String ()
532
+ if actual != testCase .ExpectedFinalURL {
533
+ t .Errorf ("%d: unexpected URL template: %s %s" , i , actual , testCase .ExpectedFinalURL )
534
+ }
535
+ if r .URL ().String () != full .String () {
536
+ t .Errorf ("%d, creating URL template changed request: %s -> %s" , i , full .String (), r .URL ().String ())
537
+ }
538
+ }
539
+ }
540
+
341
541
func TestTransformResponse (t * testing.T ) {
342
542
invalid := []byte ("aaaaa" )
343
543
uri , _ := url .Parse ("http://localhost" )
0 commit comments