Repository: avenga/couper
Pull request #453 (2022-03-15 18:26:16)

Fix missing beresp body read
Fix missing beresp body read for beresp-var reference within backend block

Fixup for #430 

---
<details>
    <summary>Reviewer checklist</summary>
    <ul>
        <li>Read PR description: a summary about the changes is required</li>
        <li>Changelog updated</li>
        <li>Documentation: docs/{Reference, Cli, ...}, Docker and cli help/usage</li>
        <li>Pulled branch, manually tested</li>
        <li>Verified requirements are met</li>
        <li>Reviewed the code</li>
        <li>Reviewed the related tests</li>
    </ul>
</details>



Patch:
diff --git a/eval/context.go b/eval/context.go
index 031cca0f..09bbc1d9 100644
--- a/eval/context.go
+++ b/eval/context.go
@@ -166,7 +166,7 @@ func (c *Context) WithClientRequest(req *http.Request) *Context {
 	return ctx
 }
 
-func (c *Context) WithBeresp(beresp *http.Response) *Context {
+func (c *Context) WithBeresp(beresp *http.Response, readBody bool) *Context {
 	ctx := &Context{
 		eval:              c.cloneEvalContext(),
 		inner:             c.inner,
@@ -182,7 +182,7 @@ func (c *Context) WithBeresp(beresp *http.Response) *Context {
 	bereqs := make(ContextMap)
 
 	if beresp != nil {
-		name, bereqVal, berespVal := newBerespValues(ctx, false, beresp)
+		name, bereqVal, berespVal := newBerespValues(ctx, readBody, beresp)
 		bereqs[name] = bereqVal
 		resps[name] = berespVal
 
diff --git a/handler/transport/backend.go b/handler/transport/backend.go
index 36b69595..d5ec1c85 100644
--- a/handler/transport/backend.go
+++ b/handler/transport/backend.go
@@ -160,7 +160,9 @@ func (b *Backend) RoundTrip(req *http.Request) (*http.Response, error) {
 	// to the current beresp obj. Downstream response context evals reading their beresp variable values
 	// from this result.
 	evalCtx := eval.ContextFromRequest(req)
-	evalCtx = evalCtx.WithBeresp(beresp)
+	// has own body variable reference?
+	readBody := eval.MustBuffer(b.context)&eval.BufferResponse == eval.BufferResponse
+	evalCtx = evalCtx.WithBeresp(beresp, readBody)
 	err = eval.ApplyResponseContext(evalCtx.HCLContext(), b.context, beresp)
 
 	if varSync, ok := req.Context().Value(request.ContextVariablesSynced).(*eval.SyncedVariables); ok {


Test patch:
diff --git a/eval/context_test.go b/eval/context_test.go
index 5385ea0d..c5c52436 100644
--- a/eval/context_test.go
+++ b/eval/context_test.go
@@ -99,7 +99,7 @@ func TestNewHTTPContext(t *testing.T) {
 
 			helper.Must(eval.SetGetBody(req, eval.BufferRequest, 512))
 
-			ctx := baseCtx.WithClientRequest(req).WithBeresp(beresp).HCLContext()
+			ctx := baseCtx.WithClientRequest(req).WithBeresp(beresp, false).HCLContext()
 			ctx.Functions = nil // we are not interested in a functions test
 
 			var resultMap map[string]cty.Value
diff --git a/eval/lib/jwt_test.go b/eval/lib/jwt_test.go
index 86d9a9d3..7218b392 100644
--- a/eval/lib/jwt_test.go
+++ b/eval/lib/jwt_test.go
@@ -633,7 +633,7 @@ func TestJwtSignDynamic(t *testing.T) {
 
 			evalCtx := cf.Context.Value(request.ContextType).(*eval.Context).
 				WithClientRequest(req).
-				WithBeresp(beresp)
+				WithBeresp(beresp, false)
 
 			now := time.Now().Unix()
 			token, err := evalCtx.HCLContext().Functions[lib.FnJWTSign].Call([]cty.Value{cty.StringVal(tt.jspLabel), claims})
diff --git a/eval/lib/oauth2_test.go b/eval/lib/oauth2_test.go
index 1fa46d50..c188aa9b 100644
--- a/eval/lib/oauth2_test.go
+++ b/eval/lib/oauth2_test.go
@@ -135,7 +135,7 @@ func TestNewOAuthAuthorizationUrlFunction(t *testing.T) {
 			ctx := eval.NewContext(nil, &config.Defaults{}).
 				WithOidcConfig(oidc.Configs{conf.Name: conf}).
 				WithClientRequest(req).
-				WithBeresp(res)
+				WithBeresp(res, false)
 
 			hclCtx := ctx.HCLContext()
 			val, err := hclCtx.Functions[lib.FnOAuthAuthorizationUrl].Call([]cty.Value{cty.StringVal("auth-ref")})
diff --git a/server/http_endpoints_test.go b/server/http_endpoints_test.go
index 879df7c1..10c6dc4e 100644
--- a/server/http_endpoints_test.go
+++ b/server/http_endpoints_test.go
@@ -46,8 +46,8 @@ func TestBackend_BackendVariable_RequestResponse(t *testing.T) {
 		res.Header.Get("X-From-Request-Json-Body") != "1" ||
 		res.Header.Get("X-From-Requests-Header") != "bar" ||
 		res.Header.Get("X-From-Requests-Json-Body") != "1" ||
-		// res.Header.Get("X-From-Response-Json-Body") != "/anything" || // not yet
-		// res.Header.Get("X-From-Responses-Json-Body") != "/anything" || // not yet
+		res.Header.Get("X-From-Response-Json-Body") != "/anything" ||
+		res.Header.Get("X-From-Responses-Json-Body") != "/anything" ||
 		res.Header.Get("X-From-Response-Header") != "application/json" ||
 		res.Header.Get("X-From-Responses-Header") != "application/json" {
 		t.Errorf("Unexpected header given: %#v", res.Header)
@@ -71,8 +71,7 @@ func TestBackend_BackendVariable_RequestResponse(t *testing.T) {
 				data["x-from-response-json-body"] != "the_access_token" {
 				t.Errorf("Unexpected logs given: %#v", data)
 			}
-			// if responseHeaders["location"] != "Basic cXBlYjpiZW4=|client_credentials|60s|the_access_token" { // not yet
-			if responseHeaders["location"] != "Basic cXBlYjpiZW4=|client_credentials|60s|" {
+			if responseHeaders["location"] != "Basic cXBlYjpiZW4=|client_credentials|60s|the_access_token" {
 				t.Errorf("Unexpected responseHeaders given: %#v", responseHeaders)
 			}
 		} else {