weldr: Return an error when an empty blueprint is received

The JSON and TOML parsers differ in how they handle an empty body so
check for a ContentLength of zero first and return a "Missing
blueprint" error to the client.

Includes updated tests for the JSON path, and new tests for empty TOML
blueprints.
This commit is contained in:
Brian C. Lane 2020-03-11 15:35:32 -07:00 committed by Tom Gundersen
parent 77fd2a0d8b
commit 7bd020ca11
2 changed files with 48 additions and 2 deletions

View file

@ -1162,6 +1162,15 @@ func (api *API) blueprintsNewHandler(writer http.ResponseWriter, request *http.R
return
}
if request.ContentLength == 0 {
errors := responseError{
ID: "BlueprintsError",
Msg: "Missing blueprint",
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
var blueprint blueprint.Blueprint
var err error
if contentType[0] == "application/json" {
@ -1210,6 +1219,15 @@ func (api *API) blueprintsWorkspaceHandler(writer http.ResponseWriter, request *
return
}
if request.ContentLength == 0 {
errors := responseError{
ID: "BlueprintsError",
Msg: "Missing blueprint",
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
var blueprint blueprint.Blueprint
var err error
if contentType[0] == "application/json" {

View file

@ -74,7 +74,7 @@ func TestBlueprintsNew(t *testing.T) {
{"POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[],"version":""}`, http.StatusOK, `{"status":true}`},
{"POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`, http.StatusOK, `{"status":true}`},
{"POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages:}`, http.StatusBadRequest, `{"status":false,"errors":[{"id":"BlueprintsError","msg":"400 Bad Request: The browser (or proxy) sent a request that this server could not understand: unexpected EOF"}]}`},
{"POST", "/api/v0/blueprints/new", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"BlueprintsError","msg":"400 Bad Request: The browser (or proxy) sent a request that this server could not understand: EOF"}]}`},
{"POST", "/api/v0/blueprints/new", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"BlueprintsError","msg":"Missing blueprint"}]}`},
}
for _, c := range cases {
@ -106,6 +106,20 @@ version = "2.4.*"`
}
}
func TestBlueprintsEmptyToml(t *testing.T) {
req := httptest.NewRequest("POST", "/api/v0/blueprints/new", bytes.NewReader(nil))
req.Header.Set("Content-Type", "text/x-toml")
recorder := httptest.NewRecorder()
api, _ := createWeldrAPI(rpmmd_mock.BaseFixture)
api.ServeHTTP(recorder, req)
r := recorder.Result()
if r.StatusCode != http.StatusBadRequest {
t.Fatalf("unexpected status %v", r.StatusCode)
}
}
func TestBlueprintsInvalidToml(t *testing.T) {
blueprint := `
name = "test"
@ -139,7 +153,7 @@ func TestBlueprintsWorkspaceJSON(t *testing.T) {
}{
{"POST", "/api/v0/blueprints/workspace", `{"name":"test","description":"Test","packages":[{"name":"systemd","version":"123"}],"version":"0.0.0"}`, http.StatusOK, `{"status":true}`},
{"POST", "/api/v0/blueprints/workspace", `{"name":"test","description":"Test","packages:}`, http.StatusBadRequest, `{"status":false,"errors":[{"id":"BlueprintsError","msg":"400 Bad Request: The browser (or proxy) sent a request that this server could not understand: unexpected EOF"}]}`},
{"POST", "/api/v0/blueprints/workspace", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"BlueprintsError","msg":"400 Bad Request: The browser (or proxy) sent a request that this server could not understand: EOF"}]}`},
{"POST", "/api/v0/blueprints/workspace", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"BlueprintsError","msg":"Missing blueprint"}]}`},
}
for _, c := range cases {
@ -171,6 +185,20 @@ version = "2.4.*"`
}
}
func TestBlueprintsWorkspaceEmptyTOML(t *testing.T) {
req := httptest.NewRequest("POST", "/api/v0/blueprints/workspace", bytes.NewReader(nil))
req.Header.Set("Content-Type", "text/x-toml")
recorder := httptest.NewRecorder()
api, _ := createWeldrAPI(rpmmd_mock.BaseFixture)
api.ServeHTTP(recorder, req)
r := recorder.Result()
if r.StatusCode != http.StatusBadRequest {
t.Fatalf("unexpected status %v", r.StatusCode)
}
}
func TestBlueprintsWorkspaceInvalidTOML(t *testing.T) {
blueprint := `
name = "test"