@@ -364,7 +364,14 @@ func (m *MockTransport) checkMethod(method string) {
364
364
// GetCallCountInfo(). As 2 regexps can match the same URL, the regexp
365
365
// responders are tested in the order they are registered. Registering
366
366
// an already existing regexp responder (same method & same regexp
367
- // string) replaces its responder but does not change its position.
367
+ // string) replaces its responder, but does not change its position.
368
+ //
369
+ // Registering an already existing responder resets the corresponding
370
+ // statistics as returned by GetCallCountInfo().
371
+ //
372
+ // Registering a nil Responder removes the existing one and the
373
+ // corresponding statistics as returned by GetCallCountInfo(). It does
374
+ // nothing if it does not already exist.
368
375
//
369
376
// See RegisterRegexpResponder() to directly pass a *regexp.Regexp.
370
377
//
@@ -410,31 +417,49 @@ func (m *MockTransport) RegisterResponder(method, url string, responder Responde
410
417
}
411
418
412
419
m .mu .Lock ()
413
- m .responders [key ] = responder
414
- m .callCountInfo [key ] = 0
420
+ if responder == nil {
421
+ delete (m .responders , key )
422
+ delete (m .callCountInfo , key )
423
+ } else {
424
+ m .responders [key ] = responder
425
+ m .callCountInfo [key ] = 0
426
+ }
415
427
m .mu .Unlock ()
416
428
}
417
429
418
- func (m * MockTransport ) registerRegexpResponder (regexpResponder regexpResponder ) {
430
+ func (m * MockTransport ) registerRegexpResponder (rxResp regexpResponder ) {
419
431
m .mu .Lock ()
420
432
defer m .mu .Unlock ()
421
433
422
434
found:
423
435
for {
424
436
for i , rr := range m .regexpResponders {
425
- if rr .method == regexpResponder .method && rr .origRx == regexpResponder .origRx {
426
- m .regexpResponders [i ] = regexpResponder
437
+ if rr .method == rxResp .method && rr .origRx == rxResp .origRx {
438
+ if rxResp .responder == nil {
439
+ copy (m .regexpResponders [:i ], m .regexpResponders [i + 1 :])
440
+ m .regexpResponders [len (m .regexpResponders )- 1 ] = regexpResponder {}
441
+ m .regexpResponders = m .regexpResponders [:len (m .regexpResponders )- 1 ]
442
+ } else {
443
+ m .regexpResponders [i ] = rxResp
444
+ }
427
445
break found
428
446
}
429
447
}
430
- m .regexpResponders = append (m .regexpResponders , regexpResponder )
448
+ if rxResp .responder != nil {
449
+ m .regexpResponders = append (m .regexpResponders , rxResp )
450
+ }
431
451
break // nolint: staticcheck
432
452
}
433
453
434
- m .callCountInfo [internal.RouteKey {
435
- Method : regexpResponder .method ,
436
- URL : regexpResponder .origRx ,
437
- }] = 0
454
+ key := internal.RouteKey {
455
+ Method : rxResp .method ,
456
+ URL : rxResp .origRx ,
457
+ }
458
+ if rxResp .responder == nil {
459
+ delete (m .callCountInfo , key )
460
+ } else {
461
+ m .callCountInfo [key ] = 0
462
+ }
438
463
}
439
464
440
465
// RegisterRegexpResponder adds a new responder, associated with a given
@@ -446,7 +471,12 @@ found:
446
471
// As 2 regexps can match the same URL, the regexp responders are
447
472
// tested in the order they are registered. Registering an already
448
473
// existing regexp responder (same method & same regexp string)
449
- // replaces its responder but does not change its position.
474
+ // replaces its responder, but does not change its position, and
475
+ // resets the corresponding statistics as returned by GetCallCountInfo().
476
+ //
477
+ // Registering a nil Responder removes the existing one and the
478
+ // corresponding statistics as returned by GetCallCountInfo(). It does
479
+ // nothing if it does not already exist.
450
480
//
451
481
// A "=~" prefix is added to the stringified regexp in the statistics
452
482
// returned by GetCallCountInfo().
@@ -483,6 +513,13 @@ func (m *MockTransport) RegisterRegexpResponder(method string, urlRegexp *regexp
483
513
// Unlike RegisterResponder, path cannot be prefixed by "=~" to say it
484
514
// is a regexp. If it is, a panic occurs.
485
515
//
516
+ // Registering an already existing responder resets the corresponding
517
+ // statistics as returned by GetCallCountInfo().
518
+ //
519
+ // Registering a nil Responder removes the existing one and the
520
+ // corresponding statistics as returned by GetCallCountInfo(). It does
521
+ // nothing if it does not already exist.
522
+ //
486
523
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
487
524
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
488
525
// mistake. This panic can be disabled by setting m.DontCheckMethod to
@@ -581,6 +618,9 @@ func sortedQuery(m url.Values) string {
581
618
// at /go/src/testing/testing.go:865
582
619
// testing.tRunner()
583
620
// at /go/src/runtime/asm_amd64.s:1337
621
+ //
622
+ // If responder is passed as nil, the default behavior
623
+ // (httpmock.ConnectionFailure) is re-enabled.
584
624
func (m * MockTransport ) RegisterNoResponder (responder Responder ) {
585
625
m .mu .Lock ()
586
626
m .noResponder = responder
@@ -812,7 +852,14 @@ func DeactivateAndReset() {
812
852
// GetCallCountInfo(). As 2 regexps can match the same URL, the regexp
813
853
// responders are tested in the order they are registered. Registering
814
854
// an already existing regexp responder (same method & same regexp
815
- // string) replaces its responder but does not change its position.
855
+ // string) replaces its responder, but does not change its position.
856
+ //
857
+ // Registering an already existing responder resets the corresponding
858
+ // statistics as returned by GetCallCountInfo().
859
+ //
860
+ // Registering a nil Responder removes the existing one and the
861
+ // corresponding statistics as returned by GetCallCountInfo(). It does
862
+ // nothing if it does not already exist.
816
863
//
817
864
// See RegisterRegexpResponder() to directly pass a *regexp.Regexp.
818
865
//
@@ -852,7 +899,12 @@ func RegisterResponder(method, url string, responder Responder) {
852
899
// As 2 regexps can match the same URL, the regexp responders are
853
900
// tested in the order they are registered. Registering an already
854
901
// existing regexp responder (same method & same regexp string)
855
- // replaces its responder but does not change its position.
902
+ // replaces its responder, but does not change its position, and
903
+ // resets the corresponding statistics as returned by GetCallCountInfo().
904
+ //
905
+ // Registering a nil Responder removes the existing one and the
906
+ // corresponding statistics as returned by GetCallCountInfo(). It does
907
+ // nothing if it does not already exist.
856
908
//
857
909
// A "=~" prefix is added to the stringified regexp in the statistics
858
910
// returned by GetCallCountInfo().
@@ -879,6 +931,16 @@ func RegisterRegexpResponder(method string, urlRegexp *regexp.Regexp, responder
879
931
// If the query type is not recognized or the string cannot be parsed
880
932
// using net/url.ParseQuery, a panic() occurs.
881
933
//
934
+ // Unlike RegisterResponder, path cannot be prefixed by "=~" to say it
935
+ // is a regexp. If it is, a panic occurs.
936
+ //
937
+ // Registering an already existing responder resets the corresponding
938
+ // statistics as returned by GetCallCountInfo().
939
+ //
940
+ // Registering a nil Responder removes the existing one and the
941
+ // corresponding statistics as returned by GetCallCountInfo(). It does
942
+ // nothing if it does not already exist.
943
+ //
882
944
// Example using a net/url.Values:
883
945
// func TestFetchArticles(t *testing.T) {
884
946
// httpmock.Activate()
0 commit comments