1 // Copyright 2015 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.
13 type redirectResult struct {
18 func errorResult(status int) redirectResult {
19 return redirectResult{status, ""}
22 func TestRedirects(t *testing.T) {
23 var tests = map[string]redirectResult{
24 "/build": {301, "http://build.golang.org"},
25 "/ref": {301, "/doc/#references"},
26 "/doc/mem": {301, "/ref/mem"},
27 "/doc/spec": {301, "/ref/spec"},
28 "/tour": {301, "http://tour.golang.org"},
29 "/foo": errorResult(404),
31 "/pkg/asn1": {301, "/pkg/encoding/asn1/"},
32 "/pkg/template/parse": {301, "/pkg/text/template/parse/"},
34 "/src/pkg/foo": {301, "/src/foo"},
36 "/cmd/gofix": {301, "/cmd/fix/"},
38 // git commits (/change)
39 // TODO: mercurial tags and LoadChangeMap.
40 "/change": {301, "https://go.googlesource.com/go"},
41 "/change/a": {302, "https://go.googlesource.com/go/+/a"},
43 "/issue": {301, "https://github.com/golang/go/issues"},
44 "/issue?": {301, "https://github.com/golang/go/issues"},
45 "/issue/1": {302, "https://github.com/golang/go/issues/1"},
46 "/issue/new": {301, "https://github.com/golang/go/issues/new"},
47 "/issue/new?a=b&c=d%20&e=f": {301, "https://github.com/golang/go/issues/new?a=b&c=d%20&e=f"},
48 "/issues": {301, "https://github.com/golang/go/issues"},
49 "/issues/1": {302, "https://github.com/golang/go/issues/1"},
50 "/issues/new": {301, "https://github.com/golang/go/issues/new"},
51 "/issues/1/2/3": errorResult(404),
53 "/wiki/foo": {302, "https://github.com/golang/go/wiki/foo"},
54 "/wiki/foo/": {302, "https://github.com/golang/go/wiki/foo/"},
56 "/design": {301, "https://go.googlesource.com/proposal/+/master/design"},
57 "/design/": {302, "/design"},
58 "/design/123-foo": {302, "https://go.googlesource.com/proposal/+/master/design/123-foo.md"},
59 "/design/text/123-foo": {302, "https://go.googlesource.com/proposal/+/master/design/text/123-foo.md"},
61 "/cl/1": {302, "https://go-review.googlesource.com/1"},
62 "/cl/1/": {302, "https://go-review.googlesource.com/1"},
63 "/cl/267120043": {302, "https://codereview.appspot.com/267120043"},
64 "/cl/267120043/": {302, "https://codereview.appspot.com/267120043"},
66 // Verify that we're using the Rietveld CL table:
67 "/cl/152046": {302, "https://codereview.appspot.com/152046"},
68 "/cl/152047": {302, "https://go-review.googlesource.com/152047"},
69 "/cl/152048": {302, "https://codereview.appspot.com/152048"},
71 // And verify we're using the "bigEnoughAssumeRietveld" value:
72 "/cl/299999": {302, "https://go-review.googlesource.com/299999"},
73 "/cl/300000": {302, "https://codereview.appspot.com/300000"},
76 mux := http.NewServeMux()
78 ts := httptest.NewServer(mux)
81 for path, want := range tests {
82 if want.path != "" && want.path[0] == '/' {
83 // All redirects are absolute.
84 want.path = ts.URL + want.path
87 req, err := http.NewRequest("GET", ts.URL+path, nil)
89 t.Errorf("(path: %q) unexpected error: %v", path, err)
93 resp, err := http.DefaultTransport.RoundTrip(req)
95 t.Errorf("(path: %q) unexpected error: %v", path, err)
99 if resp.StatusCode != want.status {
100 t.Errorf("(path: %q) got status %d, want %d", path, resp.StatusCode, want.status)
103 if want.status != 301 && want.status != 302 {
104 // Not a redirect. Just check status.
108 out, _ := resp.Location()
109 if got := out.String(); got != want.path {
110 t.Errorf("(path: %q) got %s, want %s", path, got, want.path)