-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
85 lines (73 loc) · 2.22 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
package courier
import (
"net/http"
"testing"
"time"
"github.com/nyaruka/courier/utils"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestServer(t *testing.T) {
logger := logrus.New()
config := NewConfig()
config.StatusUsername = "admin"
config.StatusPassword = "password123"
server := NewServerWithLogger(config, NewMockBackend(), logger)
server.Start()
defer server.Stop()
// wait for server to come up
time.Sleep(100 * time.Millisecond)
// hit our main pages, this is admitedly mostly in the name of coverage
req, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
rr, err := utils.MakeHTTPRequest(req)
assert.NoError(t, err)
assert.Contains(t, string(rr.Body), "courier")
// status page without auth
req, _ = http.NewRequest("GET", "http://localhost:8080/status", nil)
rr, err = utils.MakeHTTPRequest(req)
assert.Error(t, err)
assert.Equal(t, 401, rr.StatusCode)
// status page with auth
req, _ = http.NewRequest("GET", "http://localhost:8080/status", nil)
req.SetBasicAuth("admin", "password123")
rr, err = utils.MakeHTTPRequest(req)
assert.NoError(t, err)
assert.Contains(t, string(rr.Body), "courier")
// hit an invalid path
req, _ = http.NewRequest("GET", "http://localhost:8080/notthere", nil)
rr, err = utils.MakeHTTPRequest(req)
assert.Error(t, err)
assert.Contains(t, string(rr.Body), "not found")
// invalid method
req, _ = http.NewRequest("POST", "http://localhost:8080/", nil)
rr, err = utils.MakeHTTPRequest(req)
assert.Error(t, err)
assert.Contains(t, string(rr.Body), "method not allowed")
}
func TestSanitizeBody(t *testing.T) {
tcs := []struct {
Label string
Body string
Result string
}{
{
"empty",
"",
"",
},
{
"valid",
"POST /v1/messages HTTP/1.1\r\nContent-Length: 125\r\n\r\nBody",
"POST /v1/messages HTTP/1.1\r\nContent-Length: 125\r\n\r\nBody",
},
{
"application/octet-stream",
"POST /v1/messages HTTP/1.1\r\nContent-Length: 125\r\n\r\nJFIF``C",
"POST /v1/messages HTTP/1.1\r\nContent-Length: 125\r\n\r\nOmitting non text body of type: application/octet-stream",
},
}
for _, tc := range tcs {
result := sanitizeBody(tc.Body)
assert.Equal(t, tc.Result, result, "%s: unexpected result", tc.Label)
}
}