Add requests to tests
This commit is contained in:
parent
c950ef5b02
commit
bdf991691b
|
@ -100,7 +100,10 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
if err := RegisterHandlers(cf); err != nil {
|
||||
server := CreateWebserver(cf)
|
||||
mux := http.NewServeMux()
|
||||
server.Handler = mux
|
||||
if err := RegisterHandlers(cf, mux); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -127,7 +130,6 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
server := CreateWebserver(cf)
|
||||
server.Serve(listener)
|
||||
|
||||
// read guard, should never ever ever be called.
|
||||
|
@ -193,11 +195,11 @@ func CreateWebserver(cf Config) *http.Server {
|
|||
return server
|
||||
}
|
||||
|
||||
func RegisterHandlers(cf Config) error {
|
||||
http.HandleFunc("/", createDefaultHandler())
|
||||
http.HandleFunc("/health", createHealthHandler())
|
||||
http.HandleFunc("/health.json", createHealthHandler())
|
||||
http.HandleFunc("/robots.txt", createRobotsHandler())
|
||||
func RegisterHandlers(cf Config, mux *http.ServeMux) error {
|
||||
mux.HandleFunc("/", createDefaultHandler())
|
||||
mux.HandleFunc("/health", createHealthHandler())
|
||||
mux.HandleFunc("/health.json", createHealthHandler())
|
||||
mux.HandleFunc("/robots.txt", createRobotsHandler())
|
||||
|
||||
// Register hooks
|
||||
for _, hook := range cf.Hooks {
|
||||
|
@ -207,7 +209,7 @@ func RegisterHandlers(cf Config) error {
|
|||
if hook.Concurrency < 1 {
|
||||
hook.Concurrency = 1
|
||||
}
|
||||
http.HandleFunc(hook.Route, createHandler(hook))
|
||||
mux.HandleFunc(hook.Route, createHandler(hook))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -20,14 +20,20 @@ func TestWebserver(t *testing.T) {
|
|||
cf.Settings.BindAddress = "127.0.0.1:2088"
|
||||
cf.Hooks = make([]Hook, 0)
|
||||
cf.Hooks = append(cf.Hooks, Hook{Route: "/test1", Name: "test1", Command: ""})
|
||||
cf.Hooks = append(cf.Hooks, Hook{Route: "/test2", Name: "test2", Command: ""})
|
||||
|
||||
listener, err := CreateListener(cf)
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot create listener: %s", err)
|
||||
t.Fatalf("error creating listener: %s", err)
|
||||
return
|
||||
}
|
||||
server := CreateWebserver(cf)
|
||||
RegisterHandlers(cf)
|
||||
mux := http.NewServeMux()
|
||||
server.Handler = mux
|
||||
if err := RegisterHandlers(cf, mux); err != nil {
|
||||
t.Fatalf("error registering handlers: %s", err)
|
||||
return
|
||||
}
|
||||
go server.Serve(listener)
|
||||
|
||||
assertStatusCode := func(url string, statusCode int) {
|
||||
|
@ -42,8 +48,19 @@ func TestWebserver(t *testing.T) {
|
|||
}
|
||||
|
||||
// Check default sites
|
||||
baseUrl := fmt.Sprintf("http://%s/", cf.Settings.BindAddress)
|
||||
assertStatusCode(baseUrl, 200)
|
||||
assertStatusCode(fmt.Sprintf("http://%s/", cf.Settings.BindAddress), http.StatusOK)
|
||||
assertStatusCode(fmt.Sprintf("http://%s/health", cf.Settings.BindAddress), http.StatusOK)
|
||||
assertStatusCode(fmt.Sprintf("http://%s/health.json", cf.Settings.BindAddress), http.StatusOK)
|
||||
assertStatusCode(fmt.Sprintf("http://%s/robots.txt", cf.Settings.BindAddress), http.StatusOK)
|
||||
// Check for a 404 page
|
||||
assertStatusCode(fmt.Sprintf("http://%s/404", cf.Settings.BindAddress), http.StatusNotFound)
|
||||
assertStatusCode(fmt.Sprintf("http://%s/test3", cf.Settings.BindAddress), http.StatusNotFound)
|
||||
// Test registered hooks
|
||||
assertStatusCode(fmt.Sprintf("http://%s/test1", cf.Settings.BindAddress), http.StatusOK)
|
||||
assertStatusCode(fmt.Sprintf("http://%s/test2", cf.Settings.BindAddress), http.StatusOK)
|
||||
|
||||
server.Shutdown(context.Background())
|
||||
if err := server.Shutdown(context.Background()); err != nil {
|
||||
t.Fatalf("error while server shutdown: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue