Permalink
Browse files

WIP converting to rpsec+bundler

  • Loading branch information...
1 parent 376351b commit 57089307d44b6f5b05f5e0656f39014abc544e71 @bpoweski committed Jan 1, 2011
View
@@ -0,0 +1,9 @@
+source "http://rubygems.org"
+
+gem "eventmachine"
+gem "daemons"
+gem "json"
+
+group :spec do
+ gem "rspec"
+end
View
@@ -0,0 +1,24 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ daemons (1.0.10)
+ diff-lcs (1.1.2)
+ eventmachine (0.12.10)
+ json (1.4.6)
+ rspec (2.3.0)
+ rspec-core (~> 2.3.0)
+ rspec-expectations (~> 2.3.0)
+ rspec-mocks (~> 2.3.0)
+ rspec-core (2.3.1)
+ rspec-expectations (2.3.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.3.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ daemons
+ eventmachine
+ json
+ rspec
View
@@ -5,8 +5,8 @@ h1. Apple Push Notification Server Toolkit
h2. Description
apnserver is a server and set of command line programs to send push notifications to the iPhone.
-Apple recomends to maintain an open connection to the push notification service and refrain from
-opening up and tearing down SSL connections reapeated. To solve this problem an intermediate
+Apple recommends to maintain an open connection to the push notification service and refrain from
+opening up and tearing down SSL connections repeated. To solve this problem an intermediate
network server is introduced that queues are requests to the APN service and sends them via a
persistent connection.
@@ -148,7 +148,7 @@ h2. License
(The MIT License)
-Copyright (c) 2009 Ben Poweski
+Copyright (c) 2011 Ben Poweski
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
@@ -1,41 +1,10 @@
-require 'rubygems'
-require 'rake/testtask'
-require 'rake/rdoctask'
-
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gemspec|
- gemspec.name = "apnserver"
- gemspec.summary = "Apple Push Notification Server"
- gemspec.description = "A toolkit for proxying and sending Apple Push Notifications"
- gemspec.email = "bpoweski@3factors.com"
- gemspec.homepage = "http://github.com/bpoweski/apnserver"
- gemspec.authors = ["Ben Poweski"]
- gemspec.add_dependency 'eventmachine'
- gemspec.add_dependency 'daemons'
- gemspec.add_dependency 'json'
- gemspec.rubyforge_project = 'apnserver'
- gemspec.files = FileList['lib/**/*.rb', 'bin/*', '[A-Z]*', 'test/**/*'].to_a
- end
-rescue LoadError
- puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
-end
-
-Rake::TestTask.new(:test) do |test|
- test.test_files = FileList.new('test/**/test_*.rb') do |list|
- list.exclude 'test/test_helper.rb'
- end
- test.libs << 'test'
- test.verbose = true
-end
-
-Jeweler::RubyforgeTasks.new do |rubyforge|
-end
+Dir['tasks/**/*.rake'].each { |t| load t }
+require 'rake/rdoctask'
Rake::RDocTask.new do |rd|
rd.main = "README.textile"
rd.rdoc_dir = 'rdoc'
rd.rdoc_files.include("README.textile", "lib/**/*.rb")
end
-task :default => [:test]
+task :default => [:spec]
View
@@ -1 +0,0 @@
-0.1.10
View
@@ -6,3 +6,4 @@
require 'apnserver/client'
require 'apnserver/server_connection'
require 'apnserver/server'
+
View
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+module ApnServer
+ describe Client do
+ describe "#new" do
+ let(:client) { ApnServer::Client.new('cert.pem', 'gateway.sandbox.push.apple.com', 2196) }
+
+ it "sets the pem path" do
+ client.pem.should == 'cert.pem'
+ end
+
+ it "sets the host" do
+ client.host.should == 'gateway.sandbox.push.apple.com'
+ end
+
+ it "sets the port" do
+ client.port.should == 2196
+ end
+ end
+ end
+end
@@ -0,0 +1,85 @@
+require 'spec_helper'
+
+module ApnServer
+ describe Notification do
+ let(:notification) { Notification.new }
+
+ describe "#to_bytes" do
+ it "generates a byte array" do
+ payload = '{"aps":{"alert":"You have not mail!"}}'
+ device_token = "12345678123456781234567812345678"
+ notification.device_token = device_token
+ notification.alert = "You have not mail!"
+ expected = [0, 0, device_token.size, device_token, 0, payload.size, payload]
+ notification.to_bytes.should == expected.pack("ccca*CCa*")
+ end
+ end
+
+ describe "#payload" do
+ it "generates the badge element" do
+ expected = { :aps => { :badge => 1 }}
+ notification.badge = 1
+ notification.payload.should == expected
+ end
+
+ it "generates the alert alement" do
+ expected = { :aps => { :alert => 'Hi' }}
+ notification.alert = 'Hi'
+ notification.payload.should == expected
+ end
+ end
+
+ describe "#json_payload" do
+ it "converts payload to json" do
+ expected = '{"aps":{"alert":"Hi"}}'
+ notification.alert = 'Hi'
+ notification.json_payload.should == expected
+ end
+
+ it "does not allow payloads larger than 256 chars" do
+ lambda {
+ alert = []
+ 256.times { alert << 'Hi' }
+ notification.alert = alert.join
+ notification.json_payload
+ }.should raise_error(Payload::PayloadInvalid)
+ end
+ end
+
+ describe "#valid?" do
+ it "recognizes a valid request" do
+ 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*")
+ Notification.valid?(request).should be_true
+ notification = Notification.parse(request)
+ notification.device_token.should == device_token
+ notification.alert.should == "You have not mail!"
+ end
+
+ it "recognizes an invalid request" do
+ device_token = '123456781234567812345678'
+ payload = '{"aps":{"alert":"You have not mail!"}}'
+ request = [0, 0, 32, device_token, 0, payload.size, payload].pack("CCCa*CCa*")
+ Notification.valid?(request).should be_false
+ end
+ end
+
+ describe "#parse" do
+ it "reads a byte array and constructs a notification" do
+ device_token = '12345678123456781234567812345678'
+ notification.device_token = device_token
+ notification.badge = 10
+ notification.alert = 'Hi'
+ notification.sound = 'default'
+ notification.custom = { 'acme1' => "bar", 'acme2' => 42}
+
+ parsed = Notification.parse(notification.to_bytes)
+ [:device_token, :badge, :alert, :sound, :custom].each do |k|
+ expected = notification.send(k)
+ parsed.send(k).should == expected
+ end
+ end
+ end
+ end
+end
@@ -1,23 +1,23 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
+require 'spec_helper'
class NotificationTest < Test::Unit::TestCase
include ApnServer::Payload
-
+
def test_should_create_payload_with_simple_string
expected = { :aps => { :alert => 'Hi' }}
assert_equal expected, create_payload('Hi')
end
-
+
def test_should_create_payload_with_alert_key
expected = { :aps => { :alert => 'Hi' }}
- assert_equal expected, create_payload(:alert => 'Hi')
+ assert_equal expected, create_payload(:alert => 'Hi')
end
-
+
def test_should_create_payload_with_badge_and_alert
expected = { :aps => { :alert => 'Hi', :badge => 1 }}
assert_equal expected, create_payload(:alert => 'Hi', :badge => 1)
end
-
+
# example 1
def test_should_create_payload_with_custom_payload
alert = 'Message received from Bob'
@@ -27,7 +27,7 @@ def test_should_create_payload_with_custom_payload
}
assert_equal expected, create_payload(:alert => alert, :custom => { :acme2 => ['bang', 'whiz']})
end
-
+
# example 3
def test_should_create_payload_with_sound_and_multiple_custom
expected = {
@@ -37,16 +37,16 @@ def test_should_create_payload_with_sound_and_multiple_custom
:sound => "bingbong.aiff"
},
:acme1 => "bar",
- :acme2 => 42
+ :acme2 => 42
}
assert_equal expected, create_payload({
:alert => "You got your emails.",
:badge => 9,
- :sound => "bingbong.aiff",
+ :sound => "bingbong.aiff",
:custom => { :acme1 => "bar", :acme2 => 42}
})
end
-
+
# example 5
def test_should_create_payload_with_empty_aps
expected = {
@@ -55,5 +55,5 @@ def test_should_create_payload_with_empty_aps
}
assert_equal expected, create_payload(:custom => { :acme2 => [ 5, 8 ] })
end
-
+
end
@@ -1,27 +1,27 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
+require 'spec_helper'
class TestServer
attr_accessor :queue
include ApnServer::Protocol
-
+
def address
[12345, '127.0.0.1']
end
end
class TestProtocol < Test::Unit::TestCase
-
+
def setup
@server = TestServer.new
@server.queue = Array.new # fake out EM::Queue
end
-
+
def test_adds_notification_to_queue
token = "12345678123456781234567812345678"
@server.receive_data("\0\0 #{token}\0#{22.chr}{\"aps\":{\"alert\":\"Hi\"}}")
assert_equal 1, @server.queue.size
end
-
+
def test_does_not_add_invalid_notification
@server.receive_data('fakedata')
assert @server.queue.empty?
View
@@ -0,0 +1,25 @@
+require 'rubygems'
+
+# Set up gems listed in the Gemfile.
+gemfile = File.expand_path('../Gemfile', File.dirname(__FILE__))
+begin
+ ENV['BUNDLE_GEMFILE'] = gemfile
+ require 'bundler'
+ Bundler.setup
+rescue Bundler::GemNotFound => e
+ STDERR.puts e.message
+ STDERR.puts "Try running `bundle install`."
+ exit!
+end
+
+Bundler.require(:spec)
+
+Rspec.configure do |config|
+ config.mock_with :rspec
+end
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+require "apnserver"
+require 'base64'
View
@@ -0,0 +1,3 @@
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new
View
@@ -1,11 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper'
-
-class TestClient < Test::Unit::TestCase
-
- def test_creates_client
- client = ApnServer::Client.new('cert.pem', 'gateway.sandbox.push.apple.com', 2196)
- assert_equal 'cert.pem', client.pem
- assert_equal 'gateway.sandbox.push.apple.com', client.host
- assert_equal 2196, client.port
- end
-end
View
@@ -1,4 +0,0 @@
-require 'stringio'
-require 'test/unit'
-require 'rubygems'
-require File.dirname(__FILE__) + '/../lib/apnserver'
Oops, something went wrong.

0 comments on commit 5708930

Please sign in to comment.