fake_handler.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testing
  14. import (
  15. "io/ioutil"
  16. "net/http"
  17. "net/url"
  18. "reflect"
  19. "sync"
  20. )
  21. // TestInterface is a simple interface providing Errorf, to make injection for
  22. // testing easier (insert 'yo dawg' meme here).
  23. type TestInterface interface {
  24. Errorf(format string, args ...interface{})
  25. Logf(format string, args ...interface{})
  26. }
  27. // LogInterface is a simple interface to allow injection of Logf to report serving errors.
  28. type LogInterface interface {
  29. Logf(format string, args ...interface{})
  30. }
  31. // FakeHandler is to assist in testing HTTP requests. Notice that FakeHandler is
  32. // not thread safe and you must not direct traffic to except for the request
  33. // you want to test. You can do this by hiding it in an http.ServeMux.
  34. type FakeHandler struct {
  35. RequestReceived *http.Request
  36. RequestBody string
  37. StatusCode int
  38. ResponseBody string
  39. // For logging - you can use a *testing.T
  40. // This will keep log messages associated with the test.
  41. T LogInterface
  42. // Enforce "only one use" constraint.
  43. lock sync.Mutex
  44. requestCount int
  45. hasBeenChecked bool
  46. }
  47. func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
  48. f.lock.Lock()
  49. defer f.lock.Unlock()
  50. f.requestCount++
  51. if f.hasBeenChecked {
  52. panic("got request after having been validated")
  53. }
  54. f.RequestReceived = request
  55. response.Header().Set("Content-Type", "application/json")
  56. response.WriteHeader(f.StatusCode)
  57. response.Write([]byte(f.ResponseBody))
  58. bodyReceived, err := ioutil.ReadAll(request.Body)
  59. if err != nil && f.T != nil {
  60. f.T.Logf("Received read error: %v", err)
  61. }
  62. f.RequestBody = string(bodyReceived)
  63. }
  64. func (f *FakeHandler) ValidateRequestCount(t TestInterface, count int) bool {
  65. ok := true
  66. f.lock.Lock()
  67. defer f.lock.Unlock()
  68. if f.requestCount != count {
  69. ok = false
  70. t.Errorf("Expected %d call, but got %d. Only the last call is recorded and checked.", count, f.requestCount)
  71. }
  72. f.hasBeenChecked = true
  73. return ok
  74. }
  75. // ValidateRequest verifies that FakeHandler received a request with expected path, method, and body.
  76. func (f *FakeHandler) ValidateRequest(t TestInterface, expectedPath, expectedMethod string, body *string) {
  77. f.lock.Lock()
  78. defer f.lock.Unlock()
  79. if f.requestCount != 1 {
  80. t.Logf("Expected 1 call, but got %v. Only the last call is recorded and checked.", f.requestCount)
  81. }
  82. f.hasBeenChecked = true
  83. expectURL, err := url.Parse(expectedPath)
  84. if err != nil {
  85. t.Errorf("Couldn't parse %v as a URL.", expectedPath)
  86. }
  87. if f.RequestReceived == nil {
  88. t.Errorf("Unexpected nil request received for %s", expectedPath)
  89. return
  90. }
  91. if f.RequestReceived.URL.Path != expectURL.Path {
  92. t.Errorf("Unexpected request path for request %#v, received: %q, expected: %q", f.RequestReceived, f.RequestReceived.URL.Path, expectURL.Path)
  93. }
  94. if e, a := expectURL.Query(), f.RequestReceived.URL.Query(); !reflect.DeepEqual(e, a) {
  95. t.Errorf("Unexpected query for request %#v, received: %q, expected: %q", f.RequestReceived, a, e)
  96. }
  97. if f.RequestReceived.Method != expectedMethod {
  98. t.Errorf("Unexpected method: %q, expected: %q", f.RequestReceived.Method, expectedMethod)
  99. }
  100. if body != nil {
  101. if *body != f.RequestBody {
  102. t.Errorf("Received body:\n%s\n Doesn't match expected body:\n%s", f.RequestBody, *body)
  103. }
  104. }
  105. }