mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-03-25 11:29:22 +00:00
httpdriver: ExpectMockRequest should return error instead
This commit is contained in:
parent
ea4beab6bd
commit
b3b2478481
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -98,44 +99,41 @@ func (r *MockRequest) WithBody(body io.ReadCloser) {
|
||||||
|
|
||||||
// ExpectMockRequest asserts that the given request is a mock request that
|
// ExpectMockRequest asserts that the given request is a mock request that
|
||||||
// matches what is expected. The given request for got must be of type
|
// matches what is expected. The given request for got must be of type
|
||||||
// *MockRequest. The given t function can either be (*testing.T).Errorf or
|
// *MockRequest.
|
||||||
// (*testing.T).Fatalf.
|
func ExpectMockRequest(expected *MockRequest, gotAny Request) error {
|
||||||
func ExpectMockRequest(t func(f string, args ...interface{}), expected *MockRequest, gotAny Request) {
|
|
||||||
got, ok := gotAny.(*MockRequest)
|
got, ok := gotAny.(*MockRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
t("got unexpected request type %T", gotAny)
|
return fmt.Errorf("got unexpected request type %T", gotAny)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if expected.Method != got.Method {
|
if expected.Method != got.Method {
|
||||||
t("unexpected method %q, got %q", expected.Method, got.Method)
|
return fmt.Errorf("unexpected method %q, got %q", expected.Method, got.Method)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if expected.URL.String() != got.URL.String() {
|
if expected.URL.String() != got.URL.String() {
|
||||||
t("unexpected URL %q, got %q", expected.URL.String(), got.URL.String())
|
return fmt.Errorf("unexpected URL %q, got %q", expected.URL.String(), got.URL.String())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for expectK, expectV := range expected.Header {
|
for expectK, expectV := range expected.Header {
|
||||||
gotV, ok := got.Header[expectK]
|
gotV, ok := got.Header[expectK]
|
||||||
if !ok {
|
if !ok {
|
||||||
t("unexpected header key %q, got none", expectK)
|
return fmt.Errorf("unexpected header key %q, got none", expectK)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(expectV, gotV) {
|
if !reflect.DeepEqual(expectV, gotV) {
|
||||||
t("unexpected header key %q to have value %q, got %q", expectK, expectV, gotV)
|
return fmt.Errorf("unexpected header key %q to have value %q, got %q", expectK, expectV, gotV)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(expected.Body, got.Body) {
|
body1 := bytes.TrimRight(expected.Body, "\n")
|
||||||
t("unexpected body:\n"+
|
body2 := bytes.TrimRight(got.Body, "\n")
|
||||||
|
if !bytes.Equal(body1, body2) {
|
||||||
|
return fmt.Errorf("unexpected body:\n"+
|
||||||
"expected %q\n"+
|
"expected %q\n"+
|
||||||
"got %q", expected.Body, got.Body)
|
"got %q", expected.Body, got.Body)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MockResponse is a mock response. It implements the Response interface.
|
// MockResponse is a mock response. It implements the Response interface.
|
||||||
|
|
Loading…
Reference in a new issue