|
| 1 | +// Copyright 2019 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 main_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "io/ioutil" |
| 12 | + "net/http" |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "path/filepath" |
| 16 | + "strings" |
| 17 | + "testing" |
| 18 | +) |
| 19 | + |
| 20 | +func TestSnippetsCompile(t *testing.T) { |
| 21 | + if testing.Short() { |
| 22 | + t.Skip("skipping slow builds in short mode") |
| 23 | + } |
| 24 | + |
| 25 | + goFiles, err := filepath.Glob("*.go") |
| 26 | + if err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + |
| 30 | + for _, f := range goFiles { |
| 31 | + if strings.HasSuffix(f, "_test.go") { |
| 32 | + continue |
| 33 | + } |
| 34 | + f := f |
| 35 | + t.Run(f, func(t *testing.T) { |
| 36 | + t.Parallel() |
| 37 | + |
| 38 | + cmd := exec.Command("go", "build", "-o", os.DevNull, f) |
| 39 | + out, err := cmd.CombinedOutput() |
| 40 | + if err != nil { |
| 41 | + t.Errorf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, out) |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestWikiServer(t *testing.T) { |
| 48 | + must := func(err error) { |
| 49 | + if err != nil { |
| 50 | + t.Helper() |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + dir, err := ioutil.TempDir("", t.Name()) |
| 56 | + must(err) |
| 57 | + defer os.RemoveAll(dir) |
| 58 | + |
| 59 | + // We're testing a walkthrough example of how to write a server. |
| 60 | + // |
| 61 | + // That server hard-codes a port number to make the walkthrough simpler, but |
| 62 | + // we can't assume that the hard-coded port is available on an arbitrary |
| 63 | + // builder. So we'll patch out the hard-coded port, and replace it with a |
| 64 | + // function that writes the server's address to stdout |
| 65 | + // so that we can read it and know where to send the test requests. |
| 66 | + |
| 67 | + finalGo, err := ioutil.ReadFile("final.go") |
| 68 | + must(err) |
| 69 | + const patchOld = `log.Fatal(http.ListenAndServe(":8080", nil))` |
| 70 | + patched := bytes.ReplaceAll(finalGo, []byte(patchOld), []byte(`log.Fatal(serve())`)) |
| 71 | + if bytes.Equal(patched, finalGo) { |
| 72 | + t.Fatalf("Can't patch final.go: %q not found.", patchOld) |
| 73 | + } |
| 74 | + must(ioutil.WriteFile(filepath.Join(dir, "final_patched.go"), patched, 0644)) |
| 75 | + |
| 76 | + // Build the server binary from the patched sources. |
| 77 | + // The 'go' command requires that they all be in the same directory. |
| 78 | + // final_test.go provides the implemtation for our serve function. |
| 79 | + must(copyFile(filepath.Join(dir, "final_srv.go"), "final_test.go")) |
| 80 | + cmd := exec.Command("go", "build", |
| 81 | + "-o", filepath.Join(dir, "final.exe"), |
| 82 | + filepath.Join(dir, "final_patched.go"), |
| 83 | + filepath.Join(dir, "final_srv.go")) |
| 84 | + out, err := cmd.CombinedOutput() |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, out) |
| 87 | + } |
| 88 | + |
| 89 | + // Run the server in our temporary directory so that it can |
| 90 | + // write its content there. It also needs a couple of template files, |
| 91 | + // and looks for them in the same directory. |
| 92 | + must(copyFile(filepath.Join(dir, "edit.html"), "edit.html")) |
| 93 | + must(copyFile(filepath.Join(dir, "view.html"), "view.html")) |
| 94 | + cmd = exec.Command(filepath.Join(dir, "final.exe")) |
| 95 | + cmd.Dir = dir |
| 96 | + stderr := bytes.NewBuffer(nil) |
| 97 | + cmd.Stderr = stderr |
| 98 | + stdout, err := cmd.StdoutPipe() |
| 99 | + must(err) |
| 100 | + must(cmd.Start()) |
| 101 | + |
| 102 | + defer func() { |
| 103 | + cmd.Process.Kill() |
| 104 | + err := cmd.Wait() |
| 105 | + if stderr.Len() > 0 { |
| 106 | + t.Logf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, stderr) |
| 107 | + } |
| 108 | + }() |
| 109 | + |
| 110 | + var addr string |
| 111 | + if _, err := fmt.Fscanln(stdout, &addr); err != nil || addr == "" { |
| 112 | + t.Fatalf("Failed to read server address: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + // The server is up and has told us its address. |
| 116 | + // Make sure that its HTTP API works as described in the article. |
| 117 | + |
| 118 | + r, err := http.Get(fmt.Sprintf("http://%s/edit/Test", addr)) |
| 119 | + must(err) |
| 120 | + responseMustMatchFile(t, r, "test_edit.good") |
| 121 | + |
| 122 | + r, err = http.Post(fmt.Sprintf("http://%s/save/Test", addr), |
| 123 | + "application/x-www-form-urlencoded", |
| 124 | + strings.NewReader("body=some%20content")) |
| 125 | + must(err) |
| 126 | + responseMustMatchFile(t, r, "test_view.good") |
| 127 | + |
| 128 | + gotTxt, err := ioutil.ReadFile(filepath.Join(dir, "Test.txt")) |
| 129 | + must(err) |
| 130 | + wantTxt, err := ioutil.ReadFile("test_Test.txt.good") |
| 131 | + must(err) |
| 132 | + if !bytes.Equal(wantTxt, gotTxt) { |
| 133 | + t.Fatalf("Test.txt differs from expected after posting to /save.\ngot:\n%s\nwant:\n%s", gotTxt, wantTxt) |
| 134 | + } |
| 135 | + |
| 136 | + r, err = http.Get(fmt.Sprintf("http://%s/view/Test", addr)) |
| 137 | + must(err) |
| 138 | + responseMustMatchFile(t, r, "test_view.good") |
| 139 | +} |
| 140 | + |
| 141 | +func responseMustMatchFile(t *testing.T, r *http.Response, filename string) { |
| 142 | + t.Helper() |
| 143 | + |
| 144 | + defer r.Body.Close() |
| 145 | + body, err := io.ReadAll(r.Body) |
| 146 | + if err != nil { |
| 147 | + t.Fatal(err) |
| 148 | + } |
| 149 | + |
| 150 | + wantBody, err := ioutil.ReadFile(filename) |
| 151 | + if err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + |
| 155 | + if !bytes.Equal(body, wantBody) { |
| 156 | + t.Fatalf("%v: body does not match %s.\ngot:\n%s\nwant:\n%s", r.Request.URL, filename, body, wantBody) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func copyFile(dst, src string) error { |
| 161 | + buf, err := ioutil.ReadFile(src) |
| 162 | + if err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + return ioutil.WriteFile(dst, buf, 0644) |
| 166 | +} |
0 commit comments