From 8c19364b65f979fcffcc95c42880add42a68b988 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 5 Feb 2020 16:11:12 -0800 Subject: [PATCH] api: Add TOML support for workspace POST composer-cli pushes blueprints to the workspace using TOML not JSON. This also adds a test. --- internal/weldr/api.go | 16 ++++++++++++---- internal/weldr/api_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/internal/weldr/api.go b/internal/weldr/api.go index e59e4f8eb..259008c58 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -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 diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index b0cb33a87..ad22e3d9b 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -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