-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
82 lines (71 loc) · 1.74 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
80
81
82
package domain_test
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/skuid/domain"
"github.com/skuid/domain/util"
)
func TestHttpMethods(t *testing.T) {
util.SkipIntegrationTest(t)
const YES_NO_API = "yesno.wtf/api"
type YesNoResponse struct {
Answer string `json:"answer"`
Forced bool `json:"forced"`
}
for _, tc := range []struct {
description string
givenHost string
givenHeaders map[string]string // should ignore nil map
expectedErrorMsg string
}{
{
description: "https",
givenHost: fmt.Sprintf("https://%v", YES_NO_API),
},
{
description: "empty header map",
givenHost: fmt.Sprintf("https://%v", YES_NO_API),
givenHeaders: make(map[string]string),
},
{
description: "no https, should add",
givenHost: YES_NO_API,
},
{
description: "http should replace",
givenHost: fmt.Sprintf("http://%v", YES_NO_API),
},
{
description: "301 redirect to 404 page",
givenHost: "https://skuid.com/google",
expectedErrorMsg: "301",
},
{
description: "bad unmarshal",
givenHost: "https://www.uuidtools.com/api/generate/v1",
expectedErrorMsg: "json: cannot unmarshal",
},
} {
t.Run(tc.description, func(subtest *testing.T) {
actual, actualError := domain.JsonBodyRequest[YesNoResponse](
tc.givenHost,
http.MethodGet,
[]byte{},
tc.givenHeaders,
)
if actualError != nil && tc.expectedErrorMsg == "" {
subtest.Log(actualError)
subtest.FailNow()
}
if tc.expectedErrorMsg == "" && actual.Answer == "" {
subtest.Log(actualError)
subtest.FailNow()
}
if tc.expectedErrorMsg != "" {
assert.Contains(subtest, actualError.Error(), tc.expectedErrorMsg)
}
})
}
}