Created
October 22, 2019 01:56
-
-
Save jeanbza/33cb8fc1f96cebaf35c46269be045bc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// // Copyright 2019 The Go Authors. All rights reserved. | |
// // Use of this source code is governed by the Apache 2.0 | |
// // license that can be found in the LICENSE file. | |
package main_test | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
"os" | |
"os/exec" | |
"runtime" | |
"strings" | |
"testing" | |
) | |
// Tests that GoogleCN templates render properly. | |
func TestGoogleCN(t *testing.T) { | |
if runtime.GOOS == "plan9" { | |
t.Skip("skipping on plan9; fails to start up quickly enough") | |
} | |
bin, cleanup := buildGodoc(t) | |
defer cleanup() | |
addr := serverAddress(t) | |
args := []string{fmt.Sprintf("-http=%s", addr)} | |
cmd := exec.Command(bin, args...) | |
cmd.Stdout = os.Stderr | |
cmd.Stderr = os.Stderr | |
// Set GOPATH variable to non-existing path | |
// and GOPROXY=off to disable module fetches. | |
// We cannot just unset GOPATH variable because godoc would default it to ~/go. | |
// (We don't want the indexer looking at the local workspace during tests.) | |
cmd.Env = append(os.Environ(), | |
"GOPATH=does_not_exist", | |
"GOPROXY=off", | |
"GO111MODULE=off") | |
if err := cmd.Start(); err != nil { | |
t.Fatalf("failed to start godoc: %s", err) | |
} | |
defer killAndWait(cmd) | |
waitForServerReady(t, addr) | |
for _, tc := range []struct { | |
name string | |
headers http.Header | |
formValues url.Values | |
shouldShowLink bool | |
}{ | |
{name: "no signals", shouldShowLink: true}, | |
{name: "CN header", headers: map[string][]string{"X-Appengine-Country": []string{"CN"}}}, | |
{name: "ZZ header", headers: map[string][]string{"X-Appengine-Country": []string{"ZZ"}}}, | |
{name: "googlecn form value", formValues: map[string][]string{"googlecn": []string{"anything"}}}, | |
} { | |
t.Run(tc.name, func(t *testing.T) { | |
// Assumes that doc.html has a GoogleCN-protected reference to | |
// tour.golang.org. | |
req, err := http.NewRequest("GET", "http://"+addr+"/doc/docs.html", nil) | |
if err != nil { | |
t.Fatal(err) | |
} | |
if tc.headers != nil { | |
req.Header = tc.headers | |
} | |
if tc.formValues != nil { | |
req.Form = tc.formValues | |
} | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
t.Fatal(err) | |
} | |
bodyBytes, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
t.Fatal(err) | |
} | |
gotLink := strings.Contains(string(bodyBytes), "tour.golang.org") | |
if tc.shouldShowLink && !gotLink { | |
t.Fatal("expected /doc/docs.html to contain tour.golang.org, but it did not") | |
} | |
if !tc.shouldShowLink && gotLink { | |
t.Fatal("expected /doc/docs.html not to contain tour.golang.org, but it did") | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment