ValidateRequest verifies that FakeHandler received a request with expected path, method, and body.
(t TestInterface, expectedPath, expectedMethod string, body *string)
| 107 | |
| 108 | // ValidateRequest verifies that FakeHandler received a request with expected path, method, and body. |
| 109 | func (f *FakeHandler) ValidateRequest(t TestInterface, expectedPath, expectedMethod string, body *string) { |
| 110 | f.lock.Lock() |
| 111 | defer f.lock.Unlock() |
| 112 | if f.requestCount != 1 { |
| 113 | t.Logf("Expected 1 call, but got %v. Only the last call is recorded and checked.", f.requestCount) |
| 114 | } |
| 115 | f.hasBeenChecked = true |
| 116 | |
| 117 | expectURL, err := url.Parse(expectedPath) |
| 118 | if err != nil { |
| 119 | t.Errorf("Couldn't parse %v as a URL.", expectedPath) |
| 120 | } |
| 121 | if f.RequestReceived == nil { |
| 122 | t.Errorf("Unexpected nil request received for %s", expectedPath) |
| 123 | return |
| 124 | } |
| 125 | if f.RequestReceived.URL.Path != expectURL.Path { |
| 126 | t.Errorf("Unexpected request path for request %#v, received: %q, expected: %q", f.RequestReceived, f.RequestReceived.URL.Path, expectURL.Path) |
| 127 | } |
| 128 | if e, a := expectURL.Query(), f.RequestReceived.URL.Query(); !reflect.DeepEqual(e, a) { |
| 129 | t.Errorf("Unexpected query for request %#v, received: %q, expected: %q", f.RequestReceived, a, e) |
| 130 | } |
| 131 | if f.RequestReceived.Method != expectedMethod { |
| 132 | t.Errorf("Unexpected method: %q, expected: %q", f.RequestReceived.Method, expectedMethod) |
| 133 | } |
| 134 | if body != nil { |
| 135 | if *body != f.RequestBody { |
| 136 | t.Errorf("Received body:\n%s\n Doesn't match expected body:\n%s", f.RequestBody, *body) |
| 137 | } |
| 138 | } |
| 139 | } |