From e82501c92f150aa8bc7b8a7dcd80c26adc30a66e Mon Sep 17 00:00:00 2001 From: Kurtis Michie <49660055+kdamichie@users.noreply.github.com> Date: Tue, 22 Nov 2022 15:53:40 -0700 Subject: [PATCH] Added catch for missing POST body when posted to profileparameters (#7194) * Added catch for missing POST body when posted to profileparameters * Added test for missing POST body to profileparameters into v3 and v5, and added change into changelog.md * Delete traffic-ops-test.conf * Accidentally deleted. Meant to revert. Adding back in. * Update traffic-ops-test.conf Added new line to end of file * Created one off test to check POST for an empty JSON body * Corrected correct URL Path for versions 3.0 and 4.0 --- CHANGELOG.md | 1 + .../testing/api/v3/profile_parameters_test.go | 12 ++++++++++++ .../testing/api/v4/profile_parameters_test.go | 12 ++++++++++++ .../testing/api/v5/profile_parameters_test.go | 12 ++++++++++++ .../traffic_ops_golang/api/shared_handlers.go | 5 +++++ 5 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 910320a44c..50e949a7a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Switched Delivery Service active state to a three-value system, adding a state that will be used to prevent cache servers from deploying DS configuration. ### Fixed +- [#4428](https://github.com/apache/trafficcontrol/issues/4428) *Traffic Ops* Fixed Internal Server Error with POST to `profileparameters` when POST body is empty - [#7179](https://github.com/apache/trafficcontrol/issues/7179) *Traffic Portal* Fixed search filter for Delivery Service Table - [#7174](https://github.com/apache/trafficcontrol/issues/7174) *Traffic Portal* Fixed topologies sort (table and Delivery Service's form) - [#5970](https://github.com/apache/trafficcontrol/issues/5970) *Traffic Portal* Fixed numeric sort in Delivery Service's form for DSCP diff --git a/traffic_ops/testing/api/v3/profile_parameters_test.go b/traffic_ops/testing/api/v3/profile_parameters_test.go index b2931a7cec..fe73aa262b 100644 --- a/traffic_ops/testing/api/v3/profile_parameters_test.go +++ b/traffic_ops/testing/api/v3/profile_parameters_test.go @@ -34,6 +34,8 @@ const queryParamFormat = "?profileId=%s¶meterId=%s" func TestProfileParameters(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() { + // This is a one off test to check POST with an empty JSON body + TestPostWithEmptyBody(t) currentTime := time.Now().UTC().Add(-15 * time.Second) tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123) @@ -163,6 +165,16 @@ func TestProfileParameters(t *testing.T) { }) } +func TestPostWithEmptyBody(t *testing.T) { + resp, err := TOSession.Client.Post(TOSession.URL+"/api/3.0/profileparameters", "application/json", nil) + if err != nil { + t.Fatalf("error sending post to create profile parameter with an empty body: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected to get a 400 error code, but received %d instead", resp.StatusCode) + } +} + func CreateTestProfileParameters(t *testing.T) { for _, profile := range testData.Profiles { profileID := GetProfileID(t, profile.Name)() diff --git a/traffic_ops/testing/api/v4/profile_parameters_test.go b/traffic_ops/testing/api/v4/profile_parameters_test.go index efa620533e..23365c12f0 100644 --- a/traffic_ops/testing/api/v4/profile_parameters_test.go +++ b/traffic_ops/testing/api/v4/profile_parameters_test.go @@ -33,6 +33,8 @@ import ( func TestProfileParameters(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() { + // This is a one off test to check POST with an empty JSON body + TestPostWithEmptyBody(t) currentTime := time.Now().UTC().Add(-15 * time.Second) tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123) @@ -172,6 +174,16 @@ func TestProfileParameters(t *testing.T) { }) } +func TestPostWithEmptyBody(t *testing.T) { + resp, err := TOSession.Client.Post(TOSession.URL+"/api/4.0/profileparameters", "application/json", nil) + if err != nil { + t.Fatalf("error sending post to create profile parameter with an empty body: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected to get a 400 error code, but received %d instead", resp.StatusCode) + } +} + func TestProfileParameter(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles}, func() { diff --git a/traffic_ops/testing/api/v5/profile_parameters_test.go b/traffic_ops/testing/api/v5/profile_parameters_test.go index e44ebd7831..e2b264a231 100644 --- a/traffic_ops/testing/api/v5/profile_parameters_test.go +++ b/traffic_ops/testing/api/v5/profile_parameters_test.go @@ -33,6 +33,8 @@ import ( func TestProfileParameters(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() { + // This is a one off test to check POST with an empty JSON body + TestPostWithEmptyBody(t) currentTime := time.Now().UTC().Add(-15 * time.Second) tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123) @@ -172,6 +174,16 @@ func TestProfileParameters(t *testing.T) { }) } +func TestPostWithEmptyBody(t *testing.T) { + resp, err := TOSession.Client.Post(TOSession.URL+"/api/5.0/profileparameters", "application/json", nil) + if err != nil { + t.Fatalf("error sending post to create profile parameter with an empty body: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected to get a 400 error code, but received %d instead", resp.StatusCode) + } +} + func TestProfileParameter(t *testing.T) { WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles}, func() { diff --git a/traffic_ops/traffic_ops_golang/api/shared_handlers.go b/traffic_ops/traffic_ops_golang/api/shared_handlers.go index 3fe6e786d7..2f7e139801 100644 --- a/traffic_ops/traffic_ops_golang/api/shared_handlers.go +++ b/traffic_ops/traffic_ops_golang/api/shared_handlers.go @@ -495,6 +495,11 @@ func CreateHandler(creator Creator) http.HandlerFunc { return } + if len(data) == 0 { + HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, errors.New("no request body supplied"), nil) + return + } + objSlice, err := parseMultipleCreates(data, objectType, inf) if err != nil { HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, err)