~mna/kick

ref: e42652bfd3e60c862f089c6cf03d8cf0623400ef kick/builder/builder_test.go -rw-r--r-- 6.6 KiB View raw
e42652bfMartin Angers start validating handler 1 year, 3 months ago
                                                                                
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package builder

import (
	"crypto/tls"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"testing"

	"git.sr.ht/~mna/kick"
	"github.com/unrolled/secure"
)

func TestTLS_Intermediate(t *testing.T) {
	s := &kick.Server{
		TLS: &kick.TLSConfig{
			AutoCert: true,
			Mode:     kick.TLSIntermediate,
		},
	}

	conf, err := TLS(s)
	if err != nil {
		t.Fatalf("want no error, got %s", err)
	}
	if !conf.PreferServerCipherSuites {
		t.Fatalf("want PreferServerCipherSuites to be true")
	}
}

func TestTLS_Modern(t *testing.T) {
	s := &kick.Server{
		TLS: &kick.TLSConfig{
			AutoCert: true,
			Mode:     kick.TLSModern,
		},
	}

	conf, err := TLS(s)
	if err != nil {
		t.Fatalf("want no error, got %s", err)
	}
	if !conf.PreferServerCipherSuites {
		t.Fatalf("want PreferServerCipherSuites to be true")
	}
	if conf.MinVersion != tls.VersionTLS12 {
		t.Fatalf("want MinVersion to be %v, got %v", tls.VersionTLS12, conf.MinVersion)
	}
}

func TestTLS_InvalidMode(t *testing.T) {
	s := &kick.Server{
		TLS: &kick.TLSConfig{
			AutoCert: true,
			Mode:     kick.TLSModern + 1000,
		},
	}

	_, err := TLS(s)
	if err == nil {
		t.Fatalf("want error, got none")
	}
	if !strings.Contains(err.Error(), "tls: unsupported TLS mode") {
		t.Fatalf("unexpected error message: %s", err)
	}
}

func TestTLS_AutoCert(t *testing.T) {
	s := &kick.Server{
		TLS: &kick.TLSConfig{
			AutoCert: true,
		},
	}

	conf, err := TLS(s)
	if err != nil {
		t.Fatalf("want no error, got %s", err)
	}
	if conf.GetCertificate == nil {
		t.Fatalf("want GetCertificate to be set")
	}
}

func TestTLS_ValidCert(t *testing.T) {
	s := &kick.Server{
		TLS: &kick.TLSConfig{
			CertFile: os.Getenv("KICK_TEST_LOCALHOST_CERT"),
			KeyFile:  os.Getenv("KICK_TEST_LOCALHOST_KEY"),
		},
	}

	conf, err := TLS(s)
	if err != nil {
		t.Fatalf("want no error, got %s", err)
	}
	if len(conf.Certificates) != 1 {
		t.Fatalf("want 1 certificate, got %d", len(conf.Certificates))
	}
}

func TestTLS_InvalidCert(t *testing.T) {
	s := &kick.Server{
		TLS: &kick.TLSConfig{
			CertFile: os.Getenv("KICK_TEST_LOCALHOST_CERT"),
		},
	}

	_, err := TLS(s)
	if err == nil {
		t.Fatalf("want error, got none")
	}
	if !strings.Contains(err.Error(), "tls: certificate and key files") {
		t.Fatalf("unexpected error message: %s", err)
	}
}

func TestTLS_NoConfig(t *testing.T) {
	s := new(kick.Server)
	conf, err := TLS(s)
	if conf != nil {
		t.Fatalf("want no config, got %v", conf)
	}
	if err != nil {
		t.Fatalf("want no error, got %s", err)
	}
}

func TestHTTPServer(t *testing.T) {
	s := &kick.Server{
		Addr: ":1234",
	}
	srv, err := HTTPServer(s)
	if err != nil {
		t.Fatalf("want no error, got %s", err)
	}
	if srv.Addr != s.Addr {
		t.Fatalf("want address %s, got %s", s.Addr, srv.Addr)
	}
}

func TestHandler_InvalidCanonicalRedirect(t *testing.T) {
	s := &kick.Server{
		Root: &kick.Root{
			MethodNotAllowedHandler:         statusHandler(405),
			NotFoundHandler:                 statusHandler(404),
			PanicRecoveryFunc:               func(w http.ResponseWriter, r *http.Request, v interface{}) {},
			TrustProxyHeaders:               true,
			AllowMethodOverride:             true,
			RequestIDHeader:                 "X",
			LoggingFunc:                     func(w http.ResponseWriter, r *http.Request, info map[string]interface{}) {},
			CanonicalHost:                   "abc",
			CanonicalHostRedirectStatusCode: 500,
		},
	}
	_, err := Handler(s)
	if err == nil {
		t.Fatalf("want error, got none")
	}
	if !strings.Contains(err.Error(), "canonical host redirection") {
		t.Fatalf("unexpected error message: %s", err)
	}
}

func TestHandler_InvalidGzip(t *testing.T) {
	s := &kick.Server{
		Root: &kick.Root{
			CanonicalHost:                   "abc",
			CanonicalHostRedirectStatusCode: 301,
			Gzip: &kick.GzipConfig{
				CompressionLevel: -1000,
			},
		},
	}
	_, err := Handler(s)
	if err == nil {
		t.Fatalf("want error, got none")
	}
	if !strings.Contains(err.Error(), "invalid compression level") {
		t.Fatalf("unexpected error message: %s", err)
	}
}

func TestHandler_RouteNoConfig(t *testing.T) {
	s := &kick.Server{
		Root: &kick.Root{
			TrustProxyHeaders: true,
		},
		Routes: []*kick.Route{
			{
				Path:    "/",
				Method:  "GET",
				Handler: statusHandler(200),
			},
		},
	}

	h, err := Handler(s)
	if err != nil {
		t.Fatalf("want no error, got %s", err)
	}

	w := httptest.NewRecorder()
	r, _ := http.NewRequest("", "/", nil)
	h.ServeHTTP(w, r)

	if w.Code != 200 {
		t.Fatalf("want status 200, got %d", w.Code)
	}
}

func TestHandler_InvalidRoute(t *testing.T) {
	cases := []struct {
		route  *kick.Route
		errMsg string
	}{
		{
			&kick.Route{
				Path:    "",
				Method:  "GET",
				Handler: statusHandler(200),
			},
			"must have a path",
		},
		{
			&kick.Route{
				Path:    "X",
				Method:  "GET",
				Handler: statusHandler(200),
			},
			"must start with a slash",
		},
		{
			&kick.Route{
				Path:    "/a",
				Method:  "",
				Handler: statusHandler(200),
			},
			"method is missing",
		},
		{
			&kick.Route{
				Path:    "/a",
				Method:  "GET",
				Handler: nil,
			},
			"handler is missing",
		},
	}
	for _, c := range cases {
		t.Run(fmt.Sprintf("%s %s", c.route.Method, c.route.Path), func(t *testing.T) {
			s := &kick.Server{
				Routes: []*kick.Route{
					c.route,
				},
			}

			_, err := Handler(s)
			if err == nil {
				t.Fatalf("want error, got none")
			}
			if !strings.Contains(err.Error(), c.errMsg) {
				t.Fatalf("unexpected error message: %s", err)
			}
		})
	}
}

func TestHandler_Valid(t *testing.T) {
	s := &kick.Server{
		Root: &kick.Root{
			MethodNotAllowedHandler: statusHandler(405),
			NotFoundHandler:         statusHandler(404),
			PanicRecoveryFunc: func(w http.ResponseWriter, r *http.Request, v interface{}) {
				w.WriteHeader(500)
			},
			TrustProxyHeaders:   true,
			AllowMethodOverride: true,
			RequestIDHeader:     "X-Request-Id",
			SecurityHeaders: &secure.Options{
				FrameDeny: true,
			},
			Gzip: &kick.GzipConfig{
				ContentTypes: []string{"application/xml"},
			},
		},
		Routes: []*kick.Route{
			{
				Method:  "GET",
				Path:    "/",
				Handler: statusHandler(200),
			},
			{
				Method: "POST",
				Path:   "/panic",
				Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
					panic(io.EOF)
				}),
			},
		},
	}

	h, err := Handler(s)
	if err != nil {
		t.Fatalf("want no error, got %s", err)
	}

	cases := []struct {
		req  string // formatted as "METHOD /path/... body"
		reqh string // formatted as "Name:Value Name:Value"
		code int
		resh string // formatted as "Name:Value", * means any value
	}{
		{
			req:  "GET /",
			code: 200,
			resh: "X-Request-Id:*",
		},
	}
	for _, c := range cases {
		t.Run(fmt.Sprintf("%s %s", c.req, c.reqh), func(t *testing.T) {
			_ = h
		})
	}
}