forked from adamlaska/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
119 lines (103 loc) · 3.47 KB
/
context_test.go
File metadata and controls
119 lines (103 loc) · 3.47 KB
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
package web
import (
"bytes"
"crypto/tls"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
blog "github.com/letsencrypt/boulder/log"
"github.com/letsencrypt/boulder/test"
)
type myHandler struct{}
func (m myHandler) ServeHTTP(e *RequestEvent, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
e.Endpoint = "/endpoint"
_, _ = w.Write([]byte("hi"))
}
func TestLogCode(t *testing.T) {
mockLog := blog.UseMock()
th := NewTopHandler(mockLog, myHandler{})
req, err := http.NewRequest("GET", "/thisisignored", &bytes.Reader{})
if err != nil {
t.Fatal(err)
}
th.ServeHTTP(httptest.NewRecorder(), req)
expected := `INFO: GET /endpoint 0 201 0 0.0.0.0 JSON={}`
if len(mockLog.GetAllMatching(expected)) != 1 {
t.Errorf("Expected exactly one log line matching %q. Got \n%s",
expected, strings.Join(mockLog.GetAllMatching(".*"), "\n"))
}
}
type codeHandler struct{}
func (ch codeHandler) ServeHTTP(e *RequestEvent, w http.ResponseWriter, r *http.Request) {
e.Endpoint = "/endpoint"
_, _ = w.Write([]byte("hi"))
}
func TestStatusCodeLogging(t *testing.T) {
mockLog := blog.UseMock()
th := NewTopHandler(mockLog, codeHandler{})
req, err := http.NewRequest("GET", "/thisisignored", &bytes.Reader{})
if err != nil {
t.Fatal(err)
}
th.ServeHTTP(httptest.NewRecorder(), req)
expected := `INFO: GET /endpoint 0 200 0 0.0.0.0 JSON={}`
if len(mockLog.GetAllMatching(expected)) != 1 {
t.Errorf("Expected exactly one log line matching %q. Got \n%s",
expected, strings.Join(mockLog.GetAllMatching(".*"), "\n"))
}
}
func TestOrigin(t *testing.T) {
mockLog := blog.UseMock()
th := NewTopHandler(mockLog, myHandler{})
req, err := http.NewRequest("GET", "/thisisignored", &bytes.Reader{})
if err != nil {
t.Fatal(err)
}
req.Header.Add("Origin", "https://example.com")
th.ServeHTTP(httptest.NewRecorder(), req)
expected := `INFO: GET /endpoint 0 201 0 0.0.0.0 JSON={.*"Origin":"https://example.com"}`
if len(mockLog.GetAllMatching(expected)) != 1 {
t.Errorf("Expected exactly one log line matching %q. Got \n%s",
expected, strings.Join(mockLog.GetAllMatching(".*"), "\n"))
}
}
type hostHeaderHandler struct {
f func(*RequestEvent, http.ResponseWriter, *http.Request)
}
func (hhh hostHeaderHandler) ServeHTTP(e *RequestEvent, w http.ResponseWriter, r *http.Request) {
hhh.f(e, w, r)
}
func TestHostHeaderRewrite(t *testing.T) {
mockLog := blog.UseMock()
hhh := hostHeaderHandler{f: func(_ *RequestEvent, _ http.ResponseWriter, r *http.Request) {
t.Helper()
test.AssertEquals(t, r.Host, "localhost")
}}
th := NewTopHandler(mockLog, &hhh)
req, err := http.NewRequest("GET", "/", &bytes.Reader{})
test.AssertNotError(t, err, "http.NewRequest failed")
req.Host = "localhost:80"
fmt.Println("here")
th.ServeHTTP(httptest.NewRecorder(), req)
req, err = http.NewRequest("GET", "/", &bytes.Reader{})
test.AssertNotError(t, err, "http.NewRequest failed")
req.Host = "localhost:443"
req.TLS = &tls.ConnectionState{}
th.ServeHTTP(httptest.NewRecorder(), req)
req, err = http.NewRequest("GET", "/", &bytes.Reader{})
test.AssertNotError(t, err, "http.NewRequest failed")
req.Host = "localhost:443"
req.TLS = nil
th.ServeHTTP(httptest.NewRecorder(), req)
hhh.f = func(_ *RequestEvent, _ http.ResponseWriter, r *http.Request) {
t.Helper()
test.AssertEquals(t, r.Host, "localhost:123")
}
req, err = http.NewRequest("GET", "/", &bytes.Reader{})
test.AssertNotError(t, err, "http.NewRequest failed")
req.Host = "localhost:123"
th.ServeHTTP(httptest.NewRecorder(), req)
}