Add HW 3
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package hw3test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWaitForServerUsesConfiguredHost(t *testing.T) {
|
||||
t.Setenv("SOLUTION_HOST", "127.0.0.1")
|
||||
for _, tc := range []struct {
|
||||
domain string
|
||||
status int
|
||||
}{
|
||||
{"localhost", http.StatusOK},
|
||||
{"files.example.com", http.StatusNotFound},
|
||||
{"redirect.example.com", http.StatusFound},
|
||||
} {
|
||||
t.Run(tc.domain, func(t *testing.T) {
|
||||
type probe struct{ method, target, host string }
|
||||
received := make(chan probe, 16)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
select {
|
||||
case received <- probe{r.Method, r.RequestURI, r.Host}:
|
||||
default:
|
||||
}
|
||||
if tc.status == http.StatusFound {
|
||||
w.Header().Set("Location", "/redirected")
|
||||
}
|
||||
w.WriteHeader(tc.status)
|
||||
}))
|
||||
defer server.Close()
|
||||
opts := RunOpts{
|
||||
Port: server.Listener.Addr().(*net.TCPAddr).Port,
|
||||
ServerDomain: tc.domain,
|
||||
}
|
||||
if err := WaitForServer(NewTestContext(t), opts); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case got := <-received:
|
||||
if want := (probe{"GET", "/", tc.domain}); got != want {
|
||||
t.Fatalf("readiness request = %+v, want %+v", got, want)
|
||||
}
|
||||
default:
|
||||
t.Fatal("server was marked ready without receiving a request")
|
||||
}
|
||||
select {
|
||||
case extra := <-received:
|
||||
t.Fatalf("unexpected extra readiness request: %+v", extra)
|
||||
default:
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user