Skip to content
Open
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
14 changes: 14 additions & 0 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ func TestConfig_prefixed_variables_take_precedence_over_non_prefixed(t *testing.
assert.Equal(t, 4000, c.TargetPort)
}

func TestConfig_defaults_are_used_if_strconv_fails(t *testing.T) {
usingProgramArgs(t, "thruster", "echo", "hello")
usingEnvVar(t, "TARGET_PORT", "should-be-an-int")
usingEnvVar(t, "HTTP_IDLE_TIMEOUT", "should-be-a-duration")
usingEnvVar(t, "X_SENDFILE_ENABLED", "should-be-a-bool")

c, err := NewConfig()
require.NoError(t, err)

assert.Equal(t, 3000, c.TargetPort)
assert.Equal(t, 60*time.Second, c.HttpIdleTimeout)
assert.Equal(t, true, c.XSendfileEnabled)
}

func TestConfig_return_error_when_no_upstream_command(t *testing.T) {
usingProgramArgs(t, "thruster")

Expand Down
5 changes: 5 additions & 0 deletions internal/fixtures/502.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!doctype html>
<html lang="en">
<head><title>Something went wrong</title></head>
<body>Something went wrong.</body>
</html>
35 changes: 35 additions & 0 deletions internal/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,41 @@ func TestHandlerMaxRequestBody(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code)
}

func TestHandler_bad_gateway_response(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("backend error")
}))
defer upstream.Close()

options := handlerOptions(upstream.URL)
h := NewHandler(options)

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", bytes.NewReader([]byte("hello there")))
h.ServeHTTP(w, r)
assert.Equal(t, http.StatusBadGateway, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Type"))
assert.Equal(t, "", w.Body.String())
}

func TestHandler_serve_custom_bad_gateway_page(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("backend error")
}))
defer upstream.Close()

options := handlerOptions(upstream.URL)
options.badGatewayPage = fixturePath("502.html")
h := NewHandler(options)

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", bytes.NewReader([]byte("hello there")))
h.ServeHTTP(w, r)
assert.Equal(t, http.StatusBadGateway, w.Code)
assert.Equal(t, "text/html", w.Header().Get("Content-Type"))
assert.Equal(t, string(fixtureContent("502.html")), w.Body.String())
}

func TestHandlerPreserveInboundHostHeaderWhenProxying(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "example.org", r.Host)
Expand Down
16 changes: 16 additions & 0 deletions internal/upstream_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@ func TestUpstreamProcess(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, exitCode)
})

t.Run("return 0 exit code and error when command fails to start", func(t *testing.T) {
p := NewUpstreamProcess("this-is-definitely-missing")
exitCode, err := p.Run()

assert.Error(t, err)
assert.Equal(t, 0, exitCode)
})

t.Run("return 0 exit code when command doesn't error", func(t *testing.T) {
p := NewUpstreamProcess("true")
exitCode, err := p.Run()

assert.NoError(t, err)
assert.Equal(t, 0, exitCode)
})
}