-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods_test.go
98 lines (86 loc) · 2.96 KB
/
methods_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
package httpp
import (
"io"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestGet(t *testing.T) {
t.Run("no args other than mock", func(t *testing.T) {
client := &MockHttpClient{}
_, err := Get("https://catfact.ninja/fact", WithClient(client))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
have := client.CallHistory
want := []string{"GET https://catfact.ninja/fact"}
if diff := cmp.Diff(want, have); diff != "" {
t.Fatalf("mock http request history mismatch (-want +have):\n %s", diff)
}
})
}
func TestPost(t *testing.T) {
t.Run("with json body", func(t *testing.T) {
client := &MockHttpClient{}
_, err := Post("https://catfact.ninja/fact",
HttpBody{ContentType: "application/json",
Body: io.NopCloser(strings.NewReader(`{"fact":"70% of your cat's life is spent asleep.","length":39}`))},
WithClient(client))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
have := client.CallHistory
want := []string{`POST https://catfact.ninja/fact {"fact":"70% of your cat's life is spent asleep.","length":39}`}
if diff := cmp.Diff(want, have); diff != "" {
t.Fatalf("mock http request history mismatch (-want +have):\n %s", diff)
}
})
}
func TestPut(t *testing.T) {
t.Run("with json body", func(t *testing.T) {
client := &MockHttpClient{}
_, err := Put("https://catfact.ninja/fact",
HttpBody{ContentType: "application/json",
Body: io.NopCloser(strings.NewReader(`{"fact":"70% of your cat's life is spent asleep.","length":39}`))},
WithClient(client))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
have := client.CallHistory
want := []string{`PUT https://catfact.ninja/fact {"fact":"70% of your cat's life is spent asleep.","length":39}`}
if diff := cmp.Diff(want, have); diff != "" {
t.Fatalf("mock http request history mismatch (-want +have):\n %s", diff)
}
})
}
func TestPatch(t *testing.T) {
t.Run("with json body", func(t *testing.T) {
client := &MockHttpClient{}
_, err := Patch("https://catfact.ninja/fact",
HttpBody{ContentType: "application/json",
Body: io.NopCloser(strings.NewReader(`{"fact":"70% of your cat's life is spent asleep.","length":39}`))},
WithClient(client))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
have := client.CallHistory
want := []string{`PATCH https://catfact.ninja/fact {"fact":"70% of your cat's life is spent asleep.","length":39}`}
if diff := cmp.Diff(want, have); diff != "" {
t.Fatalf("mock http request history mismatch (-want +have):\n %s", diff)
}
})
}
func TestDelete(t *testing.T) {
t.Run("no args", func(t *testing.T) {
client := &MockHttpClient{}
_, err := Delete("https://catfact.ninja/fact", WithClient(client))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
have := client.CallHistory
want := []string{`DELETE https://catfact.ninja/fact`}
if diff := cmp.Diff(want, have); diff != "" {
t.Fatalf("mock http request history mismatch (-want +have):\n %s", diff)
}
})
}