Skip to content

Commit

Permalink
added more test cases for set_response_headers/custom_log_fields in b…
Browse files Browse the repository at this point in the history
…ackend/oauth2/backend
  • Loading branch information
Johannes Koch committed Feb 16, 2022
1 parent e99ca67 commit 9c2b25e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
35 changes: 25 additions & 10 deletions server/http_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

const testdataPath = "testdata/endpoints"

func TestBackend_BackendVariable_Request(t *testing.T) {
func TestBackend_BackendVariable_RequestResponse(t *testing.T) {
client := newClient()
helper := test.New(t)

Expand Down Expand Up @@ -58,17 +58,32 @@ func TestBackend_BackendVariable_Request(t *testing.T) {
continue
}

responseHeaders := entry.Data["response"].(logging.Fields)["headers"].(map[string]string)
data := entry.Data["custom"].(logrus.Fields)

if data["x-from-request-json-body"] != float64(1) ||
data["x-from-request-header"] != "bar" ||
data["x-from-requests-json-body"] != float64(1) ||
data["x-from-requests-header"] != "bar" ||
data["x-from-response-header"] != "application/json" ||
data["x-from-response-json-body"] != "/anything" ||
data["x-from-responses-header"] != "application/json" ||
data["x-from-responses-json-body"] != "/anything" {
t.Errorf("Unexpected logs given: %#v", data)
url := entry.Data["url"]
if url == "http://localhost:8081/token" {
if data["x-from-request-body"] != "grant_type=client_credentials" ||
// data["x-from-request-form-body"] != "client_credentials" || // not yet
data["x-from-request-header"] != "Basic cXBlYjpiZW4=" ||
data["x-from-response-header"] != "60s" ||
data["x-from-response-body"] != `{"access_token":"the_access_token","expires_in":60}` ||
data["x-from-response-json-body"] != "the_access_token" ||
responseHeaders["location"] != "Basic cXBlYjpiZW4=|60s||" {
// responseHeaders["location"] != "Basic cXBlYjpiZW4=|60s|client_credentials|the_access_token" { // not yet
t.Errorf("Unexpected logs given: %#v", data)
}
} else {
if data["x-from-request-json-body"] != float64(1) ||
data["x-from-request-header"] != "bar" ||
data["x-from-requests-json-body"] != float64(1) ||
data["x-from-requests-header"] != "bar" ||
data["x-from-response-header"] != "application/json" ||
data["x-from-response-json-body"] != "/anything" ||
data["x-from-responses-header"] != "application/json" ||
data["x-from-responses-json-body"] != "/anything" {
t.Errorf("Unexpected logs given: %#v", data)
}
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions server/testdata/integration/backend/01_couper.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
server {
hosts = ["*:8080"]
endpoint "/" {
proxy "default" {
url = "${env.COUPER_TEST_BACKEND_ADDR}/anything"
Expand Down Expand Up @@ -71,6 +72,27 @@ server {
x-from-response-json-body = backend_response.json_body.Url
x-from-responses-json-body = backend_responses.default.json_body.Url
}
oauth2 {
grant_type = "client_credentials"
token_endpoint = "http://localhost:8081/token"
client_id = "qpeb"
client_secret = "ben"
backend {
set_response_headers = {
# use a response header field that is actually logged
location = "${backend_request.headers.authorization}|${backend_response.headers.x-pires-in}||"
# location = "${backend_request.headers.authorization}|${backend_response.headers.x-pires-in}|${backend_request.form_body.grant_type[0]}|${backend_response.json_body.access_token}"
}
custom_log_fields = {
x-from-request-header = backend_request.headers.authorization
x-from-request-body = backend_request.body
x-from-request-form-body = backend_request.form_body.grant_type[0]
x-from-response-header = backend_response.headers.x-pires-in
x-from-response-body = backend_response.body
x-from-response-json-body = backend_response.json_body.access_token
}
}
}
}
}
}
Expand All @@ -85,3 +107,20 @@ definitions {
}
}
}

server "as" {
hosts = ["*:8081"]
api {
endpoint "/token" {
response {
headers = {
x-pires-in = "60s"
}
json_body = {
access_token = "the_access_token"
expires_in = 60
}
}
}
}
}

0 comments on commit 9c2b25e

Please sign in to comment.