-
Notifications
You must be signed in to change notification settings - Fork 3
/
haproxy_handlers_test.go
69 lines (57 loc) · 2.15 KB
/
haproxy_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
package main
import (
"errors"
"fmt"
"net/http/httptest"
"testing"
)
// ----------------------------------------------
// GetHAProxyConfig TESTS
// ----------------------------------------------
// Tests the "happy path" for the GetHAProxyConfig() handler.
func Test_GetHAProxyConfig(t *testing.T) {
// setup objects and mocks
configStr := "test config"
w := httptest.NewRecorder()
enc := JSONEncoder{}
h := testHelpers.NewHAProxyMock()
h.config = configStr
// execute function to test
GetHAProxyConfig(w, enc, h)
actCT := w.Header().Get("Content-Type")
// assert return values
assert.Equal(t, actCT, "text/plain", "GetHAProxyConfig() response has unexpected content type")
assert.Equal(t, w.Code, 200, "GetHAProxyConfig() returned unexpected status code")
assert.Equal(t, w.Body.String(), configStr, "GetHAProxyConfig() returned unexpected body")
}
func Test_GetHAProxyConfig_ErrorReadingConfig(t *testing.T) {
// setup objects and mocks
w := httptest.NewRecorder()
enc := JSONEncoder{}
h := testHelpers.NewHAProxyMock()
h.getConfigAction = func() (string, error) { return "", errors.New("error") }
// execute function to test
GetHAProxyConfig(w, enc, h)
// assert return values
expCode := 500
expBody := fmt.Sprintf(`"code":%d`, expCode)
assert.Equal(t, w.Code, expCode, "GetHAProxyConfig() returned unexpected status code")
assert.StringContains(t, w.Body.String(), expBody, "GetHAProxyConfig() returned unexpected body")
}
// ----------------------------------------------
// ReloadHAProxy TESTS
// ----------------------------------------------
// Tests the "happy path" for the RestartHAProxy() handler.
func Test_ReloadHAProxy(t *testing.T) {
// setup objects and mocks
w := httptest.NewRecorder()
enc := JSONEncoder{}
h := testHelpers.NewHAProxyMock()
// execute function to test
ReloadHAProxy(w, enc, h)
actCT := w.Header().Get("Content-Type")
// assert return values
assert.Equal(t, actCT, "text/plain", "RestartHAProxy() response has unexpected content type")
assert.Equal(t, w.Code, 200, "RestartHAProxy() returned unexpected status code")
assert.StringContains(t, w.Body.String(), "success", "RestartHAProxy() returned unexpected body")
}