Skip to content

Commit

Permalink
Added catch for missing POST body when posted to profileparameters (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
kdamichie committed Nov 22, 2022
1 parent 3ff8ae5 commit e82501c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions traffic_ops/testing/api/v3/profile_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const queryParamFormat = "?profileId=%s&parameterId=%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)

Expand Down Expand Up @@ -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)()
Expand Down
12 changes: 12 additions & 0 deletions traffic_ops/testing/api/v4/profile_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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() {

Expand Down
12 changes: 12 additions & 0 deletions traffic_ops/testing/api/v5/profile_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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() {

Expand Down
5 changes: 5 additions & 0 deletions traffic_ops/traffic_ops_golang/api/shared_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e82501c

Please sign in to comment.