Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Spec passes on ruby19 #46

Open
wants to merge 21 commits into from
This page is out of date. Refresh to see the latest.
57 spec/apn_on_rails/app/models/apn/group_notification_spec.rb
View
@@ -1,19 +1,19 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper.rb')
describe APN::GroupNotification do
-
+
describe 'alert' do
-
+
it 'should trim the message to 150 characters' do
noty = APN::GroupNotification.new
noty.alert = 'a' * 200
noty.alert.should == ('a' * 147) + '...'
end
-
+
end
-
+
describe 'apple_hash' do
-
+
it 'should return a hash of the appropriate params for Apple' do
noty = APN::GroupNotification.first
noty.apple_hash.should == {"aps" => {"badge" => 5, "sound" => "my_sound.aiff", "alert" => "Hello!"},"typ" => "1"}
@@ -28,27 +28,42 @@
noty.sound = true
noty.apple_hash.should == {"aps" => {"sound" => "1.aiff"}}
end
-
+
end
-
+
describe 'to_apple_json' do
-
+
it 'should return the necessary JSON for Apple' do
noty = APN::GroupNotification.first
- noty.to_apple_json.should == %{{"typ":"1","aps":{"badge":5,"sound":"my_sound.aiff","alert":"Hello!"}}}
+ noty.to_apple_json.should be_same_meaning_as %{{"typ":"1","aps":{"badge":5,"sound":"my_sound.aiff","alert":"Hello!"}}}
end
-
+
end
-
+
describe 'message_for_sending' do
-
- it 'should create a binary message to be sent to Apple' do
- noty = APN::GroupNotification.first
- noty.custom_properties = nil
- device = DeviceFactory.new(:token => '5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz')
- noty.message_for_sending(device).should == fixture_value('message_for_sending.bin')
+
+ describe 'should create a binary message to be sent to Apple' do
+ subject {
+ noty = APN::GroupNotification.first
+ noty.custom_properties = nil
+ device = DeviceFactory.new(:token => token)
+ noty.message_for_sending(device)
+ }
+ let(:token) { '5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz' }
+ let(:device_token_binary_size) { [token.delete(' ')].pack('H*').size }
+ let(:token_part_header_length) { 1 + 2 } # Command length(1 byte) + Token length(2 byte)
+ let(:payload_part_header_length) { 2 } # Payload length(2 byte)
+ let(:boundaly_between_binary_and_payload) { token_part_header_length + device_token_binary_size + payload_part_header_length }
+
+ it 'should eq eqch binary part' do
+ subject[0...boundaly_between_binary_and_payload].should == fixture_value('message_for_sending.bin')[0...boundaly_between_binary_and_payload]
+ end
+
+ it 'should be same meaning as each payload part' do
+ subject[boundaly_between_binary_and_payload..-1].should be_same_meaning_as fixture_value('message_for_sending.bin')[boundaly_between_binary_and_payload..-1]
+ end
end
-
+
it 'should raise an APN::Errors::ExceededMessageSizeError if the message is too big' do
app = AppFactory.create
device = DeviceFactory.create({:app_id => app.id})
@@ -60,7 +75,7 @@
noty.message_for_sending(device)
}.should raise_error(APN::Errors::ExceededMessageSizeError)
end
-
+
end
-
-end
+
+end
63 spec/apn_on_rails/app/models/apn/notification_spec.rb
View
@@ -1,19 +1,19 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper.rb')
describe APN::Notification do
-
+
describe 'alert' do
-
+
it 'should trim the message to 150 characters' do
noty = APN::Notification.new
noty.alert = 'a' * 200
noty.alert.should == ('a' * 147) + '...'
end
-
+
end
-
+
describe 'apple_hash' do
-
+
it 'should return a hash of the appropriate params for Apple' do
noty = APN::Notification.first
noty.apple_hash.should == {"aps" => {"badge" => 5, "sound" => "my_sound.aiff", "alert" => "Hello!"},"typ" => "1"}
@@ -28,27 +28,42 @@
noty.sound = true
noty.apple_hash.should == {"aps" => {"sound" => "1.aiff"}}
end
-
+
end
-
+
describe 'to_apple_json' do
-
+
it 'should return the necessary JSON for Apple' do
noty = APN::Notification.first
- noty.to_apple_json.should == %{{"typ":"1","aps":{"badge":5,"sound":"my_sound.aiff","alert":"Hello!"}}}
+ noty.to_apple_json.should be_same_meaning_as %{{"typ":"1","aps":{"badge":5,"sound":"my_sound.aiff","alert":"Hello!"}}}
end
-
+
end
-
+
describe 'message_for_sending' do
-
- it 'should create a binary message to be sent to Apple' do
- noty = APN::Notification.first
- noty.custom_properties = nil
- noty.device = DeviceFactory.new(:token => '5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz')
- noty.message_for_sending.should == fixture_value('message_for_sending.bin')
+
+ describe 'should create a binary message to be sent to Apple' do
+ subject {
+ noty = APN::Notification.first
+ noty.custom_properties = nil
+ noty.device = DeviceFactory.new(:token => token)
+ noty.message_for_sending
+ }
+ let(:token) { '5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz' }
+ let(:device_token_binary_size) { [token.delete(' ')].pack('H*').size }
+ let(:token_part_header_length) { 1 + 2 } # Command length(1 byte) + Token length(2 byte)
+ let(:payload_part_header_length) { 2 } # Payload length(2 byte)
+ let(:boundaly_between_binary_and_payload) { token_part_header_length + device_token_binary_size + payload_part_header_length }
+
+ it 'should eq each binary part' do
+ subject[0...boundaly_between_binary_and_payload].should == fixture_value('message_for_sending.bin')[0...boundaly_between_binary_and_payload]
+ end
+
+ it 'should be same meaning as each payload part' do
+ subject[boundaly_between_binary_and_payload..-1].should be_same_meaning_as fixture_value('message_for_sending.bin')[boundaly_between_binary_and_payload..-1]
+ end
end
-
+
it 'should raise an APN::Errors::ExceededMessageSizeError if the message is too big' do
noty = NotificationFactory.new(:device_id => DeviceFactory.create, :sound => true, :badge => nil)
noty.send(:write_attribute, 'alert', 'a' * 183)
@@ -56,16 +71,16 @@
noty.message_for_sending
}.should raise_error(APN::Errors::ExceededMessageSizeError)
end
-
+
end
-
- describe 'send_notifications' do
-
+
+ describe 'send_notifications' do
+
it 'should warn the user the method is deprecated and call the corresponding method on APN::App' do
ActiveSupport::Deprecation.should_receive(:warn)
APN::App.should_receive(:send_notifications)
APN::Notification.send_notifications
end
end
-
-end
+
+end
30 spec/spec_helper.rb
View
@@ -25,23 +25,23 @@
configatron.apn.cert = File.expand_path(File.join(File.dirname(__FILE__), 'rails_root', 'config', 'apple_push_notification_development.pem'))
RSpec.configure do |config|
-
+
config.before(:all) do
-
+
end
-
+
config.after(:all) do
-
+
end
-
+
config.before(:each) do
end
-
+
config.after(:each) do
-
+
end
-
+
end
def fixture_path(*name)
@@ -49,7 +49,11 @@ def fixture_path(*name)
end
def fixture_value(*name)
- return File.read(fixture_path(*name))
+ if RUBY_VERSION =~ /^1\.8/
+ File.read(fixture_path(*name))
+ else
+ File.read(fixture_path(*name), :encoding => 'BINARY')
+ end
end
def write_fixture(name, value)
@@ -61,4 +65,10 @@ def apn_cert
end
class BlockRan < StandardError
-end
+end
+
+RSpec::Matchers.define :be_same_meaning_as do |expected|
+ match do |actual|
+ ActiveSupport::JSON.decode(actual) == ActiveSupport::JSON.decode(expected)
+ end
+end
Something went wrong with that request. Please try again.