forked from jomei/notionapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
39 lines (33 loc) · 887 Bytes
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package notionapi_test
import (
"net/http"
"os"
"testing"
)
// RoundTripFunc .
type RoundTripFunc func(req *http.Request) *http.Response
// RoundTrip .
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
// newTestClient returns *http.Client with Transport replaced to avoid making real calls
func newTestClient(fn RoundTripFunc) *http.Client {
return &http.Client{
Transport: fn,
}
}
// newMockedClient returns *http.Client which responds with content from given file
func newMockedClient(t *testing.T, requestMockFile string, statusCode int) *http.Client {
return newTestClient(func(req *http.Request) *http.Response {
b, err := os.Open(requestMockFile)
if err != nil {
t.Fatal(err)
}
resp := &http.Response{
StatusCode: statusCode,
Body: b,
Header: make(http.Header),
}
return resp
})
}