/
requester_test.go
111 lines (90 loc) · 3.1 KB
/
requester_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 go_requester
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// TestResult is the structure used in table test cases
type TestResult struct {
statusCode int
body string
}
// ClientResponse is response which will be returned by the fake server
type ClientResponse struct {
Message string `json:"message"`
}
func TestMake(t *testing.T) {
// Json response
fakeServer1 := newTestServer(200, `{"message":"ok"}`)
defer fakeServer1.Close()
requester := New(http.DefaultClient)
requester.SerUserAgent("test")
var clientJSONResponse ClientResponse
response, err := requester.Make("GET", fakeServer1.URL, map[string]string{}, &clientJSONResponse)
expectedResponse := ClientResponse{Message: "ok"}
assert.Nil(t, err)
assert.Equal(t, []byte(`{"message":"ok"}`), response.Body)
assert.Equal(t, 200, response.Status)
assert.Equal(t, expectedResponse, clientJSONResponse)
// String response
fakeServer2 := newTestServer(200, `ok`)
defer fakeServer2.Close()
requester = New(http.DefaultClient)
requester.SerUserAgent("test")
var clientStringResponse string
response, err = requester.Make("GET", fakeServer2.URL, map[string]string{}, &clientStringResponse)
expectedStringResponse := "ok"
assert.Nil(t, err)
assert.Equal(t, []byte(expectedStringResponse), response.Body)
assert.Equal(t, expectedStringResponse, clientStringResponse)
}
func TestMakeError(t *testing.T) {
testCases := []struct {
testName string
fakeServerResponse TestResult
method string
expectedStatusCode int
expectedErr error
expectedRawResponse []byte
expectedResponse interface{}
}{
{
testName: "Error Case - Invalid Json",
fakeServerResponse: TestResult{statusCode: 500, body: `{{dsadsa}}`},
method: "GET",
expectedErr: errors.New("invalid character '{' looking for beginning of object key string"),
expectedRawResponse: []byte(`{{dsadsa}}`),
expectedResponse: ClientResponse{},
},
{
testName: "Error Case - Server response code is incorrect",
fakeServerResponse: TestResult{statusCode: 500, body: `{"error":"test"}`},
method: "GET",
expectedErr: nil,
expectedRawResponse: []byte(`{"error":"test"}`),
expectedResponse: ClientResponse{},
},
}
for _, test := range testCases {
fakeServer := newTestServer(test.fakeServerResponse.statusCode, test.fakeServerResponse.body)
defer fakeServer.Close()
requester := New(http.DefaultClient)
requester.SerUserAgent("test")
var clientResponse ClientResponse
response, err := requester.Make("GET", fakeServer.URL, map[string]string{}, &clientResponse)
assert.Equal(t, test.expectedErr, err)
assert.Equal(t, test.expectedRawResponse, response.Body)
assert.Equal(t, test.expectedResponse, clientResponse)
}
}
func newTestServer(code int, body string) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, body)
}))
return server
}