Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Timestamp parsing issue #11

Merged
merged 1 commit into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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