-
Notifications
You must be signed in to change notification settings - Fork 4
/
handlers_test.go
155 lines (140 loc) · 5.16 KB
/
handlers_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
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
155
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
gomock "github.com/golang/mock/gomock"
"github.com/mozilla-services/autograph-edge/mock_main"
)
func Test_heartbeatHandler(t *testing.T) {
type args struct {
baseURL string
r *http.Request
}
type expectedResponse struct {
status int
body []byte
contentType string
}
tests := []struct {
name string
args args
upstreamResponse *http.Response
upstreamErr error
expectedResponse expectedResponse
}{
{
name: "edge heartbeat OK when autograph app returns 200",
args: args{
baseURL: conf.BaseURL,
r: httptest.NewRequest("GET", "http://localhost:8080/__heartbeat__", nil),
},
upstreamResponse: &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("{}"))),
},
upstreamErr: nil,
expectedResponse: expectedResponse{
status: http.StatusOK,
contentType: "application/json",
body: []byte("{\"status\":true,\"checks\":{\"check_autograph_heartbeat\":true},\"details\":\"\"}"),
},
},
{
name: "edge heartbeat 503 when autograph app returns 502",
args: args{
baseURL: conf.BaseURL,
r: httptest.NewRequest("GET", "http://localhost:8080/__heartbeat__", nil),
},
upstreamResponse: &http.Response{
Status: http.StatusText(http.StatusBadGateway),
StatusCode: http.StatusBadGateway,
Body: io.NopCloser(bytes.NewReader([]byte("{}"))),
},
upstreamErr: nil,
expectedResponse: expectedResponse{
status: http.StatusServiceUnavailable,
contentType: "application/json",
body: []byte("{\"status\":false,\"checks\":{\"check_autograph_heartbeat\":false},\"details\":\"upstream autograph returned heartbeat code 502 Bad Gateway\"}"),
},
},
{
name: "edge heartbeat 503 when autograph app is down",
args: args{
baseURL: conf.BaseURL,
r: httptest.NewRequest("GET", "http://localhost:8080/__heartbeat__", nil),
},
upstreamResponse: &http.Response{},
upstreamErr: fmt.Errorf("Get \"http://localhost:8000/__heartbeat__\": dial tcp 127.0.0.1:8000: connect: connection refused <nil>"),
expectedResponse: expectedResponse{
status: http.StatusServiceUnavailable,
contentType: "application/json",
body: []byte("{\"status\":false,\"checks\":{\"check_autograph_heartbeat\":false},\"details\":\"failed to request autograph heartbeat from http://localhost:8000/__heartbeat__: Get \\\"http://localhost:8000/__heartbeat__\\\": dial tcp 127.0.0.1:8000: connect: connection refused \\u003cnil\\u003e\"}"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var client heartbeatRequester
if os.Getenv("MOCK_AUTOGRAPH_CALLS") == string("1") {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
clientMock := mock_main.NewMockheartbeatRequester(ctrl)
clientMock.EXPECT().Get(tt.args.baseURL+"__heartbeat__").Return(tt.upstreamResponse, tt.upstreamErr)
client = clientMock
} else {
client = &heartbeatClient{&http.Client{}}
}
w := httptest.NewRecorder()
heartbeatHandler(tt.args.baseURL, client)(w, tt.args.r)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != tt.expectedResponse.status {
t.Fatalf("heartbeatHandler() returned unexpected status %v expected %v", resp.StatusCode, tt.expectedResponse.status)
}
if !bytes.Equal(body, tt.expectedResponse.body) {
t.Fatalf("heartbeatHandler() returned body:\n%s\nand expected:\n%s", body, tt.expectedResponse.body)
}
if resp.Header.Get("Content-Type") != tt.expectedResponse.contentType {
t.Fatalf("heartbeatHandler() returned unexpected content type: %s, expected %s", resp.Header.Get("Content-Type"), tt.expectedResponse.contentType)
}
})
}
}
func TestVersion(t *testing.T) {
req := httptest.NewRequest("GET", "http://localhost:8080/__version__", nil)
w := httptest.NewRecorder()
versionHandler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Fatalf("returned unexpected status %v expected %v", resp.StatusCode, http.StatusOK)
}
if !bytes.Equal(body, jsonVersion) {
t.Fatalf("failed to return version.json contents got %s and expected %s", body, jsonVersion)
}
if resp.Header.Get("Content-Type") != "application/json" {
t.Fatalf("version returned unexpected content type: %s", resp.Header.Get("Content-Type"))
}
}
func TestNotFoundHandler(t *testing.T) {
req := httptest.NewRequest("GET", "http://localhost:8080/", nil)
w := httptest.NewRecorder()
notFoundHandler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("returned unexpected status %v expected %v", resp.StatusCode, http.StatusOK)
}
if !bytes.Equal(body, []byte("404 page not found\n")) {
t.Fatalf("failed to return 404 contents got %q and expected %q", body, "404 page not found\n")
}
if resp.Header.Get("Content-Type") != "text/plain; charset=utf-8" {
t.Fatalf("notFoundHandler returned unexpected content type: %q", resp.Header.Get("Content-Type"))
}
}