261101.6 · d81d8f77dde7b2f932e94d374c52339ffce44bfc
Tests follow the code they cover
server_test.go and web_test.go kept their tests when server.go and web.go split. Tests now sit in the test file of the code they drive, and the shared helpers stay in the two original files.
Declarations moved unchanged, as a sorted comparison of every line outside the import blocks confirms. git blame -C -C traces them.
reading declarations
Changes to guard_test.go. All files
@@ -0,0 +1,37 @@+package main++import (+ "net/http"+ "os"+ "path/filepath"+ "testing"+)++func TestForeignHostRejected(t *testing.T) {+ s, _, _ := serveTree(t)+ if rec := get(s.handler(), "/", "Host", "evil.example:7417"); rec.Code != http.StatusMisdirectedRequest {+ t.Fatalf("status %d, want 421", rec.Code)+ }+ for _, host := range []string{"localhost:9", "[::1]:7417", "127.0.0.1"} {+ if rec := get(s.handler(), "/bin.dat", "Host", host); rec.Code != http.StatusOK {+ t.Errorf("host %s: status %d, want 200", host, rec.Code)+ }+ }+}++func TestLocalHost(t *testing.T) {+ hosts := filepath.Join(t.TempDir(), "hosts")+ os.WriteFile(hosts, []byte("127.31.1.1 docs docs.local # "+progName+"\n192.168.1.5 nas\n# 127.0.0.1 commented\n"), 0o644)+ old := hostsFile+ hostsFile = hosts+ t.Cleanup(func() { hostsFile = old })+ for host, want := range map[string]bool{+ "127.31.1.1": true, "127.31.200.1": true, "127.0.0.1": true, "::1": true, "localhost": true,+ "docs": true, "DOCS.local": true,+ "nas": false, "commented": false, "evil.example": false, "10.0.0.1": false,+ } {+ if got := localHost(host); got != want {+ t.Errorf("%s: %v, want %v", host, got, want)+ }+ }+}