-
Notifications
You must be signed in to change notification settings - Fork 33
/
main_test.go
114 lines (93 loc) · 2.92 KB
/
main_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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/kiskolabs/heroku-cloudwatch-drain/logparser"
"github.com/stretchr/testify/assert"
)
var l = new(LastMessageLogger)
var parseFunc = func(b []byte) (*logparser.LogEntry, error) {
return &logparser.LogEntry{Time: time.Now(), Message: ""}, nil
}
var app = &App{
loggers: map[string]logger{"app": l},
parse: parseFunc,
}
var server = httptest.NewServer(app)
func TestRequestNotFoundWithGet(t *testing.T) {
r, err := http.Get(server.URL + "/app")
assert.NoError(t, err)
assert.Equal(t, http.StatusNotFound, r.StatusCode)
}
func TestRequestPathMustBeAppName(t *testing.T) {
r, err := http.Post(server.URL+"/", "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
}
func TestBasicAuth(t *testing.T) {
app.user = "me"
app.pass = "SECRET"
defer func() {
app.user = ""
app.pass = ""
}()
r, err := http.Post(server.URL+"/app", "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusForbidden, r.StatusCode)
uri, _ := url.Parse(server.URL)
uri.User = url.UserPassword("me", "SECRET")
r, err = http.Post(uri.String()+"/app", "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusAccepted, r.StatusCode)
}
func TestNoBasicAuth(t *testing.T) {
r, err := http.Post(server.URL+"/app", "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusAccepted, r.StatusCode)
}
func TestSingleLogEntry(t *testing.T) {
app.parse = logparser.Parse
defer func() {
app.parse = parseFunc
}()
body := bytes.NewBuffer([]byte(`89 <45>1 2016-10-15T08:59:08.723822+00:00 host heroku web.1 - State changed from up to down`))
r, err := http.Post(server.URL+"/app", "", body)
assert.NoError(t, err)
assert.Equal(t, http.StatusAccepted, r.StatusCode)
assert.Equal(t, "heroku[web.1]: State changed from up to down", l.m)
}
func TestLogEntryWithEmptyLineAtTheEnd(t *testing.T) {
app.parse = logparser.Parse
defer func() {
app.parse = parseFunc
}()
body := bytes.NewBuffer([]byte("89 <45>1 2016-10-15T08:59:08.723822+00:00 host heroku web.1 - State changed from up to down\n"))
r, err := http.Post(server.URL+"/app", "", body)
assert.NoError(t, err)
assert.Equal(t, http.StatusAccepted, r.StatusCode)
assert.Equal(t, "heroku[web.1]: State changed from up to down", l.m)
}
func TestAnsiCodeStripping(t *testing.T) {
app.parse = logparser.Parse
app.stripAnsiCodes = true
defer func() {
app.parse = parseFunc
app.stripAnsiCodes = false
}()
body := bytes.NewBuffer([]byte(`89 <45>1 2016-10-15T08:59:08.723822+00:00 host heroku web.1 - [1m[36m(0.1ms)[0m [1mBEGIN[0m`))
r, err := http.Post(server.URL+"/app", "", body)
assert.NoError(t, err)
assert.Equal(t, http.StatusAccepted, r.StatusCode)
assert.Equal(t, "heroku[web.1]: (0.1ms) BEGIN", l.m)
}
type LastMessageLogger struct {
m string
}
func (l *LastMessageLogger) Log(t time.Time, s string) {
l.m = s
}
func (l *LastMessageLogger) Close() {}