diff --git a/internal/config_test.go b/internal/config_test.go index c8415d9..a135c78 100644 --- a/internal/config_test.go +++ b/internal/config_test.go @@ -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") diff --git a/internal/fixtures/502.html b/internal/fixtures/502.html new file mode 100644 index 0000000..109e87a --- /dev/null +++ b/internal/fixtures/502.html @@ -0,0 +1,5 @@ + + + Something went wrong + Something went wrong. + diff --git a/internal/handler_test.go b/internal/handler_test.go index a3485b6..bbdfc62 100644 --- a/internal/handler_test.go +++ b/internal/handler_test.go @@ -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) diff --git a/internal/upstream_process_test.go b/internal/upstream_process_test.go index 6dec844..f37c278 100644 --- a/internal/upstream_process_test.go +++ b/internal/upstream_process_test.go @@ -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) + }) }