Added validation on client_received_end_timestamp
diff --git a/api_helper.go b/api_helper.go index 45e99f0..ee65631 100644 --- a/api_helper.go +++ b/api_helper.go
@@ -101,11 +101,11 @@ /* Does basic validation on each analytics message -1. client_received_start_timestamp should exist -2. if client_received_end_timestamp exists then it should be > client_received_start_timestamp +1. client_received_start_timestamp, client_received_end_timestamp should exist +2. client_received_end_timestamp should be > client_received_start_timestamp and not 0 */ func validate(recordMap map[string]interface{}) (bool, errResponse) { - elems := []string{"client_received_start_timestamp"} + elems := []string{"client_received_start_timestamp", "client_received_end_timestamp"} for _, elem := range elems { if recordMap[elem] == nil { return false, errResponse{ @@ -117,7 +117,12 @@ crst, exists1 := recordMap["client_received_start_timestamp"] cret, exists2 := recordMap["client_received_end_timestamp"] if exists1 && exists2 { - if crst.(json.Number) > cret.(json.Number) { + if crst.(json.Number) == json.Number("0") || cret.(json.Number) == json.Number("0") { + return false, errResponse{ + ErrorCode: "BAD_DATA", + Reason: "client_received_start_timestamp or " + + "> client_received_end_timestamp cannot be 0"} + } else if crst.(json.Number) > cret.(json.Number) { return false, errResponse{ ErrorCode: "BAD_DATA", Reason: "client_received_start_timestamp " +
diff --git a/api_helper_test.go b/api_helper_test.go index 7b83679..4304769 100644 --- a/api_helper_test.go +++ b/api_helper_test.go
@@ -36,8 +36,23 @@ Expect(valid).To(BeFalse()) Expect(e.ErrorCode).To(Equal("BAD_DATA")) - Expect(e.Reason).To(Equal("client_received_start_timestamp > client_received_end_timestamp")) + Expect(e.Reason).To(Equal("client_received_start_timestamp " + + "> client_received_end_timestamp")) + By("payload with clst = 0") + record = []byte(`{ + "response_status_code": 200, + "client_id":"testapikey", + "client_received_start_timestamp": 0, + "client_received_end_timestamp": 1486406248260 + }`) + raw = getRaw(record) + valid, e = validate(raw) + + Expect(valid).To(BeFalse()) + Expect(e.ErrorCode).To(Equal("BAD_DATA")) + Expect(e.Reason).To(Equal("client_received_start_timestamp or " + + "> client_received_end_timestamp cannot be 0")) }) }) Context("valid record", func() {