This repository was archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_builder_test.go
More file actions
154 lines (121 loc) · 3.12 KB
/
request_builder_test.go
File metadata and controls
154 lines (121 loc) · 3.12 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package form3api_test
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"testing"
"github.com/screwyprof/form3api"
"github.com/screwyprof/form3api/assert"
)
func TestRequestBuilder(t *testing.T) {
t.Run("invalid request params given, error returned", func(t *testing.T) {
t.Parallel()
// arrange
params := make(chan struct{})
rb := form3api.NewRequest()
// act
err := rb.Exec(context.Background(), params, nil)
// assert
assert.NotNil(t, err)
})
t.Run("invalid request url given, error returned", func(t *testing.T) {
t.Parallel()
// arrange
rb := form3api.NewRequest().
WithBaseURL("unknown_scheme://_invalid__url")
// act
err := rb.Exec(context.Background(), nil, nil)
// assert
assert.NotNil(t, err)
})
t.Run("expected empty response body returned, no error returned", func(t *testing.T) {
t.Parallel()
// arrange
req := &testRequest{SomeValue: 42}
client := &httpClientMock{
TB: t,
ExpectedReqMethod: http.MethodGet,
ExpectedReqBody: req,
StatusCode: http.StatusNoContent,
}
rb := form3api.NewRequest().
WithClient(client).
WithBaseURL("/some_url").
WithMethod(http.MethodGet)
// act
err := rb.Exec(context.Background(), req, nil)
// assert
assert.Ok(t, err)
})
t.Run("API error occurred, error returned", func(t *testing.T) {
t.Parallel()
// arrange
want := &form3api.APIError{
Response: &http.Response{StatusCode: http.StatusInternalServerError},
Code: "SOME_ERROR",
Msg: "some error",
}
client := &httpClientMock{
TB: t,
ExpectedReqMethod: http.MethodGet,
StatusCode: http.StatusInternalServerError,
ResponseBody: want,
}
rb := form3api.NewRequest().
WithClient(client).
WithBaseURL("/some_url")
// act
err := rb.Exec(context.Background(), nil, nil)
// assert
assert.NotNil(t, err)
})
t.Run("body deserialization error occurred, error returned", func(t *testing.T) {
t.Parallel()
// arrange
client := &httpClientMock{}
client.HandlerFunc = func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte("invalid json body"))),
}
return resp, nil
}
rb := form3api.NewRequest().
WithClient(client).
WithBaseURL("/some_url")
// act
err := rb.Exec(context.Background(), nil, nil)
// assert
assert.NotNil(t, err)
})
t.Run("valid request, valid response", func(t *testing.T) {
t.Parallel()
// arrange
req := &testRequest{SomeValue: 42}
want := &testResponse{SomeValue: 777}
client := &httpClientMock{
TB: t,
ExpectedReqMethod: http.MethodPost,
ExpectedReqBody: req,
ResponseBody: want,
StatusCode: http.StatusCreated,
}
rb := form3api.NewRequest().
WithClient(client).
WithBaseURL("/some_url").
WithMethod(http.MethodPost)
// act
var got *testResponse
err := rb.Exec(context.Background(), req, &got)
// assert
assert.Ok(t, err)
assert.Equals(t, want, got)
})
}
type testRequest struct {
SomeValue int
}
type testResponse struct {
SomeValue int
}