1
// Copyright 2012 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
14
var serveMuxRegister = []struct {
18
{"/dir/", serve(200)},
19
{"/search", serve(201)},
20
{"codesearch.google.com/search", serve(202)},
21
{"codesearch.google.com/", serve(203)},
24
// serve returns a handler that sends a response with the given code.
25
func serve(code int) HandlerFunc {
26
return func(w ResponseWriter, r *Request) {
31
var serveMuxTests = []struct {
38
{"GET", "google.com", "/", 404, ""},
39
{"GET", "google.com", "/dir", 301, "/dir/"},
40
{"GET", "google.com", "/dir/", 200, "/dir/"},
41
{"GET", "google.com", "/dir/file", 200, "/dir/"},
42
{"GET", "google.com", "/search", 201, "/search"},
43
{"GET", "google.com", "/search/", 404, ""},
44
{"GET", "google.com", "/search/foo", 404, ""},
45
{"GET", "codesearch.google.com", "/search", 202, "codesearch.google.com/search"},
46
{"GET", "codesearch.google.com", "/search/", 203, "codesearch.google.com/"},
47
{"GET", "codesearch.google.com", "/search/foo", 203, "codesearch.google.com/"},
48
{"GET", "codesearch.google.com", "/", 203, "codesearch.google.com/"},
49
{"GET", "images.google.com", "/search", 201, "/search"},
50
{"GET", "images.google.com", "/search/", 404, ""},
51
{"GET", "images.google.com", "/search/foo", 404, ""},
52
{"GET", "google.com", "/../search", 301, "/search"},
53
{"GET", "google.com", "/dir/..", 301, ""},
54
{"GET", "google.com", "/dir/..", 301, ""},
55
{"GET", "google.com", "/dir/./file", 301, "/dir/"},
57
// The /foo -> /foo/ redirect applies to CONNECT requests
58
// but the path canonicalization does not.
59
{"CONNECT", "google.com", "/dir", 301, "/dir/"},
60
{"CONNECT", "google.com", "/../search", 404, ""},
61
{"CONNECT", "google.com", "/dir/..", 200, "/dir/"},
62
{"CONNECT", "google.com", "/dir/..", 200, "/dir/"},
63
{"CONNECT", "google.com", "/dir/./file", 200, "/dir/"},
66
func TestServeMuxHandler(t *testing.T) {
68
for _, e := range serveMuxRegister {
69
mux.Handle(e.pattern, e.h)
72
for _, tt := range serveMuxTests {
80
h, pattern := mux.Handler(r)
81
rr := httptest.NewRecorder()
83
if pattern != tt.pattern || rr.Code != tt.code {
84
t.Errorf("%s %s %s = %d, %q, want %d, %q", tt.method, tt.host, tt.path, rr.Code, pattern, tt.code, tt.pattern)
89
func TestServerRedirect(t *testing.T) {
90
// This used to crash. It's not valid input (bad path), but it
92
rr := httptest.NewRecorder()
97
Path: "not-empty-but-no-leading-slash", // bogus
100
Redirect(rr, req, "", 304)
102
t.Errorf("Code = %d; want 304", rr.Code)