-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
144 lines (125 loc) · 3.6 KB
/
server_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
// server_test.go
package main
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"fortio.org/assert"
"github.com/prometheus/client_golang/prometheus"
)
func TestHandleRequest(t *testing.T) {
tests := []struct {
name string
method string
body string
wantStatus int
wantBody SlackResponse
}{
{
name: "valid post request",
method: http.MethodPost,
body: `{"channel": "test_channel", "text": "Hello"}`,
wantStatus: http.StatusOK,
wantBody: SlackResponse{Ok: true},
},
{
name: "invalid method",
method: http.MethodGet,
body: ``,
wantStatus: http.StatusMethodNotAllowed,
},
{
name: "invalid post request",
method: http.MethodPost,
body: `{"foo": "bar"}`,
wantStatus: http.StatusBadRequest,
wantBody: SlackResponse{Ok: false, Error: "Channel is not set and Neither attachments, blocks, nor text is set"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := prometheus.NewRegistry()
metrics := NewMetrics(r)
app := &App{
slackQueue: make(chan SlackPostMessageRequest, 10),
metrics: metrics,
}
req, err := http.NewRequest(tt.method, "/", bytes.NewBufferString(tt.body))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
app.handleRequest(rr, req)
assert.Equal(t, tt.wantStatus, rr.Code)
if tt.wantBody != (SlackResponse{}) {
var response SlackResponse
err := json.NewDecoder(rr.Body).Decode(&response)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tt.wantBody, response)
}
})
}
}
func TestStartServer(t *testing.T) {
r := prometheus.NewRegistry()
metrics := NewMetrics(r)
app := &App{
slackQueue: make(chan SlackPostMessageRequest, 10),
metrics: metrics,
}
testPort := ":9090"
ctx, cancel := context.WithCancel(context.Background())
// T.Fatalf, which must be called in the same goroutine as the test (SA2002)
// Sue me, I don't know how to fix this nicer than this...
errCh := make(chan error)
go func() {
err := app.StartServer(ctx, testPort)
if err != nil {
errCh <- err // Send the error to the channel
}
close(errCh)
}()
// Give server some time to start
// If you are running on a non-priviledged account, and get a popup asking for permission to accept
// incoming connections, you can increase this time...
time.Sleep(1 * time.Second)
// Make a sample request to ensure server is running
resp, err := http.Post("http://localhost"+testPort, "application/json",
bytes.NewBufferString(`{"channel": "test_channel", "text": "Hello"}`))
if err != nil {
t.Fatalf("Could not make GET request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status OK, got: %v", resp.StatusCode)
}
// Cancel the context, which should stop the server
cancel()
// Give server some time to shut down
time.Sleep(1 * time.Second)
// Make another request, this should fail since the server should be stopped
secondResp, err := http.Post("http://localhost"+testPort, "application/json",
bytes.NewBufferString(`{"channel": "test_channel", "text": "Hello"}`))
if err == nil {
t.Fatal("Expected error making POST request after server shut down, got none")
}
// to avoid confusion; we _are_ expecting a err, but for the edge-case it doesn't (resp != nil), we
// should close the body.
if secondResp != nil {
defer secondResp.Body.Close()
}
select {
case err := <-errCh:
if err != nil {
t.Fatalf("Error starting server: %v", err)
}
default:
// No error received from the channel
}
}