Skip to content

Commit

Permalink
Get endpoint JSON tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guscarreon committed Jan 7, 2025
1 parent bae814f commit d1fc525
Show file tree
Hide file tree
Showing 31 changed files with 165 additions and 58 deletions.
11 changes: 10 additions & 1 deletion endpoints/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ func TestGetJsonTests(t *testing.T) {

router := httprouter.New()
router.GET("/cache", NewGetHandler(backend, m, tc.HostConfig.AllowSettingKeys, tc.HostConfig.RefererLogRate))
request, err := http.NewRequest("GET", "/cache?"+tc.Query, nil)
request, err := http.NewRequest("GET", "/cache?"+tc.Request.Query, nil)
if !assert.NoError(t, err, "Failed to create a GET request: %v", err) {
hook.Reset()
assert.Nil(t, hook.LastEntry())
continue
}

if len(tc.Request.Headers) > 0 {
for header, values := range tc.Request.Headers {
for _, v := range values {
request.Header.Set(header, v)
}
}
}

rr := httptest.NewRecorder()

// Run test
Expand Down
2 changes: 1 addition & 1 deletion endpoints/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (e *PutHandler) handle(w http.ResponseWriter, r *http.Request, ps httproute
// If incoming request comes with a referer header, there's a e.cfg.refererLogRate percent chance
// getting it logged
if referer := r.Referer(); referer != "" && utils.RandomPick(e.cfg.refererLogRate) {
logrus.Info("PUT request Referer header: " + referer)
logrus.Info("POST request Referer header: " + referer)
}

start := time.Now()
Expand Down
25 changes: 17 additions & 8 deletions endpoints/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ func TestPutJsonTests(t *testing.T) {

router := httprouter.New()
router.POST("/cache", NewPutHandler(backend, m, tc.HostConfig.MaxNumValues, tc.HostConfig.AllowSettingKeys, tc.HostConfig.RefererLogRate))
request, err := http.NewRequest("POST", "/cache", strings.NewReader(string(tc.PutRequest.Body)))
request, err := http.NewRequest("POST", "/cache", strings.NewReader(string(tc.Request.Body)))
if !assert.NoError(t, err, "Failed to create a POST request. Test file: %s Error: %v", testFile, err) {
hook.Reset()
continue
}

if len(tc.Request.Headers) > 0 {
for header, values := range tc.Request.Headers {
for _, v := range values {
request.Header.Set(header, v)
}
}
}

rr := httptest.NewRecorder()

// RUN TEST
Expand Down Expand Up @@ -96,17 +105,17 @@ func TestPutJsonTests(t *testing.T) {
}

type testData struct {
HostConfig hostConfig `json:"config"`
PutRequest testPutRequest `json:"put_request"`
ExpectedOutput expectedOut `json:"expected_output"`
ExpectedLogEntries []logEntry `json:"expected_log_entries"`
ExpectedMetrics []string `json:"expected_metrics"`
Query string `json:"get_request_query"`
HostConfig hostConfig `json:"config"`
Request testRequest `json:"request"`
ExpectedOutput expectedOut `json:"expected_output"`
ExpectedLogEntries []logEntry `json:"expected_log_entries"`
ExpectedMetrics []string `json:"expected_metrics"`
}

type testPutRequest struct {
type testRequest struct {
Body json.RawMessage `json:"body"`
Headers map[string][]string `json:"headers"`
Query string `json:"query"`
}

type expectedOut struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
]
}
},
"get_request_query": "uuid=36-char-uid-maps-to-actual-xml-value",
"request": {
"query": "uuid=36-char-uid-maps-to-actual-xml-value"
},
"expected_log_entries": [
{
"message": "GET /cache uuid=36-char-uid-maps-to-actual-xml-value: Cache data was corrupted. Cannot determine type.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
]
}
},
"get_request_query": "uuid=36-char-uuid-is-not-found-in-backend",
"request": {
"query": "uuid=36-char-uuid-is-not-found-in-backend"
},
"expected_metrics": [
"RecordGetTotal",
"RecordGetBackendError",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
]
}
},
"get_request_query": "uuid=",
"request": {
"query": "uuid="
},
"expected_log_entries": [
{
"message": "GET /cache: Missing required parameter uuid",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"description": "Gut request doesn't come with a UUID value in the URL query, expect MISSING_KEY error",
"get_request_query": "uuid=non-36-char-uuid",
"request": {
"query": "uuid=non-36-char-uuid"
},
"expected_log_entries": [
{
"message": "GET /cache uuid=non-36-char-uuid: invalid uuid length",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
]
}
},
"get_request_query": "uuid=36-char-uid-maps-to-stored-xml-value",
"request": {
"query": "uuid=36-char-uid-maps-to-stored-xml-value"
},
"expected_metrics": [
"RecordGetBackendTotal",
"RecordGetDuration",
Expand All @@ -21,4 +23,4 @@
"code": 200,
"get_response": "<description>stored xml value</description>"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"description": "Prebid Cache configured to log the referer header of 100% of incoming requests. Referer successfully logged.",
"config": {
"fake_backend": {
"stored_data": [
{
"key": "36-char-uid-maps-to-stored-xml-value",
"value": "xml<description>stored xml value</description>"
}
]
},
"referer_sampling_rate": 1.0
},
"request": {
"query": "uuid=36-char-uid-maps-to-stored-xml-value",
"headers": {
"Referer": [ "anyreferer" ]
}
},
"expected_log_entries": [
{
"message": "GET request Referer header: anyreferer",
"level": 4
}
],
"expected_metrics": [
"RecordGetBackendTotal",
"RecordGetDuration",
"RecordGetBackendDuration",
"RecordGetTotal"
],
"expected_output": {
"code": 200,
"get_response": "<description>stored xml value</description>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"server_response": "{\"successStatus\":0,\"error\":\"\",\"response\":false}"
}
},
"put_request": {
"request": {
"body": {
"puts": [
{
Expand All @@ -28,4 +28,4 @@
"code": 200,
"expected_error_message": "{\"responses\":[{\"uuid\":\"\"}]}"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"throw_error_message": "Redis server side error"
}
},
"put_request": {
"request": {
"body": {
"puts": [
{
Expand Down Expand Up @@ -38,4 +38,4 @@
"code": 500,
"expected_error_message": "Redis server side error\n"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
]
}
},
"put_request": {
"request": {
"body": {
"puts": [
{
Expand Down Expand Up @@ -42,4 +42,4 @@
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"throw_error_message": "redis: nil"
}
},
"put_request": {
"request": {
"body": {
"puts": [
{
Expand Down Expand Up @@ -35,4 +35,4 @@
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"allow_setting_keys": true
},
"put_request": {
"request": {
"body": {
"puts": [
{
Expand Down Expand Up @@ -33,4 +33,4 @@
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"allow_setting_keys": true
},
"put_request": {
"request": {
"body": {
"puts": [
{
Expand Down Expand Up @@ -31,4 +31,4 @@
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"description": "Put request wants to store element under a custom key but custom keys are not allowed in Prebid Cache's config. Store under a random UUID",
"put_request": {
"request": {
"body": {
"puts": [
{
Expand Down Expand Up @@ -29,4 +29,4 @@
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"max_num_values": 1
},
"put_request": {
"request": {
"body": {
"puts": [
{
Expand All @@ -27,4 +27,4 @@
"code": 400,
"expected_error_message": "More keys than allowed: 1\n"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"description": "Request is missing the 'type' field. Respond with error",
"put_request": {
"request": {
"body": {
"puts": [
{
Expand All @@ -27,4 +27,4 @@
"code": 400,
"expected_error_message": "Type must be one of [\"json\", \"xml\"]. Found ''\n"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"description": "Prebid Cache only allows to store JSON or XML types and the type 'unknown' is not supported. Respond with error",
"put_request": {
"request": {
"body": {
"puts": [
{
Expand Down Expand Up @@ -28,4 +28,4 @@
"code": 400,
"expected_error_message": "Type must be one of [\"json\", \"xml\"]. Found 'unknown'\n"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"max_size_bytes": 1
},
"put_request": {
"request": {
"body": {
"puts": [
{
Expand Down Expand Up @@ -35,4 +35,4 @@
"code": 400,
"expected_error_message": "POST /cache element 0 exceeded max size: Payload size 73 exceeded max 1\n"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"description": "Prebid Cache returns an error if a request doesn't come with a 'value' field.",
"put_request": {
"request": {
"body": {
"puts": [
{
Expand All @@ -27,4 +27,4 @@
"code": 400,
"expected_error_message": "Missing value.\n"
}
}
}
Loading

0 comments on commit d1fc525

Please sign in to comment.