github.com/v2fly/tools@v0.100.0/godoc/redirect/redirect_test.go (about)

     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.
     4  
     5  package redirect
     6  
     7  import (
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  )
    12  
    13  type redirectResult struct {
    14  	status int
    15  	path   string
    16  }
    17  
    18  func errorResult(status int) redirectResult {
    19  	return redirectResult{status, ""}
    20  }
    21  
    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),
    30  
    31  		"/pkg/asn1":           {301, "/pkg/encoding/asn1/"},
    32  		"/pkg/template/parse": {301, "/pkg/text/template/parse/"},
    33  
    34  		"/src/pkg/foo": {301, "/src/foo"},
    35  
    36  		"/cmd/gofix": {301, "/cmd/fix/"},
    37  
    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"},
    42  
    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),
    52  
    53  		"/wiki/foo":  {302, "https://github.com/golang/go/wiki/foo"},
    54  		"/wiki/foo/": {302, "https://github.com/golang/go/wiki/foo/"},
    55  
    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"},
    60  
    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"},
    65  
    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"},
    70  
    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"},
    74  	}
    75  
    76  	mux := http.NewServeMux()
    77  	Register(mux)
    78  	ts := httptest.NewServer(mux)
    79  	defer ts.Close()
    80  
    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
    85  		}
    86  
    87  		req, err := http.NewRequest("GET", ts.URL+path, nil)
    88  		if err != nil {
    89  			t.Errorf("(path: %q) unexpected error: %v", path, err)
    90  			continue
    91  		}
    92  
    93  		resp, err := http.DefaultTransport.RoundTrip(req)
    94  		if err != nil {
    95  			t.Errorf("(path: %q) unexpected error: %v", path, err)
    96  			continue
    97  		}
    98  
    99  		if resp.StatusCode != want.status {
   100  			t.Errorf("(path: %q) got status %d, want %d", path, resp.StatusCode, want.status)
   101  		}
   102  
   103  		if want.status != 301 && want.status != 302 {
   104  			// Not a redirect. Just check status.
   105  			continue
   106  		}
   107  
   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)
   111  		}
   112  	}
   113  }