Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a configurable default backend. #426

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions integration_tests/backend_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import (
"github.com/onsi/gomega/ghttp"
)

func startSimpleBackend(identifier string) *httptest.Server {
func startDummyBackend(id string, statusCode int) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(identifier))
w.Header().Add("Server", id)
w.WriteHeader(statusCode)
_, err := w.Write([]byte(id))
Expect(err).NotTo(HaveOccurred())
}))
}

func startSimpleBackend(id string) *httptest.Server {
return startDummyBackend(id, 200)
}

func startTarpitBackend(delays ...time.Duration) *httptest.Server {
responseDelay := 2 * time.Second
if len(delays) > 0 {
Expand All @@ -29,18 +35,14 @@ func startTarpitBackend(delays ...time.Duration) *httptest.Server {
bodyDelay = delays[1]
}
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body := "Tarpit\n"
const body = "Tarpit\n"

if responseDelay > 0 {
time.Sleep(responseDelay)
}
time.Sleep(responseDelay)
w.Header().Add("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(http.StatusOK)
w.(http.Flusher).Flush()

if bodyDelay > 0 {
time.Sleep(bodyDelay)
}
time.Sleep(bodyDelay)
_, err := w.Write([]byte(body))
Expect(err).NotTo(HaveOccurred())
}))
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/disabled_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ var _ = Describe("marking routes as disabled", func() {

It("should return a 503 to the client", func() {
resp := routerRequest(routerPort, "/unavailable")
Expect(resp.StatusCode).To(Equal(503))
Expect(resp).To(HaveHTTPStatus(503))
})

It("should continue to route other requests", func() {
resp := routerRequest(routerPort, "/something-live")
Expect(resp.StatusCode).To(Equal(301))
Expect(resp).To(HaveHTTPStatus(301))
Expect(resp.Header.Get("Location")).To(Equal("/somewhere-else"))
})
})
Expand Down
12 changes: 6 additions & 6 deletions integration_tests/error_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
)

var _ = Describe("error handling", func() {

Describe("handling an empty routing table", func() {
Describe("when no routes are loaded", func() {
BeforeEach(func() {
reloadRoutes(apiPort)
})

It("should return a 503 error to the client", func() {
It("should forward to the default backend", func() {
resp := routerRequest(routerPort, "/")
Expect(resp.StatusCode).To(Equal(503))
Expect(resp).To(HaveHTTPHeaderWithValue("Server", "dummy-default-backend"))

resp = routerRequest(routerPort, "/foo")
Expect(resp.StatusCode).To(Equal(503))
Expect(resp).To(HaveHTTPStatus(404))
Expect(resp).To(HaveHTTPHeaderWithValue("Server", "dummy-default-backend"))
})
})

Expand All @@ -31,7 +31,7 @@ var _ = Describe("error handling", func() {

It("should return a 500 error to the client", func() {
resp := routerRequest(routerPort, "/boom")
Expect(resp.StatusCode).To(Equal(500))
Expect(resp).To(HaveHTTPStatus(500))
})

It("should log the fact", func() {
Expand Down
15 changes: 5 additions & 10 deletions integration_tests/gone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

var _ = Describe("Gone routes", func() {

BeforeEach(func() {
addRoute("/foo", NewGoneRoute())
addRoute("/bar", NewGoneRoute("prefix"))
Expand All @@ -15,21 +14,17 @@ var _ = Describe("Gone routes", func() {

It("should support an exact gone route", func() {
resp := routerRequest(routerPort, "/foo")
Expect(resp.StatusCode).To(Equal(410))
Expect(readBody(resp)).To(Equal("410 Gone\n"))
Expect(resp).To(HaveHTTPStatus(410))

resp = routerRequest(routerPort, "/foo/bar")
Expect(resp.StatusCode).To(Equal(404))
Expect(readBody(resp)).To(Equal("404 page not found\n"))
resp = routerRequest(routerPort, "/foo/no-match")
Expect(resp).To(HaveHTTPStatus(404))
})

It("should support a prefix gone route", func() {
resp := routerRequest(routerPort, "/bar")
Expect(resp.StatusCode).To(Equal(410))
Expect(readBody(resp)).To(Equal("410 Gone\n"))
Expect(resp).To(HaveHTTPStatus(410))

resp = routerRequest(routerPort, "/bar/baz")
Expect(resp.StatusCode).To(Equal(410))
Expect(readBody(resp)).To(Equal("410 Gone\n"))
Expect(resp).To(HaveHTTPStatus(410))
})
})
5 changes: 1 addition & 4 deletions integration_tests/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package integration

import (
"runtime"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -14,9 +13,7 @@ func TestEverything(t *testing.T) {
}

var _ = BeforeSuite(func() {
runtime.GOMAXPROCS(runtime.NumCPU())
var err error
err = setupTempLogfile()
err := setupTempLogfile()
if err != nil {
Fail(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var _ = Describe("/metrics API endpoint", func() {

BeforeEach(func() {
resp := doRequest(newRequest("GET", routerURL(apiPort, "/metrics")))
Expect(resp.StatusCode).To(Equal(200))
Expect(resp).To(HaveHTTPStatus(200))
responseBody = readBody(resp)
})

Expand Down
Loading
Loading