From 7d88240af0a19905c2a69865e22234a58ed05bd7 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 2 Jun 2021 09:34:08 +0200 Subject: [PATCH 01/15] Add the set_response_status attribute --- config/backend.go | 11 ++++++----- config/endpoint.go | 5 +++-- config/error_handler.go | 5 +++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/config/backend.go b/config/backend.go index 7bc6d7994..2a5ea9fac 100644 --- a/config/backend.go +++ b/config/backend.go @@ -50,11 +50,12 @@ func (b Backend) Schema(inline bool) *hcl.BodySchema { type Inline struct { meta.Attributes - BasicAuth string `hcl:"basic_auth,optional"` - Hostname string `hcl:"hostname,optional"` - Origin string `hcl:"origin,optional"` - PathPrefix string `hcl:"path_prefix,optional"` - ProxyURL string `hcl:"proxy,optional"` + BasicAuth string `hcl:"basic_auth,optional"` + Hostname string `hcl:"hostname,optional"` + Origin string `hcl:"origin,optional"` + PathPrefix string `hcl:"path_prefix,optional"` + ProxyURL string `hcl:"proxy,optional"` + ResponseStatus *uint8 `hcl:"set_response_status,optional"` } schema, _ = gohcl.ImpliedBodySchema(&Inline{}) diff --git a/config/endpoint.go b/config/endpoint.go index 77da46867..60ee1dff4 100644 --- a/config/endpoint.go +++ b/config/endpoint.go @@ -40,8 +40,9 @@ func (e Endpoint) Schema(inline bool) *hcl.BodySchema { type Inline struct { meta.Attributes - Requests Requests `hcl:"request,block"` - Proxies Proxies `hcl:"proxy,block"` + Proxies Proxies `hcl:"proxy,block"` + Requests Requests `hcl:"request,block"` + ResponseStatus *uint8 `hcl:"set_response_status,optional"` } schema, _ := gohcl.ImpliedBodySchema(&Inline{}) return schema diff --git a/config/error_handler.go b/config/error_handler.go index e76f46e8f..eede6f83d 100644 --- a/config/error_handler.go +++ b/config/error_handler.go @@ -39,8 +39,9 @@ func (e ErrorHandler) Schema(inline bool) *hcl.BodySchema { type Inline struct { meta.Attributes - Proxies Proxies `hcl:"proxy,block"` - Requests Requests `hcl:"request,block"` + Proxies Proxies `hcl:"proxy,block"` + Requests Requests `hcl:"request,block"` + ResponseStatus *uint8 `hcl:"set_response_status,optional"` } schema, _ := gohcl.ImpliedBodySchema(&Inline{}) From 216e62a79bd1fb8e87c9382c3f8e187d2c7259d5 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 2 Jun 2021 09:35:08 +0200 Subject: [PATCH 02/15] Exit with an error if ApplyResponseContext() fails --- handler/endpoint.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/handler/endpoint.go b/handler/endpoint.go index f47e560ac..48adcf7e9 100644 --- a/handler/endpoint.go +++ b/handler/endpoint.go @@ -166,7 +166,8 @@ func (e *Endpoint) ServeHTTP(rw http.ResponseWriter, req *http.Request) { // always apply before write: redirect, response if err = eval.ApplyResponseContext(evalContext, e.opts.Context, clientres); err != nil { - log.WithError(err).Error() + e.opts.Error.ServeError(err).ServeHTTP(rw, req) + return } select { From 6cc8970dfab4b92df6907a818ba42cc2abed98e1 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 2 Jun 2021 09:35:45 +0200 Subject: [PATCH 03/15] Apply the new attribute, check for errors, clear body for 204 --- eval/http.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/eval/http.go b/eval/http.go index c86beca65..a8f61d9c8 100644 --- a/eval/http.go +++ b/eval/http.go @@ -4,6 +4,7 @@ import ( "bytes" "context" er "errors" + "fmt" "io" "net/http" "net/url" @@ -16,6 +17,7 @@ import ( "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/function/stdlib" + "github.com/avenga/couper/config" "github.com/avenga/couper/config/meta" "github.com/avenga/couper/config/request" "github.com/avenga/couper/errors" @@ -317,6 +319,49 @@ func ApplyResponseContext(ctx context.Context, body hcl.Body, beresp *http.Respo if err != nil { return errors.Evaluation.With(err) } + + content, _, _ = body.PartialContent(config.BackendInlineSchema) + for _, attr := range content.Attributes { + if attr.Name == "set_response_status" { + val, attrDiags := attr.Expr.Value(httpCtx) + if seetie.SetSeverityLevel(attrDiags).HasErrors() { + return attrDiags + } + + status := seetie.ValueToInt(val) + if status < 100 || status > 599 { + return errors.Configuration.With( + fmt.Errorf( + "set_response_status sets an invalid HTTP status code: %d; set the status code to 500", + status, + )) + } + + if status == 204 { + beresp.Request.Context().Value(request.LogEntry).(*logrus.Entry).Warn( + "set_response_status sets the HTTP status code to 204 - removing the response body if any", + ) + + beresp.Body = io.NopCloser(bytes.NewBuffer([]byte{})) + beresp.ContentLength = -1 + + for h := range beresp.Header { + h = strings.ToLower(h) + + if strings.HasPrefix(h, "content-") || h == "x-content-type-options" { + beresp.Header.Del(h) + } + } + + // TODO: Delete/modify more headers, e.g. `Vary: Accept-Encoding`? + } + + beresp.StatusCode = int(status) + + break + } + } + return nil } From 39dec28d2471003ac2eba356ed94bf826a9994a0 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 2 Jun 2021 09:36:15 +0200 Subject: [PATCH 04/15] Docu --- docs/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/README.md b/docs/README.md index 56364a32b..812d1fe6d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -42,6 +42,7 @@ * [Response Header](#response-header) * [Query Parameter](#query-parameter) * [Form Parameter](#form-parameter) + * [Set Response Status](#set-response-status) * [Path Parameter](#path-parameter) * [Access Control](#access-control) * [Error Handler](#error-handler) @@ -677,6 +678,7 @@ The CORS block configures the CORS (Cross-Origin Resource Sharing) behavior in C * [Response Header](#response-header) * [Query Parameter](#query-parameter) * [Form Parameter](#form-parameter) +* [Set Response Status](#set-response-status) #### Request Header @@ -801,6 +803,19 @@ definitions { } ``` +#### Set Response Status + +The `set_response_status` attribute allows to modify the HTTP status code to the +given value. + +| Modifier | Contexts | Description | +|:----------------------|:----------------------------------------------------------------------------------------------------|:------------| +| `set_response_status` | [Endpoint Block](#endpoint-block), [Backend Block](#backend-block), [Error Handler](#error-handler) | HTTP status code to be set to the client response. | + +If the HTTP status code ist set to `204`, the reponse body and all HTTP header +fields related to the body (`Content-Length`, `Content-Encoding` etc.) are removed +from the client response, and a warning is logged. + ### Path Parameter An endpoint label could be defined as `endpoint "/app/{section}/{project}/view" { ... }` From ee3e81812ddcd475409c34237ce4f3d381d41fa9 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 2 Jun 2021 11:09:23 +0200 Subject: [PATCH 05/15] Remove only the C/L header --- eval/http.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/eval/http.go b/eval/http.go index a8f61d9c8..f77b30bc3 100644 --- a/eval/http.go +++ b/eval/http.go @@ -344,16 +344,7 @@ func ApplyResponseContext(ctx context.Context, body hcl.Body, beresp *http.Respo beresp.Body = io.NopCloser(bytes.NewBuffer([]byte{})) beresp.ContentLength = -1 - - for h := range beresp.Header { - h = strings.ToLower(h) - - if strings.HasPrefix(h, "content-") || h == "x-content-type-options" { - beresp.Header.Del(h) - } - } - - // TODO: Delete/modify more headers, e.g. `Vary: Accept-Encoding`? + beresp.Header.Del("Content-Length") } beresp.StatusCode = int(status) From 52013142ac9e696c324a3443e2c2b8fb28d06dd5 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 2 Jun 2021 12:50:47 +0200 Subject: [PATCH 06/15] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05d5476b7..c0b419326 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Unreleased changes are available as `avenga/couper:edge` container. * **Added** * Modifier (`set/add/remove_form_params`) for the form parameters ([#223](https://github.com/avenga/couper/pull/223)) + * Modifier (`set_response_status`) to be able to modify the response HTTP status code ([#250](https://github.com/avenga/couper/pull/250)) * **Changed** * Stronger configuration check for `path` and `path_prefix` attributes, possibly resulting in configuration errors ([#232](https://github.com/avenga/couper/pull/232)) From 87dbe1e2e1f24e9d937449e918c62ce8cb9910a1 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 2 Jun 2021 13:03:32 +0200 Subject: [PATCH 07/15] Fix docu --- docs/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 812d1fe6d..6371c73b8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -812,9 +812,8 @@ given value. |:----------------------|:----------------------------------------------------------------------------------------------------|:------------| | `set_response_status` | [Endpoint Block](#endpoint-block), [Backend Block](#backend-block), [Error Handler](#error-handler) | HTTP status code to be set to the client response. | -If the HTTP status code ist set to `204`, the reponse body and all HTTP header -fields related to the body (`Content-Length`, `Content-Encoding` etc.) are removed -from the client response, and a warning is logged. +If the HTTP status code ist set to `204`, the reponse body and the HTTP header +field `Content-Length` is removed from the client response, and a warning is logged. ### Path Parameter From 4d4c7fc5cca489a8b2b0d63b9b55490c5047e660 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Mon, 7 Jun 2021 11:05:11 +0200 Subject: [PATCH 08/15] Tests --- server/modifier_test.go | 44 +++++++++++++++++++ .../integration/modifier/02_couper.hcl | 18 ++++++++ 2 files changed, 62 insertions(+) create mode 100644 server/testdata/integration/modifier/02_couper.hcl diff --git a/server/modifier_test.go b/server/modifier_test.go index fc8e82cd5..7f0f7cc65 100644 --- a/server/modifier_test.go +++ b/server/modifier_test.go @@ -65,3 +65,47 @@ func TestIntegration_ResponseHeaders(t *testing.T) { }) } } + +func TestIntegration_SetResponseStatus(t *testing.T) { + const confFile = "testdata/integration/modifier/02_couper.hcl" + + client := newClient() + helper := test.New(t) + + shutdown, hook := newCouper(confFile, helper) + defer shutdown() + + // ================================================== 204 >>> + + req, err := http.NewRequest(http.MethodGet, "http://example.com:8080/204", nil) + helper.Must(err) + + hook.Reset() + res, err := client.Do(req) + helper.Must(err) + + if res.StatusCode != 204 { + t.Errorf("Expected status code 204, given: %d", res.StatusCode) + } + + exp := "set_response_status sets the HTTP status code to 204 - removing the response body if any" + if hook.Entries[0].Message != exp { + t.Errorf("Unexpected message given: %s", hook.Entries[0].Message) + } + + // ================================================== 201 >>> + + req, err = http.NewRequest(http.MethodGet, "http://example.com:8080/201", nil) + helper.Must(err) + + hook.Reset() + res, err = client.Do(req) + helper.Must(err) + + if res.StatusCode != 201 { + t.Errorf("Expected status code 201, given: %d", res.StatusCode) + } + if hook.Entries[0].Message != "" { + t.Errorf("Unexpected message given: %s", hook.Entries[0].Message) + } +} diff --git a/server/testdata/integration/modifier/02_couper.hcl b/server/testdata/integration/modifier/02_couper.hcl new file mode 100644 index 000000000..43d5633ba --- /dev/null +++ b/server/testdata/integration/modifier/02_couper.hcl @@ -0,0 +1,18 @@ +server "set-response-status" { + endpoint "/204" { + proxy { + url = "${env.COUPER_TEST_BACKEND_ADDR}/anything" + backend { + set_response_status = 204 + } + } + } + endpoint "/201" { + proxy { + url = "${env.COUPER_TEST_BACKEND_ADDR}/anything" + backend { + set_response_status = 201 + } + } + } +} \ No newline at end of file From 064dca6d8b1c813bae505899ad0f25662055bbab Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Mon, 7 Jun 2021 13:03:28 +0200 Subject: [PATCH 09/15] More tests --- server/modifier_test.go | 18 ++++++++++++++++++ .../integration/modifier/02_couper.hcl | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/server/modifier_test.go b/server/modifier_test.go index 7f0f7cc65..7559dcf00 100644 --- a/server/modifier_test.go +++ b/server/modifier_test.go @@ -108,4 +108,22 @@ func TestIntegration_SetResponseStatus(t *testing.T) { if hook.Entries[0].Message != "" { t.Errorf("Unexpected message given: %s", hook.Entries[0].Message) } + + // ================================================== 600 >>> + + req, err = http.NewRequest(http.MethodGet, "http://example.com:8080/600", nil) + helper.Must(err) + + hook.Reset() + res, err = client.Do(req) + helper.Must(err) + + if res.StatusCode != 500 { + t.Errorf("Expected status code 500, given: %d", res.StatusCode) + } + + exp = "configuration error: set_response_status sets an invalid HTTP status code: 600; set the status code to 500" + if hook.Entries[0].Message != exp { + t.Errorf("Unexpected message given: %s", hook.Entries[0].Message) + } } diff --git a/server/testdata/integration/modifier/02_couper.hcl b/server/testdata/integration/modifier/02_couper.hcl index 43d5633ba..7c49d72ba 100644 --- a/server/testdata/integration/modifier/02_couper.hcl +++ b/server/testdata/integration/modifier/02_couper.hcl @@ -15,4 +15,12 @@ server "set-response-status" { } } } + endpoint "/600" { + proxy { + url = "${env.COUPER_TEST_BACKEND_ADDR}/anything" + backend { + set_response_status = 600 + } + } + } } \ No newline at end of file From 07e1c6c52ebbc3b0aec172488659f59b3a360bf4 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Thu, 10 Jun 2021 11:14:06 +0200 Subject: [PATCH 10/15] Test => table test --- server/modifier_test.go | 82 ++++++++----------- .../integration/modifier/02_couper.hcl | 2 +- 2 files changed, 37 insertions(+), 47 deletions(-) diff --git a/server/modifier_test.go b/server/modifier_test.go index 7559dcf00..002abc611 100644 --- a/server/modifier_test.go +++ b/server/modifier_test.go @@ -72,58 +72,48 @@ func TestIntegration_SetResponseStatus(t *testing.T) { client := newClient() helper := test.New(t) - shutdown, hook := newCouper(confFile, helper) - defer shutdown() - - // ================================================== 204 >>> - - req, err := http.NewRequest(http.MethodGet, "http://example.com:8080/204", nil) - helper.Must(err) - - hook.Reset() - res, err := client.Do(req) - helper.Must(err) - - if res.StatusCode != 204 { - t.Errorf("Expected status code 204, given: %d", res.StatusCode) - } - - exp := "set_response_status sets the HTTP status code to 204 - removing the response body if any" - if hook.Entries[0].Message != exp { - t.Errorf("Unexpected message given: %s", hook.Entries[0].Message) + type testCase struct { + path string + expMessage string + expStatus int } - // ================================================== 201 >>> - - req, err = http.NewRequest(http.MethodGet, "http://example.com:8080/201", nil) - helper.Must(err) - - hook.Reset() - res, err = client.Do(req) - helper.Must(err) - - if res.StatusCode != 201 { - t.Errorf("Expected status code 201, given: %d", res.StatusCode) - } - if hook.Entries[0].Message != "" { - t.Errorf("Unexpected message given: %s", hook.Entries[0].Message) - } + for _, tc := range []testCase{ + { + path: "/204", + expMessage: "set_response_status sets the HTTP status code to 204 - removing the response body if any", + expStatus: 204, + }, + { + path: "/201", + expMessage: "", + expStatus: 201, + }, + { + path: "/600", + expMessage: "configuration error: set_response_status sets an invalid HTTP status code: 600; set the status code to 500", + expStatus: 500, + }, + } { + t.Run(tc.path, func(subT *testing.T) { - // ================================================== 600 >>> + shutdown, hook := newCouper(confFile, helper) + defer shutdown() - req, err = http.NewRequest(http.MethodGet, "http://example.com:8080/600", nil) - helper.Must(err) + req, err := http.NewRequest(http.MethodGet, "http://example.com:8080"+tc.path, nil) + helper.Must(err) - hook.Reset() - res, err = client.Do(req) - helper.Must(err) + hook.Reset() + res, err := client.Do(req) + helper.Must(err) - if res.StatusCode != 500 { - t.Errorf("Expected status code 500, given: %d", res.StatusCode) - } + if res.StatusCode != tc.expStatus { + t.Errorf("Expected status code %d, given: %d", tc.expStatus, res.StatusCode) + } - exp = "configuration error: set_response_status sets an invalid HTTP status code: 600; set the status code to 500" - if hook.Entries[0].Message != exp { - t.Errorf("Unexpected message given: %s", hook.Entries[0].Message) + if hook.Entries[0].Message != tc.expMessage { + t.Errorf("Unexpected message given: %s", hook.Entries[0].Message) + } + }) } } diff --git a/server/testdata/integration/modifier/02_couper.hcl b/server/testdata/integration/modifier/02_couper.hcl index 7c49d72ba..fa4827939 100644 --- a/server/testdata/integration/modifier/02_couper.hcl +++ b/server/testdata/integration/modifier/02_couper.hcl @@ -23,4 +23,4 @@ server "set-response-status" { } } } -} \ No newline at end of file +} From 2f95c218be82a199b904bb384cc8afaa6dabf251 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Thu, 10 Jun 2021 11:26:46 +0200 Subject: [PATCH 11/15] Remove the loop, we need only a single attribute --- eval/http.go | 56 ++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/eval/http.go b/eval/http.go index b4271d134..43f252cf5 100644 --- a/eval/http.go +++ b/eval/http.go @@ -297,41 +297,37 @@ func ApplyResponseContext(ctx context.Context, body hcl.Body, beresp *http.Respo } content, _, _ := body.PartialContent(config.BackendInlineSchema) - for _, attr := range content.Attributes { - if attr.Name == "set_response_status" { - var httpCtx *hcl.EvalContext - if c, ok := ctx.Value(ContextType).(*Context); ok { - httpCtx = c.eval - } - - val, attrDiags := attr.Expr.Value(httpCtx) - if seetie.SetSeverityLevel(attrDiags).HasErrors() { - return attrDiags - } - - status := seetie.ValueToInt(val) - if status < 100 || status > 599 { - return errors.Configuration.With( - fmt.Errorf( - "set_response_status sets an invalid HTTP status code: %d; set the status code to 500", - status, - )) - } + if attr, ok := content.Attributes["set_response_status"]; ok { + var httpCtx *hcl.EvalContext + if c, ok := ctx.Value(ContextType).(*Context); ok { + httpCtx = c.eval + } - if status == 204 { - beresp.Request.Context().Value(request.LogEntry).(*logrus.Entry).Warn( - "set_response_status sets the HTTP status code to 204 - removing the response body if any", - ) + val, attrDiags := attr.Expr.Value(httpCtx) + if seetie.SetSeverityLevel(attrDiags).HasErrors() { + return attrDiags + } - beresp.Body = io.NopCloser(bytes.NewBuffer([]byte{})) - beresp.ContentLength = -1 - beresp.Header.Del("Content-Length") - } + status := seetie.ValueToInt(val) + if status < 100 || status > 599 { + return errors.Configuration.With( + fmt.Errorf( + "set_response_status sets an invalid HTTP status code: %d; set the status code to 500", + status, + )) + } - beresp.StatusCode = int(status) + if status == 204 { + beresp.Request.Context().Value(request.LogEntry).(*logrus.Entry).Warn( + "set_response_status sets the HTTP status code to 204 - removing the response body if any", + ) - break + beresp.Body = io.NopCloser(bytes.NewBuffer([]byte{})) + beresp.ContentLength = -1 + beresp.Header.Del("Content-Length") } + + beresp.StatusCode = int(status) } return nil From 36f158520d83edc0b1eb828f5d6c9961b8d8028f Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 16 Jun 2021 13:20:56 +0200 Subject: [PATCH 12/15] fmt.Errorf => errors.Configuration Co-authored-by: Marcel Ludwig --- eval/http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eval/http.go b/eval/http.go index 43f252cf5..9cead2109 100644 --- a/eval/http.go +++ b/eval/http.go @@ -310,7 +310,7 @@ func ApplyResponseContext(ctx context.Context, body hcl.Body, beresp *http.Respo status := seetie.ValueToInt(val) if status < 100 || status > 599 { - return errors.Configuration.With( + return errors.Configuration.Label("set_response_status").Messagef("invalid http status code: %d", status) fmt.Errorf( "set_response_status sets an invalid HTTP status code: %d; set the status code to 500", status, From d53bdaeab3136008871e3a3469191f8a1dc888f5 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 16 Jun 2021 13:21:57 +0200 Subject: [PATCH 13/15] Format code Co-authored-by: Marcel Ludwig --- eval/http.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eval/http.go b/eval/http.go index 9cead2109..6cf943994 100644 --- a/eval/http.go +++ b/eval/http.go @@ -318,7 +318,9 @@ func ApplyResponseContext(ctx context.Context, body hcl.Body, beresp *http.Respo } if status == 204 { - beresp.Request.Context().Value(request.LogEntry).(*logrus.Entry).Warn( + beresp.Request.Context(). + Value(request.LogEntry).(*logrus.Entry). + Warn("set_response_status: removing body, if any due to status-code 204") "set_response_status sets the HTTP status code to 204 - removing the response body if any", ) From 976caf155d861ff284a7ee2cf9b57450a502a930 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 16 Jun 2021 13:25:06 +0200 Subject: [PATCH 14/15] Change test --- server/modifier_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/modifier_test.go b/server/modifier_test.go index 002abc611..f547f29dc 100644 --- a/server/modifier_test.go +++ b/server/modifier_test.go @@ -69,8 +69,10 @@ func TestIntegration_ResponseHeaders(t *testing.T) { func TestIntegration_SetResponseStatus(t *testing.T) { const confFile = "testdata/integration/modifier/02_couper.hcl" + shutdown, hook := newCouper(confFile, test.New(t)) + defer shutdown() + client := newClient() - helper := test.New(t) type testCase struct { path string @@ -96,9 +98,7 @@ func TestIntegration_SetResponseStatus(t *testing.T) { }, } { t.Run(tc.path, func(subT *testing.T) { - - shutdown, hook := newCouper(confFile, helper) - defer shutdown() + helper := test.New(subT) req, err := http.NewRequest(http.MethodGet, "http://example.com:8080"+tc.path, nil) helper.Must(err) From c037c05bd7648a1170d63af243cbf1ef61461c52 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Wed, 16 Jun 2021 13:30:21 +0200 Subject: [PATCH 15/15] Some fixes --- eval/http.go | 7 ------- server/modifier_test.go | 12 ++++++------ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/eval/http.go b/eval/http.go index 6cf943994..84bacf647 100644 --- a/eval/http.go +++ b/eval/http.go @@ -4,7 +4,6 @@ import ( "bytes" "context" er "errors" - "fmt" "io" "net/http" "net/url" @@ -311,18 +310,12 @@ func ApplyResponseContext(ctx context.Context, body hcl.Body, beresp *http.Respo status := seetie.ValueToInt(val) if status < 100 || status > 599 { return errors.Configuration.Label("set_response_status").Messagef("invalid http status code: %d", status) - fmt.Errorf( - "set_response_status sets an invalid HTTP status code: %d; set the status code to 500", - status, - )) } if status == 204 { beresp.Request.Context(). Value(request.LogEntry).(*logrus.Entry). Warn("set_response_status: removing body, if any due to status-code 204") - "set_response_status sets the HTTP status code to 204 - removing the response body if any", - ) beresp.Body = io.NopCloser(bytes.NewBuffer([]byte{})) beresp.ContentLength = -1 diff --git a/server/modifier_test.go b/server/modifier_test.go index f547f29dc..1d357625f 100644 --- a/server/modifier_test.go +++ b/server/modifier_test.go @@ -10,6 +10,9 @@ import ( func TestIntegration_ResponseHeaders(t *testing.T) { const confFile = "testdata/integration/modifier/01_couper.hcl" + shutdown, _ := newCouper(confFile, test.New(t)) + defer shutdown() + client := newClient() type testCase struct { @@ -46,10 +49,7 @@ func TestIntegration_ResponseHeaders(t *testing.T) { }, } { t.Run(tc.path, func(subT *testing.T) { - helper := test.New(t) - - shutdown, _ := newCouper(confFile, helper) - defer shutdown() + helper := test.New(subT) req, err := http.NewRequest(http.MethodGet, "http://example.com:8080"+tc.path, nil) helper.Must(err) @@ -83,7 +83,7 @@ func TestIntegration_SetResponseStatus(t *testing.T) { for _, tc := range []testCase{ { path: "/204", - expMessage: "set_response_status sets the HTTP status code to 204 - removing the response body if any", + expMessage: "set_response_status: removing body, if any due to status-code 204", expStatus: 204, }, { @@ -93,7 +93,7 @@ func TestIntegration_SetResponseStatus(t *testing.T) { }, { path: "/600", - expMessage: "configuration error: set_response_status sets an invalid HTTP status code: 600; set the status code to 500", + expMessage: "configuration error: set_response_status: invalid http status code: 600", expStatus: 500, }, } {