~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/net/http/server_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
4
 
 
5
package http_test
 
6
 
 
7
import (
 
8
        . "net/http"
 
9
        "net/http/httptest"
 
10
        "net/url"
 
11
        "testing"
 
12
)
 
13
 
 
14
var serveMuxRegister = []struct {
 
15
        pattern string
 
16
        h       Handler
 
17
}{
 
18
        {"/dir/", serve(200)},
 
19
        {"/search", serve(201)},
 
20
        {"codesearch.google.com/search", serve(202)},
 
21
        {"codesearch.google.com/", serve(203)},
 
22
}
 
23
 
 
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) {
 
27
                w.WriteHeader(code)
 
28
        }
 
29
}
 
30
 
 
31
var serveMuxTests = []struct {
 
32
        method  string
 
33
        host    string
 
34
        path    string
 
35
        code    int
 
36
        pattern string
 
37
}{
 
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/"},
 
56
 
 
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/"},
 
64
}
 
65
 
 
66
func TestServeMuxHandler(t *testing.T) {
 
67
        mux := NewServeMux()
 
68
        for _, e := range serveMuxRegister {
 
69
                mux.Handle(e.pattern, e.h)
 
70
        }
 
71
 
 
72
        for _, tt := range serveMuxTests {
 
73
                r := &Request{
 
74
                        Method: tt.method,
 
75
                        Host:   tt.host,
 
76
                        URL: &url.URL{
 
77
                                Path: tt.path,
 
78
                        },
 
79
                }
 
80
                h, pattern := mux.Handler(r)
 
81
                rr := httptest.NewRecorder()
 
82
                h.ServeHTTP(rr, r)
 
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)
 
85
                }
 
86
        }
 
87
}
 
88
 
 
89
func TestServerRedirect(t *testing.T) {
 
90
        // This used to crash. It's not valid input (bad path), but it
 
91
        // shouldn't crash.
 
92
        rr := httptest.NewRecorder()
 
93
        req := &Request{
 
94
                Method: "GET",
 
95
                URL: &url.URL{
 
96
                        Scheme: "http",
 
97
                        Path:   "not-empty-but-no-leading-slash", // bogus
 
98
                },
 
99
        }
 
100
        Redirect(rr, req, "", 304)
 
101
        if rr.Code != 304 {
 
102
                t.Errorf("Code = %d; want 304", rr.Code)
 
103
        }
 
104
}