forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_test.go
79 lines (76 loc) · 1.93 KB
/
http_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
package integration
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/infobloxopen/atlas-app-toolkit/auth"
)
func TestMakeStandardRequest(t *testing.T) {
client := http.Client{}
var tests = []struct {
name string
method string
payload interface{}
handler http.Handler
err error
}{
{
name: "check JWT presence in GET request",
method: http.MethodGet,
payload: nil,
handler: http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get(auth.AuthorizationHeader)
if expected := fmt.Sprintf(
"%s %s", auth.DefaultTokenType, standardToken,
); expected != authHeader {
t.Error("token missing in standard request")
}
},
),
err: nil,
},
{
name: "check JSON payload in POST request",
method: http.MethodPost,
payload: map[string]string{"test-key": "test-value"},
handler: http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
body := make(map[string]string)
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Fatalf("unable to decode json payload: %v", err)
}
if value, ok := body["test-key"]; !ok || value != "test-value" {
t.Errorf("unexpected json payload in request body: %v", body)
}
},
),
err: nil,
},
{
name: "check invalid JSON payload with unsupported type",
method: "not-an-http-verb",
payload: nil,
handler: http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {},
),
err: errors.New("net/http: invalid method"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testServer := httptest.NewServer(test.handler)
defer testServer.Close()
req, err := MakeStandardRequest(
test.method, testServer.URL, test.payload,
)
if err != nil && test.err == nil {
t.Errorf("unexpected error: %v", err)
}
client.Do(req)
})
}
}