forked from HereMobilityDevelopers/mediary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediary_test.go
47 lines (41 loc) · 1.28 KB
/
mediary_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
40
41
42
43
44
45
46
47
package mediary
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestCustomClient(t *testing.T) {
expected := &http.Client{}
actual := Init().WithPreconfiguredClient(expected).Build()
require.Equal(t, expected, actual, "other client")
}
func TestDefault(t *testing.T) {
client := Init().Build()
require.NotNil(t, client, "an empty client")
}
func TestWithInterceptor(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
name := req.Header.Get("name")
_, err := writer.Write([]byte("Hello " + name))
require.NoError(t, err)
}))
defer server.Close()
client := Init().WithPreconfiguredClient(server.Client()).AddInterceptors(testInterceptor).Build()
response, err := client.Get(server.URL)
require.NoError(t, err)
defer response.Body.Close()
bodyBytes, err := ioutil.ReadAll(response.Body)
require.NoError(t, err)
require.Contains(t, string(bodyBytes), "Hello Robert")
family := response.Header.Get("family")
require.Equal(t, "Pike", family)
}
func testInterceptor(req *http.Request, handler Handler) (resp *http.Response, err error) {
req.Header.Set("name", "Robert")
if resp, err = handler(req); err == nil {
resp.Header.Set("family", "Pike")
}
return
}