GoPLS Viewer

Home|gopls/godoc/redirect/redirect_test.go
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
5package redirect
6
7import (
8    "net/http"
9    "net/http/httptest"
10    "testing"
11)
12
13type redirectResult struct {
14    status int
15    path   string
16}
17
18func errorResult(status intredirectResult {
19    return redirectResult{status""}
20}
21
22func TestRedirects(t *testing.T) {
23    var tests = map[string]redirectResult{
24        "/foo"errorResult(404),
25    }
26
27    mux := http.NewServeMux()
28    Register(mux)
29    ts := httptest.NewServer(mux)
30    defer ts.Close()
31
32    for pathwant := range tests {
33        if want.path != "" && want.path[0] == '/' {
34            // All redirects are absolute.
35            want.path = ts.URL + want.path
36        }
37
38        reqerr := http.NewRequest("GET"ts.URL+pathnil)
39        if err != nil {
40            t.Errorf("(path: %q) unexpected error: %v"patherr)
41            continue
42        }
43
44        resperr := http.DefaultTransport.RoundTrip(req)
45        if err != nil {
46            t.Errorf("(path: %q) unexpected error: %v"patherr)
47            continue
48        }
49        resp.Body.Close() // We only care about the headers, so close the body immediately.
50
51        if resp.StatusCode != want.status {
52            t.Errorf("(path: %q) got status %d, want %d"pathresp.StatusCodewant.status)
53        }
54
55        if want.status != 301 && want.status != 302 {
56            // Not a redirect. Just check status.
57            continue
58        }
59
60        out_ := resp.Location()
61        if got := out.String(); got != want.path {
62            t.Errorf("(path: %q) got %s, want %s"pathgotwant.path)
63        }
64    }
65}
66
MembersX
httptest
redirectResult.status
TestRedirects.t
TestRedirects.RangeStmt_578.BlockStmt.resp
redirectResult.path
TestRedirects.mux
TestRedirects.RangeStmt_578.want
TestRedirects.RangeStmt_578.BlockStmt.got
redirectResult
TestRedirects.ts
TestRedirects.RangeStmt_578.path
TestRedirects.RangeStmt_578.BlockStmt.err
TestRedirects.RangeStmt_578.BlockStmt.out
TestRedirects.RangeStmt_578.BlockStmt._
testing
errorResult
errorResult.status
TestRedirects
TestRedirects.RangeStmt_578.BlockStmt.req
Members
X