[XAPID-1002] add more tests
diff --git a/api.go b/api.go index 564dc8b..ef1d3ba 100644 --- a/api.go +++ b/api.go
@@ -376,8 +376,8 @@ reqBody := ®isterBody{} err = json.Unmarshal(bodyBytes, reqBody) if err != nil { - log.Errorf("apiPutRegister error: %v", err) - a.writeError(w, http.StatusInternalServerError, API_ERR_INTERNAL, "Failed to read request body.") + log.Debugf("apiPutRegister error: %v", err) + a.writeError(w, http.StatusBadRequest, API_ERR_INVALID_PARAMETERS, "Failed to read request body json: "+err.Error()) return } @@ -416,8 +416,8 @@ reqBody := &configStatusBody{} err = json.Unmarshal(bodyBytes, reqBody) if err != nil { - log.Errorf("apiPutConfigStatus error: %v", err) - a.writeError(w, http.StatusInternalServerError, API_ERR_INTERNAL, "Failed to read request body.") + log.Debugf("apiPutConfigStatus error: %v", err) + a.writeError(w, http.StatusBadRequest, API_ERR_INVALID_PARAMETERS, "Failed to read request body json: "+err.Error()) return }
diff --git a/api_test.go b/api_test.go index 4cf93c0..86b3f76 100644 --- a/api_test.go +++ b/api_test.go
@@ -278,7 +278,7 @@ }) }) - FContext("Tracking endpoints", func() { + Context("Tracking endpoints", func() { var dummyClient *dummyTrackerClient var testClient *http.Client @@ -525,6 +525,49 @@ } }) + It("/configurations/status should reject invalid json, and marshal valid fields", func() { + // setup test data + dummyClient.code = http.StatusOK + testData := []string{ + "invalid-json", + `{"serviceId":"8eebdb60-be68-4380-a902-8cd0a2a0744c", + "reportedTime":"2017-08-09T13:30:03.987-07:00"}`, + `{"invalid-field1":"8eebdb60-be68-4380-a902-8cd0a2a0744c", + "invalid-field2":"2017-08-09T13:30:03.987-07:00"}`, + } + + expectedCode := []int{ + http.StatusBadRequest, + http.StatusOK, + http.StatusBadRequest, + } + + expectedBody := []string{ + "json", + "", + "", + } + + // setup http client + uri, err := url.Parse(apiTestUrl) + Expect(err).Should(Succeed()) + for i, data := range testData { + uri.Path = testApiMan.configStatusEndpoint + log.Debug(uri.String(), data) + req, err := http.NewRequest("PUT", uri.String(), strings.NewReader(data)) + Expect(err).Should(Succeed()) + // http put + res, err := testClient.Do(req) + Expect(err).Should(Succeed()) + // parse response + defer res.Body.Close() + Expect(res.StatusCode).Should(Equal(expectedCode[i])) + body, err := ioutil.ReadAll(res.Body) + Expect(err).Should(Succeed()) + Expect(strings.Contains(strings.ToLower(string(body)), strings.ToLower(expectedBody[i]))).To(BeTrue()) + } + }) + It("/configurations/status should populate errors from tracker", func() { // setup test data testData := [][]string{ @@ -804,7 +847,6 @@ } func generateStatusDetails(flag int) (*statusDetailsJson, string) { - log.Warnf("flag: %v", flag) id := GenerateUUID() var ret *statusDetailsJson var expected string