Skip to content

Commit 6cad27e

Browse files
committed
[XAPID-1002] add more tests
1 parent 4feedec commit 6cad27e

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ func (a *apiManager) apiPutRegister(w http.ResponseWriter, r *http.Request) {
376376
reqBody := &registerBody{}
377377
err = json.Unmarshal(bodyBytes, reqBody)
378378
if err != nil {
379-
log.Errorf("apiPutRegister error: %v", err)
380-
a.writeError(w, http.StatusInternalServerError, API_ERR_INTERNAL, "Failed to read request body.")
379+
log.Debugf("apiPutRegister error: %v", err)
380+
a.writeError(w, http.StatusBadRequest, API_ERR_INVALID_PARAMETERS, "Failed to read request body json: "+err.Error())
381381
return
382382
}
383383

@@ -416,8 +416,8 @@ func (a *apiManager) apiPutConfigStatus(w http.ResponseWriter, r *http.Request)
416416
reqBody := &configStatusBody{}
417417
err = json.Unmarshal(bodyBytes, reqBody)
418418
if err != nil {
419-
log.Errorf("apiPutConfigStatus error: %v", err)
420-
a.writeError(w, http.StatusInternalServerError, API_ERR_INTERNAL, "Failed to read request body.")
419+
log.Debugf("apiPutConfigStatus error: %v", err)
420+
a.writeError(w, http.StatusBadRequest, API_ERR_INVALID_PARAMETERS, "Failed to read request body json: "+err.Error())
421421
return
422422
}
423423

api_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ var _ = Describe("api", func() {
278278
})
279279
})
280280

281-
FContext("Tracking endpoints", func() {
281+
Context("Tracking endpoints", func() {
282282
var dummyClient *dummyTrackerClient
283283
var testClient *http.Client
284284

@@ -525,6 +525,49 @@ var _ = Describe("api", func() {
525525
}
526526
})
527527

528+
It("/configurations/status should reject invalid json, and marshal valid fields", func() {
529+
// setup test data
530+
dummyClient.code = http.StatusOK
531+
testData := []string{
532+
"invalid-json",
533+
`{"serviceId":"8eebdb60-be68-4380-a902-8cd0a2a0744c",
534+
"reportedTime":"2017-08-09T13:30:03.987-07:00"}`,
535+
`{"invalid-field1":"8eebdb60-be68-4380-a902-8cd0a2a0744c",
536+
"invalid-field2":"2017-08-09T13:30:03.987-07:00"}`,
537+
}
538+
539+
expectedCode := []int{
540+
http.StatusBadRequest,
541+
http.StatusOK,
542+
http.StatusBadRequest,
543+
}
544+
545+
expectedBody := []string{
546+
"json",
547+
"",
548+
"",
549+
}
550+
551+
// setup http client
552+
uri, err := url.Parse(apiTestUrl)
553+
Expect(err).Should(Succeed())
554+
for i, data := range testData {
555+
uri.Path = testApiMan.configStatusEndpoint
556+
log.Debug(uri.String(), data)
557+
req, err := http.NewRequest("PUT", uri.String(), strings.NewReader(data))
558+
Expect(err).Should(Succeed())
559+
// http put
560+
res, err := testClient.Do(req)
561+
Expect(err).Should(Succeed())
562+
// parse response
563+
defer res.Body.Close()
564+
Expect(res.StatusCode).Should(Equal(expectedCode[i]))
565+
body, err := ioutil.ReadAll(res.Body)
566+
Expect(err).Should(Succeed())
567+
Expect(strings.Contains(strings.ToLower(string(body)), strings.ToLower(expectedBody[i]))).To(BeTrue())
568+
}
569+
})
570+
528571
It("/configurations/status should populate errors from tracker", func() {
529572
// setup test data
530573
testData := [][]string{
@@ -804,7 +847,6 @@ func GenerateUUID() string {
804847
}
805848

806849
func generateStatusDetails(flag int) (*statusDetailsJson, string) {
807-
log.Warnf("flag: %v", flag)
808850
id := GenerateUUID()
809851
var ret *statusDetailsJson
810852
var expected string

0 commit comments

Comments
 (0)