Skip to content

Commit

Permalink
Merge pull request #11 from sideshow/fix-timestamp
Browse files Browse the repository at this point in the history
Fix Timestamp parsing issue
  • Loading branch information
alissonsales committed Mar 16, 2016
2 parents e445b7d + cb5abc9 commit a6f9928
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
14 changes: 7 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func TestClientBadTransportError(t *testing.T) {
}

func TestClientNameToCertificate(t *testing.T) {
certificate2 := tls.Certificate{}
client2 := apns.NewClient(certificate2)
name2 := client2.HTTPClient.Transport.(*http2.Transport).TLSClientConfig.NameToCertificate
assert.Len(t, name2, 0)

certificate, _ := certificate.FromP12File("certificate/_fixtures/certificate-valid.p12", "")
client := apns.NewClient(certificate)
name := client.HTTPClient.Transport.(*http2.Transport).TLSClientConfig.NameToCertificate
assert.Len(t, name, 1)

certificate2 := tls.Certificate{}
client2 := apns.NewClient(certificate2)
name2 := client2.HTTPClient.Transport.(*http2.Transport).TLSClientConfig.NameToCertificate
assert.Len(t, name2, 0)
}

// Functional Tests
Expand Down Expand Up @@ -182,15 +182,15 @@ func Test410UnregisteredResponse(t *testing.T) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("apns-id", apnsID)
w.WriteHeader(http.StatusGone)
w.Write([]byte("{\"reason\":\"Unregistered\", \"timestamp\":\"1421147681\"}"))
w.Write([]byte("{\"reason\":\"Unregistered\", \"timestamp\": 1458114061260 }"))
}))
defer server.Close()
res, err := mockClient(server.URL).Push(n)
assert.NoError(t, err)
assert.Equal(t, 410, res.StatusCode)
assert.Equal(t, apnsID, res.ApnsID)
assert.Equal(t, apns.ReasonUnregistered, res.Reason)
assert.Equal(t, int64(1421147681), res.Timestamp.Unix())
assert.Equal(t, int64(1458114061260)/1000, res.Timestamp.Unix())
assert.Equal(t, false, res.Sent())
}

Expand Down
7 changes: 2 additions & 5 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,10 @@ type Time struct {
}

func (t *Time) UnmarshalJSON(b []byte) error {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
i, err := strconv.ParseInt(string(b), 10, 64)
ts, err := strconv.Atoi(string(b))
if err != nil {
return err
}
t.Time = time.Unix(i, 0)
t.Time = time.Unix(int64(ts/1000), 0)
return nil
}
11 changes: 2 additions & 9 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@ func TestResponseSent(t *testing.T) {
assert.Equal(t, false, (&apns.Response{StatusCode: 400}).Sent())
}

func TestStringTimestampParse(t *testing.T) {
response := &apns.Response{}
payload := "{\"reason\":\"Unregistered\", \"timestamp\":\"1421147681\"}"
json.Unmarshal([]byte(payload), &response)
assert.Equal(t, int64(1421147681), response.Timestamp.Unix())
}

func TestIntTimestampParse(t *testing.T) {
response := &apns.Response{}
payload := "{\"reason\":\"Unregistered\", \"timestamp\":1421147681}"
payload := "{\"reason\":\"Unregistered\", \"timestamp\":1458114061260}"
json.Unmarshal([]byte(payload), &response)
assert.Equal(t, int64(1421147681), response.Timestamp.Unix())
assert.Equal(t, int64(1458114061260)/1000, response.Timestamp.Unix())
}

func TestInvalidTimestampParse(t *testing.T) {
Expand Down

0 comments on commit a6f9928

Please sign in to comment.