diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 55dd816bd..d00491640 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -313,17 +313,25 @@ func (api *API) sourceNewHandler(writer http.ResponseWriter, request *http.Reque } contentType := request.Header["Content-Type"] - if len(contentType) != 1 || contentType[0] != "application/json" { + if len(contentType) == 0 { errors := responseError{ ID: "HTTPError", - Msg: "Internal Server Error", + Msg: "malformed Content-Type header", } - statusResponseError(writer, http.StatusInternalServerError, errors) + statusResponseError(writer, http.StatusBadRequest, errors) return } var source store.SourceConfig - err := json.NewDecoder(request.Body).Decode(&source) + var err error + if contentType[0] == "application/json" { + err = json.NewDecoder(request.Body).Decode(&source) + } else if contentType[0] == "text/x-toml" { + _, err = toml.DecodeReader(request.Body, &source) + } else { + err = errors.New("blueprint must be in json or toml format") + } + if err != nil { errors := responseError{ Code: http.StatusBadRequest, @@ -1052,7 +1060,7 @@ func (api *API) blueprintsNewHandler(writer http.ResponseWriter, request *http.R } contentType := request.Header["Content-Type"] - if len(contentType) != 1 { + if len(contentType) == 0 { errors := responseError{ ID: "BlueprintsError", Msg: "missing Content-Type header", diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index 8c0e1020b..a04a27ea3 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -594,6 +594,29 @@ func TestSourcesNew(t *testing.T) { } } +func TestSourcesNewToml(t *testing.T) { + source := ` +name = "fish" +url = "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/" +type = "yum-baseurl" +check_ssl = false +check_gpg = false +` + req := httptest.NewRequest("POST", "/api/v0/projects/source/new", bytes.NewReader([]byte(source))) + 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) + } + + test.SendHTTP(api, true, "DELETE", "/api/v0/projects/source/delete/fish", ``) +} + func TestSourcesDelete(t *testing.T) { var cases = []struct { Method string