|
| 1 | +// Copyright 2018 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 | +// Regression tests to run against a production instance of godoc. |
| 6 | + |
| 7 | +package main_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "bytes" |
| 11 | + "flag" |
| 12 | + "io" |
| 13 | + "io/ioutil" |
| 14 | + "net/http" |
| 15 | + "net/url" |
| 16 | + "regexp" |
| 17 | + "strings" |
| 18 | + "testing" |
| 19 | +) |
| 20 | + |
| 21 | +var host = flag.String("regtest.host", "", "host to run regression test against") |
| 22 | + |
| 23 | +func init() { |
| 24 | + flag.Parse() |
| 25 | + *host = strings.TrimSuffix(*host, "/") |
| 26 | +} |
| 27 | + |
| 28 | +func TestLiveServer(t *testing.T) { |
| 29 | + if *host == "" { |
| 30 | + t.Skip("regtest.host flag missing.") |
| 31 | + } |
| 32 | + substringTests := []struct { |
| 33 | + Message string |
| 34 | + Path string |
| 35 | + Substring string |
| 36 | + Regexp string |
| 37 | + NoAnalytics bool // expect the response to not contain GA. |
| 38 | + PostBody string |
| 39 | + }{ |
| 40 | + { |
| 41 | + Path: "/doc/faq", |
| 42 | + Substring: "What is the purpose of the project", |
| 43 | + }, |
| 44 | + { |
| 45 | + Path: "/pkg/", |
| 46 | + Substring: "Package tar", |
| 47 | + }, |
| 48 | + { |
| 49 | + Path: "/pkg/os/", |
| 50 | + Substring: "func Open", |
| 51 | + }, |
| 52 | + { |
| 53 | + Path: "/robots.txt", |
| 54 | + Substring: "Disallow: /search", |
| 55 | + Message: "robots not present - not deployed from Dockerfile?", |
| 56 | + NoAnalytics: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + Path: "/change/75944e2e3a63", |
| 60 | + Substring: "bdb10cf", |
| 61 | + Message: "no change redirect - hg to git mapping not registered?", |
| 62 | + NoAnalytics: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + Path: "/dl/", |
| 66 | + Substring: "go1.11.windows-amd64.msi", |
| 67 | + Message: "missing data on dl page - misconfiguration of datastore?", |
| 68 | + }, |
| 69 | + { |
| 70 | + Path: "/dl/?mode=json", |
| 71 | + Substring: ".windows-amd64.msi", |
| 72 | + NoAnalytics: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + Message: "broken shortlinks - misconfiguration of datastore or memcache?", |
| 76 | + Path: "/s/go2design", |
| 77 | + Regexp: "proposal.*Found", |
| 78 | + NoAnalytics: true, |
| 79 | + }, |
| 80 | + { |
| 81 | + Message: "incorrect search result - broken index?", |
| 82 | + Path: "/search?q=IsDir", |
| 83 | + Substring: "src/os/types.go", |
| 84 | + }, |
| 85 | + { |
| 86 | + Path: "/compile", |
| 87 | + PostBody: "body=" + url.QueryEscape("package main; func main() { print(6*7); }"), |
| 88 | + Regexp: `^{"compile_errors":"","output":"42"}$`, |
| 89 | + NoAnalytics: true, |
| 90 | + }, |
| 91 | + { |
| 92 | + Path: "/compile", |
| 93 | + PostBody: "body=" + url.QueryEscape("//empty"), |
| 94 | + Substring: "expected 'package', found 'EOF'", |
| 95 | + NoAnalytics: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + Path: "/compile", |
| 99 | + PostBody: "version=2&body=package+main%3Bimport+(%22fmt%22%3B%22time%22)%3Bfunc+main()%7Bfmt.Print(%22A%22)%3Btime.Sleep(time.Second)%3Bfmt.Print(%22B%22)%7D", |
| 100 | + Regexp: `^{"Errors":"","Events":\[{"Message":"A","Kind":"stdout","Delay":0},{"Message":"B","Kind":"stdout","Delay":1000000000}\]}$`, |
| 101 | + NoAnalytics: true, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tc := range substringTests { |
| 106 | + t.Run(tc.Path, func(t *testing.T) { |
| 107 | + method := "GET" |
| 108 | + var reqBody io.Reader |
| 109 | + if tc.PostBody != "" { |
| 110 | + method = "POST" |
| 111 | + reqBody = strings.NewReader(tc.PostBody) |
| 112 | + } |
| 113 | + req, err := http.NewRequest(method, *host+tc.Path, reqBody) |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("NewRequest: %v", err) |
| 116 | + } |
| 117 | + if reqBody != nil { |
| 118 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 119 | + } |
| 120 | + resp, err := http.DefaultTransport.RoundTrip(req) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("RoundTrip: %v", err) |
| 123 | + } |
| 124 | + body, err := ioutil.ReadAll(resp.Body) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("ReadAll: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + const googleAnalyticsID = "UA-11222381-2" // golang.org analytics ID |
| 130 | + if !tc.NoAnalytics && !bytes.Contains(body, []byte(googleAnalyticsID)) { |
| 131 | + t.Errorf("want response to contain analytics tracking ID") |
| 132 | + } |
| 133 | + |
| 134 | + if tc.Substring != "" { |
| 135 | + tc.Regexp = regexp.QuoteMeta(tc.Substring) |
| 136 | + } |
| 137 | + re := regexp.MustCompile(tc.Regexp) |
| 138 | + |
| 139 | + if !re.Match(body) { |
| 140 | + t.Log("------ actual output -------") |
| 141 | + t.Log(string(body)) |
| 142 | + t.Log("----------------------------") |
| 143 | + if tc.Message != "" { |
| 144 | + t.Log(tc.Message) |
| 145 | + } |
| 146 | + t.Fatalf("wanted response to match %s", tc.Regexp) |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
0 commit comments