Skip to content

Commit

Permalink
fix "RangeError: 256 out of char range", because Integer#chr handles …
Browse files Browse the repository at this point in the history
…255 at most, but the value to be 256.
  • Loading branch information
niku committed Apr 27, 2012
1 parent 3d5a590 commit d1a879e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/apn_on_rails/app/models/apn/group_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def to_apple_json
# Creates the binary message needed to send to Apple.
def message_for_sending(device)
json = self.to_apple_json
message = "\0\0 #{device.to_hexa}\0#{json.length.chr}#{json}"
message = "\0\0 #{device.to_hexa}#{[json.length].pack('n')}#{json}"
raise APN::Errors::ExceededMessageSizeError.new(message) if message.size.to_i > 256
message
end
Expand Down
12 changes: 12 additions & 0 deletions spec/apn_on_rails/app/models/apn/group_notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
}.should raise_error(APN::Errors::ExceededMessageSizeError)
end

it 'should not raise any error if the payload is not too big' do
app = AppFactory.create
device = DeviceFactory.create({:app_id => app.id})
group = GroupFactory.create({:app_id => app.id})
device_grouping = DeviceGroupingFactory.create({:group_id => group.id,:device_id => device.id})
noty = GroupNotificationFactory.new(:group_id => group.id, :sound => true, :badge => nil)
noty.stub(:to_apple_json).and_return('_' * 256)
lambda {
noty.message_for_sending(device)
}.should_not raise_error
end

end

end

0 comments on commit d1a879e

Please sign in to comment.