api: Add TOML support for workspace POST

composer-cli pushes blueprints to the workspace using TOML not JSON.
This also adds a test.
This commit is contained in:
Brian C. Lane 2020-02-05 16:11:12 -08:00 committed by Tom Gundersen
parent 8781d41da6
commit 8c19364b65
2 changed files with 36 additions and 5 deletions

View file

@ -1127,21 +1127,29 @@ func (api *API) blueprintsWorkspaceHandler(writer http.ResponseWriter, request *
}
contentType := request.Header["Content-Type"]
if len(contentType) != 1 || contentType[0] != "application/json" {
if len(contentType) == 0 {
errors := responseError{
ID: "BlueprintsError",
Msg: "'blueprint must be json'",
Msg: "missing Content-Type header",
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
var blueprint blueprint.Blueprint
err := json.NewDecoder(request.Body).Decode(&blueprint)
var err error
if contentType[0] == "application/json" {
err = json.NewDecoder(request.Body).Decode(&blueprint)
} else if contentType[0] == "text/x-toml" {
_, err = toml.DecodeReader(request.Body, &blueprint)
} else {
err = errors.New("blueprint must be in json or toml format")
}
if err != nil {
errors := responseError{
ID: "BlueprintsError",
Msg: "400 Bad Request: The browser (or proxy) sent a request that this server could not understand.",
Msg: "400 Bad Request: The browser (or proxy) sent a request that this server could not understand: " + err.Error(),
}
statusResponseError(writer, http.StatusBadRequest, errors)
return

View file

@ -101,7 +101,7 @@ version = "2.4.*"`
}
}
func TestBlueprintsWorkspace(t *testing.T) {
func TestBlueprintsWorkspaceJSON(t *testing.T) {
var cases = []struct {
Method string
Path string
@ -119,6 +119,29 @@ func TestBlueprintsWorkspace(t *testing.T) {
}
}
func TestBlueprintsWorkspaceTOML(t *testing.T) {
blueprint := `
name = "test"
description = "Test"
version = "0.0.0"
[[packages]]
name = "httpd"
version = "2.4.*"`
req := httptest.NewRequest("POST", "/api/v0/blueprints/workspace", bytes.NewReader([]byte(blueprint)))
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.StatusOK {
t.Fatalf("unexpected status %v", r.StatusCode)
}
}
func TestBlueprintsInfo(t *testing.T) {
var cases = []struct {
Method string