Skip to content

Commit

Permalink
adds custom http req matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoescher committed Nov 14, 2023
1 parent e347223 commit 3a540da
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCall(t *testing.T) {
defer ctrl.Finish()
mockHttpClient := NewMockHttpClient(ctrl)
expected := &http.Response{Status: "200 OK"}
mockHttpClient.EXPECT().Do(gomock.Any()).Return(expected, nil)
mockHttpClient.EXPECT().Do(RequestMatcher{req}).Return(expected, nil)

// calls client
c := &Client{
Expand All @@ -52,3 +52,38 @@ func TestCall(t *testing.T) {
require.Equal(t, expected, response)
})
}

type RequestMatcher struct {
req *http.Request
}

func (m RequestMatcher) Matches(x interface{}) bool {
// check x type
parsed, ok := x.(*http.Request)
if !ok {
return false
}
if m.req.Method != parsed.Method {
return false
}
if m.req.URL.String() != parsed.URL.String() {
return false
}
if m.req.Header.Get("Authorization") != parsed.Header.Get("Authorization") {
return false
}
if m.req.Header.Get("Content-Type") != parsed.Header.Get("Content-Type") {
return false
}
if m.req.Header.Get("OpenAI-Organization") != parsed.Header.Get("OpenAI-Organization") {
return false
}
if m.req.Body != parsed.Body {
return false
}
return true
}

func (m RequestMatcher) String() string {
return "is an http request with the same method, url, required headers and body"
}

0 comments on commit 3a540da

Please sign in to comment.