-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes_internal_test.go
95 lines (75 loc) · 2.71 KB
/
routes_internal_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
package calc
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/antklim/go-calc/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestNotFoundHandler(t *testing.T) {
req := httptest.NewRequest("GET", "/not-found", nil)
rr := httptest.NewRecorder()
notFoundHandler(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.JSONEq(t, `{"message": "not found"}`, string(resBody))
}
func TestDoHandler(t *testing.T) {
req := httptest.NewRequest("POST", "/do", strings.NewReader(`{"operation": "add", "arguments": [1,2]}`))
rr := httptest.NewRecorder()
doHandler()(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.JSONEq(t, `{"operation": "add", "arguments": [1,2], "result": 3}`, string(resBody))
}
func TestRemoteHandlerWithClientMock(t *testing.T) {
mockRes := &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{"foo": "bar"}`)),
}
clientMock := mocks.HTTPClient{}
clientMock.On("Do", mock.AnythingOfType("*http.Request")).Return(mockRes, nil)
req := httptest.NewRequest("GET", "/remote", nil)
rr := httptest.NewRecorder()
remoteHandler(&clientMock)(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.JSONEq(t, `{"foo": "bar"}`, string(resBody))
}
func TestRemoteHandlerWithStubServer(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer ts.Close()
url := fmt.Sprintf("/remote?url=%s", ts.URL)
req := httptest.NewRequest("GET", url, nil)
rr := httptest.NewRecorder()
remoteHandler(&http.Client{})(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusInternalServerError, res.StatusCode)
assert.JSONEq(t, `{"message": "remote call failed, returned status code 503"}`, string(resBody))
}
func TestRemoteHandlerWithDelayResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer ts.Close()
url := fmt.Sprintf("/remote?url=%s", ts.URL)
req := httptest.NewRequest("GET", url, nil)
rr := httptest.NewRecorder()
remoteHandler(&http.Client{Timeout: 100 * time.Millisecond})(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusInternalServerError, res.StatusCode)
assert.Contains(t, string(resBody), "context deadline exceeded")
}