-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
111 lines (98 loc) · 2.86 KB
/
request_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
110
111
package OpenTracing
import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"
)
func TestRequest(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write(nil)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}))
defer ts.Close()
t.Run("invalid method", func(t *testing.T) {
resp, code, err := Request(context.Background(), RequestOpts{})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, 0, code)
})
t.Run("no token", func(t *testing.T) {
resp, code, err := Request(context.Background(), RequestOpts{
InjectBearer: true,
})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, 0, code)
})
t.Run("no span", func(t *testing.T) {
ctx := context.WithValue(context.Background(), "token", "test")
resp, code, err := Request(ctx, RequestOpts{
InjectBearer: true,
})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, 0, code)
})
t.Run("wrong url", func(t *testing.T) {
ctx := context.WithValue(context.Background(), "token", "test")
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
require.NotNil(t, span)
resp, code, err := Request(ctx, RequestOpts{
InjectBearer: true,
})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, 0, code)
})
t.Run("non 1xx, 2xx, 3xx response", func(t *testing.T) {
ctx := context.WithValue(context.Background(), "token", "test")
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
require.NotNil(t, span)
resp, code, err := Request(ctx, RequestOpts{
Method: "GET",
URL: ts.URL,
InjectBearer: true,
})
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, http.StatusBadRequest, code)
})
t.Run("200", func(t *testing.T) {
ctx := context.WithValue(context.Background(), "token", "test")
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
require.NotNil(t, span)
resp, code, err := Request(ctx, RequestOpts{
Method: "POST",
URL: ts.URL,
InjectBearer: true,
})
require.NoError(t, err)
require.Equal(t, []byte("OK"), resp)
require.Equal(t, http.StatusOK, code)
})
}
func TestSetAuthorization(t *testing.T) {
t.Run("empty context", func(t *testing.T) {
ctx := context.Background()
require.Error(t, SetAuthorization(ctx, nil))
})
t.Run("empty request", func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, "token", "test")
require.Error(t, SetAuthorization(ctx, nil))
})
t.Run("ok", func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, "token", "test")
req := httptest.NewRequest("GET", "http://test.com", nil)
require.NoError(t, SetAuthorization(ctx, req))
})
}