generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roundtripper_test.go
53 lines (45 loc) · 1.14 KB
/
roundtripper_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
48
49
50
51
52
53
package roundtripper_test
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"moul.io/roundtripper"
)
func Example() {
client := http.Client{
Transport: &roundtripper.Transport{
ExtraHeader: http.Header{
"Authorization": []string{"Bearer LoremIpsumDolorSitAmet..."},
},
},
}
client.Get("...")
}
func TestTransport(t *testing.T) {
client := http.Client{
Transport: &roundtripper.Transport{
ExtraHeader: http.Header{
"Authorization": []string{"Bearer LoremIpsumDolorSitAmet..."},
},
},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expected := http.Header{
"Authorization": []string{"Bearer LoremIpsumDolorSitAmet..."},
"Accept-Encoding": []string{"gzip"},
"User-Agent": []string{"Go-http-client/1.1"},
}
require.Equal(t, r.Header, expected)
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
res, err := client.Get(ts.URL)
require.NoError(t, err)
greeting, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
res.Body.Close()
require.Equal(t, greeting, []byte("Hello, client\n"))
}