-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathaccess_log_apache_test.go
78 lines (61 loc) · 1.92 KB
/
access_log_apache_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
package rest
import (
"bytes"
"github.com/ant0ine/go-json-rest/rest/test"
"log"
"regexp"
"testing"
)
func TestAccessLogApacheMiddleware(t *testing.T) {
api := NewApi()
// the middlewares stack
buffer := bytes.NewBufferString("")
api.Use(&AccessLogApacheMiddleware{
Logger: log.New(buffer, "", 0),
Format: CommonLogFormat,
textTemplate: nil,
})
api.Use(&TimerMiddleware{})
api.Use(&RecorderMiddleware{})
// a simple app
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) {
w.WriteJson(map[string]string{"Id": "123"})
}))
// wrap all
handler := api.MakeHandler()
req := test.MakeSimpleRequest("GET", "http://localhost/", nil)
req.RemoteAddr = "127.0.0.1:1234"
recorded := test.RunRequest(t, handler, req)
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
// log tests, eg: '127.0.0.1 - - 29/Nov/2014:22:28:34 +0000 "GET / HTTP/1.1" 200 12'
apacheCommon := regexp.MustCompile(`127.0.0.1 - - \d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [+\-]\d{4}\ "GET / HTTP/1.1" 200 12`)
if !apacheCommon.Match(buffer.Bytes()) {
t.Errorf("Got: %s", buffer.String())
}
}
func TestAccessLogApacheMiddlewareMissingData(t *testing.T) {
api := NewApi()
// the uncomplete middlewares stack
buffer := bytes.NewBufferString("")
api.Use(&AccessLogApacheMiddleware{
Logger: log.New(buffer, "", 0),
Format: CommonLogFormat,
textTemplate: nil,
})
// a simple app
api.SetApp(AppSimple(func(w ResponseWriter, r *Request) {
w.WriteJson(map[string]string{"Id": "123"})
}))
// wrap all
handler := api.MakeHandler()
req := test.MakeSimpleRequest("GET", "http://localhost/", nil)
recorded := test.RunRequest(t, handler, req)
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
// not much to log when the Env data is missing, but this should still work
apacheCommon := regexp.MustCompile(` - - "GET / HTTP/1.1" 0 -`)
if !apacheCommon.Match(buffer.Bytes()) {
t.Errorf("Got: %s", buffer.String())
}
}