Permalink
Browse files

fixed unit tests

  • Loading branch information...
1 parent 636d048 commit b3dfbf038f8c2a043aab6844d9e2735bb9191cb1 Ben Poweski committed Oct 26, 2009
Showing with 17 additions and 23 deletions.
  1. +12 −5 lib/apnserver/notification.rb
  2. +5 −18 test/test_notification.rb
@@ -27,7 +27,9 @@ def payload
end
def json_payload
- defined?(Rails) ? payload.to_json : JSON.generate(payload)
+ j = defined?(Rails) ? payload.to_json : JSON.generate(payload)
+ raise PayloadInvalid.new("The payload is larger than allowed: #{j.length}") if j.size > 256
+ j
end
def push
@@ -48,9 +50,14 @@ def to_bytes
[0, 0, device_token.size, device_token, 0, j.size, j].pack("ccca*cca*")
end
- def self.valid?(payload)
+ def self.valid?(p)
begin
- Notification.parse(payload)
+ Notification.parse(p)
+ rescue PayloadInvalid => p
+ puts "PayloadInvalid: #{p}"
+ false
+ rescue JSON::ParserError => p
+ false
rescue RuntimeError
false
end
@@ -61,7 +68,7 @@ def self.parse(p)
notification = Notification.new
header = buffer.slice!(0, 3).unpack('ccc')
- if header[0] != 0 || header[1] != 0 || header[2] != 32
+ if header[0] != 0
raise RuntimeError.new("Header of notification is invalid: #{header.inspect}")
end
@@ -71,7 +78,7 @@ def self.parse(p)
# parse json payload
payload_len = buffer.slice!(0, 2).unpack('CC')
j = buffer.slice!(0, payload_len.last)
- result = JSON.parse(buffer.slice!(0, payload_len.last))
+ result = JSON.parse(j)
['alert', 'badge', 'sound'].each do |k|
notification.send("#{k}=", result['aps'][k]) if result['aps'] && result['aps'][k]
@@ -1,6 +1,5 @@
require File.dirname(__FILE__) + '/test_helper.rb'
require 'base64'
-require 'ruby-debug'
class NotificationTest < Test::Unit::TestCase
include ApnServer
@@ -15,7 +14,7 @@ def test_should_generate_byte_array
@notification.device_token = device_token
@notification.alert = "You have not mail!"
expected = [0, 0, device_token.size, device_token, 0, payload.size, payload]
- assert_equal expected.pack("ccca*cca*"), @notification.to_bytes
+ assert_equal expected.pack("ccca*CCa*"), @notification.to_bytes
end
def test_should_create_payload_with_badge_attribute
@@ -48,18 +47,18 @@ def test_should_not_allow_for_payloads_larger_than_256_chars
def test_should_recognize_valid_request
device_token = '12345678123456781234567812345678'
payload = '{"aps":{"alert":"You have not mail!"}}'
- request = [0, 0, device_token.size, device_token, 0, payload.size, payload].pack("ccca*cca*")
+ request = [0, 0, device_token.size, device_token, 0, payload.size, payload].pack("CCCa*CCa*")
assert Notification.valid?(request)
notification = Notification.parse(request)
assert_equal device_token, notification.device_token
assert_equal "You have not mail!", notification.alert
end
def test_should_not_recognize_invalid_request
- device_token = '12345678123456781234567812345678'
+ device_token = '123456781234567812345678'
payload = '{"aps":{"alert":"You have not mail!"}}'
- request = [0, 0, 20, device_token, 0, payload.size, payload].pack("ccca*cca*")
- assert !Notification.valid?(request)
+ request = [0, 0, 32, device_token, 0, payload.size, payload].pack("CCCa*CCa*")
+ assert !Notification.valid?(request), request
end
def test_should_pack_and_unpack_json
@@ -77,16 +76,4 @@ def test_should_pack_and_unpack_json
assert_equal expected, parsed.send(k), "Expected #{k} to be #{expected}"
end
end
-
- def test_should_parse_notification
-
- n = Notification.parse(Base64.decode64(<<-B64
- AAAgt30z8rJVUlqcAwcOVeSIrsBxibaJ0xyCi8/AkmzNlk8An3siYXBzIjp7
- InNvdW5kIjoiZGVmYXVsdCIsImFsZXJ0IjoiWW91J3ZlIGJlZW4gbnVkZ2Vk
- IGh0dHA6XC9cL2JpdC5seVwvMzdEWmZGIiwiYmFkZ2UiOjF9LCJsYXRpdHVk
- ZSI6MzMuMDgwOTMzLCJsb25naXR1ZGUiOi05Ni44MTkyODgsInNjcmVlbl9u
- YW1lIjoiM2ZhY3RvcnMifQ==
- B64
- ))
- end
end

0 comments on commit b3dfbf0

Please sign in to comment.