-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackagefunctions_test.go
109 lines (81 loc) · 2.37 KB
/
packagefunctions_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package httpsling
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRequest(t *testing.T) {
req, err := Request(Get("http://blue.com/red"))
require.NoError(t, err)
require.NotNil(t, req)
require.Equal(t, "http://blue.com/red", req.URL.String())
}
type testContextKey string
const colorContextKey = testContextKey("color")
func TestRequestContext(t *testing.T) {
req, err := RequestWithContext(
context.WithValue(context.Background(), colorContextKey, "green"),
Get("http://blue.com/red"),
)
require.NoError(t, err)
require.NotNil(t, req)
assert.Equal(t, "http://blue.com/red", req.URL.String())
assert.Equal(t, "green", req.Context().Value(colorContextKey))
}
func TestSend(t *testing.T) {
i := Inspector{}
resp, err := Send(Get("/red"), WithDoer(MockDoer(204)), &i)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 204, resp.StatusCode)
assert.Equal(t, "/red", i.Request.URL.Path)
}
func TestSendContext(t *testing.T) {
i := Inspector{}
resp, err := SendWithContext(
context.WithValue(context.Background(), colorContextKey, "blue"),
Get("/profile"),
WithDoer(MockDoer(204)),
&i,
)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 204, resp.StatusCode)
assert.Equal(t, "blue", i.Request.Context().Value(colorContextKey))
assert.Equal(t, "/profile", i.Request.URL.Path)
}
func TestReceive(t *testing.T) {
i := Inspector{}
doer := MockDoer(205, Body(`{"count":25}`), JSON(false))
var m testModel
resp, err := Receive(&m, Get("/red"), WithDoer(doer), &i)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 205, resp.StatusCode)
assert.Equal(t, "/red", i.Request.URL.Path)
assert.Equal(t, 25, m.Count)
t.Run("Context", func(t *testing.T) {
var m testModel
i := Inspector{}
resp, err := ReceiveWithContext(
context.WithValue(context.Background(), colorContextKey, "yellow"),
&m,
Get("/red"),
WithDoer(doer),
&i,
)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, 205, resp.StatusCode)
assert.Equal(t, 25, m.Count)
assert.Equal(t, "yellow", i.Request.Context().Value(colorContextKey))
assert.Equal(t, "/red", i.Request.URL.Path)
})
}
func ExampleRequest() {
req, err := Request(Get("http://api.com/resource"))
fmt.Println(req.URL.String(), err)
// Output: http://api.com/resource <nil>
}