gopkg.in/hugelgupf/u-root.v9@v9.0.0-20180831063832-3f6f1057f09b/cmds/wget/wget_test.go (about) 1 // Copyright 2017 the u-root 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 // A parity test can be run: 6 // go test 7 // EXECPATH="wget -O -" go test 8 package main 9 10 import ( 11 "fmt" 12 "io/ioutil" 13 "net" 14 "net/http" 15 "path/filepath" 16 "testing" 17 18 "github.com/u-root/u-root/pkg/testutil" 19 ) 20 21 const content = "Very simple web server" 22 23 type handler struct{} 24 25 func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 26 switch r.URL.Path { 27 case "/200": 28 w.WriteHeader(200) 29 w.Write([]byte(content)) 30 case "/302": 31 http.Redirect(w, r, "/200", 302) 32 case "/500": 33 w.WriteHeader(500) 34 w.Write([]byte(content)) 35 default: 36 w.WriteHeader(404) 37 w.Write([]byte(content)) 38 } 39 } 40 41 var tests = []struct { 42 flags []string // in, %[1]d is the server's port, %[2] is an unopen port 43 url string // in 44 content string // out 45 retCode int // out 46 }{ 47 { 48 // basic 49 flags: []string{}, 50 url: "http://localhost:%[1]d/200", 51 content: content, 52 retCode: 0, 53 }, { 54 // ipv4 55 flags: []string{}, 56 url: "http://127.0.0.1:%[1]d/200", 57 content: content, 58 retCode: 0, 59 }, /*{ TODO: travis does not support ipv6 60 // ipv6 61 flags: []string{}, 62 url: "http://[::1]:%[1]d/200", 63 content: content, 64 retCode: 0, 65 },*/{ 66 // redirect 67 flags: []string{}, 68 url: "http://localhost:%[1]d/302", 69 content: "", 70 retCode: 0, 71 }, { 72 // 4xx error 73 flags: []string{}, 74 url: "http://localhost:%[1]d/404", 75 content: "", 76 retCode: 1, 77 }, { 78 // 5xx error 79 flags: []string{}, 80 url: "http://localhost:%[1]d/500", 81 content: "", 82 retCode: 1, 83 }, { 84 // no server 85 flags: []string{}, 86 url: "http://localhost:%[2]d/200", 87 content: "", 88 retCode: 1, 89 }, { 90 // output file 91 flags: []string{"-O", "/dev/null"}, 92 url: "http://localhost:%[1]d/200", 93 content: "", 94 retCode: 0, 95 }, 96 } 97 98 func getFreePort(t *testing.T) int { 99 l, err := net.Listen("tcp", ":0") 100 if err != nil { 101 t.Fatalf("Cannot create free port: %v", err) 102 } 103 l.Close() 104 return l.Addr().(*net.TCPAddr).Port 105 } 106 107 // TestWget implements a table-driven test. 108 func TestWget(t *testing.T) { 109 // Start a webserver on a free port. 110 unusedPort := getFreePort(t) 111 112 l, err := net.Listen("tcp", ":0") 113 if err != nil { 114 t.Fatalf("Cannot create free port: %v", err) 115 } 116 port := l.Addr().(*net.TCPAddr).Port 117 118 h := handler{} 119 go func() { 120 t.Fatal(http.Serve(l, h)) 121 }() 122 123 for i, tt := range tests { 124 args := append(tt.flags, fmt.Sprintf(tt.url, port, unusedPort)) 125 _, err := testutil.Command(t, args...).Output() 126 127 // Check return code. 128 if err := testutil.IsExitCode(err, tt.retCode); err != nil { 129 t.Error(err) 130 } 131 132 if tt.content != "" { 133 fileName := filepath.Base(tt.url) 134 content, err := ioutil.ReadFile(fileName) 135 if err != nil { 136 t.Errorf("%d. File %s was not created: %v", i, fileName, err) 137 } 138 139 // Check content. 140 if string(content) != tt.content { 141 t.Errorf("%d. Want:\n%#v\nGot:\n%#v", i, tt.content, string(content)) 142 } 143 } 144 } 145 } 146 147 func TestMain(m *testing.M) { 148 testutil.Run(m, main) 149 }