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
|
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func testLogWithStatus(ww http.ResponseWriter, r *http.Request) {
w := newLoggingResponseWriter(ww)
defer w.Log(r)
w.WriteHeader(http.StatusOK)
fmt.Fprint(&w, "with-status")
}
func testLogWithoutStatus(ww http.ResponseWriter, r *http.Request) {
w := newLoggingResponseWriter(ww)
defer w.Log(r)
fmt.Fprint(&w, "no-status")
}
func testLogWithDoubleStatus(ww http.ResponseWriter, r *http.Request) {
w := newLoggingResponseWriter(ww)
defer w.Log(r)
w.WriteHeader(http.StatusOK)
http.Redirect(&w, r, "/test", 301)
}
func TestExtractLogFieldsHidesQueryStrings(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/foo?token=bar", nil)
r.Header.Set("Referer", "http://invalid.com/bar?token=baz")
l := newLoggingResponseWriter(w)
fields := l.extractLogFields(r)
assert.Equal(t, fields["uri"], "/foo")
assert.Equal(t, fields["referer"], "http://invalid.com/bar")
}
func TestLoggingWriter(t *testing.T) {
assert.HTTPBodyContains(t, testLogWithStatus, "GET", "/test", nil, "with-status")
assert.HTTPBodyContains(t, testLogWithoutStatus, "GET", "/test", nil, "no-status")
assert.HTTPSuccess(t, testLogWithDoubleStatus, "GET", "/test", nil)
}
|