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

Port tests to support RSpec 3 #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
102 changes: 51 additions & 51 deletions spec/twitter/json_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def receive_data data
context "authentication" do
it "should connect with basic auth credentials" do
connect_stream :auth => "username:password", :ssl => false
$recieved_data.should include('Authorization: Basic')
expect($recieved_data).to include('Authorization: Basic')
end

it "should connect with oauth credentials" do
Expand All @@ -35,44 +35,44 @@ def receive_data data
:access_secret => 'ohno'
}
connect_stream :oauth => oauth, :ssl => false
$recieved_data.should include('Authorization: OAuth')
expect($recieved_data).to include('Authorization: OAuth')
end
end

context "on create" do

it "should return stream" do
EM.should_receive(:connect).and_return('TEST INSTANCE')
expect(EM).to receive(:connect).and_return('TEST INSTANCE')
stream = JSONStream.connect {}
stream.should == 'TEST INSTANCE'
expect(stream).to eq('TEST INSTANCE')
end

it "should define default properties" do
EM.should_receive(:connect).with do |host, port, handler, opts|
host.should == 'stream.twitter.com'
port.should == 443
opts[:path].should == '/1/statuses/filter.json'
opts[:method].should == 'GET'
end
expect(EM).to receive(:connect).with(any_args) { |host, port, handler, opts|
expect(host).to eq('stream.twitter.com')
expect(port).to eq(443)
expect(opts[:path]).to eq('/1/statuses/filter.json')
expect(opts[:method]).to eq('GET')
}
stream = JSONStream.connect {}
end

it "should connect to the proxy if provided" do
EM.should_receive(:connect).with do |host, port, handler, opts|
host.should == 'my-proxy'
port.should == 8080
opts[:host].should == 'stream.twitter.com'
opts[:port].should == 443
opts[:proxy].should == 'http://my-proxy:8080'
end
expect(EM).to receive(:connect).with(any_args) { |host, port, handler, opts|
expect(host).to eq('my-proxy')
expect(port).to eq(8080)
expect(opts[:host]).to eq('stream.twitter.com')
expect(opts[:port]).to eq(443)
expect(opts[:proxy]).to eq('http://my-proxy:8080')
}
stream = JSONStream.connect(:proxy => "http://my-proxy:8080") {}
end

it "should not trigger SSL until connection is established" do
connection = stub('connection')
EM.should_receive(:connect).and_return(connection)
connection = double('connection')
expect(EM).to receive(:connect).and_return(connection)
stream = JSONStream.connect(:ssl => true)
stream.should == connection
expect(stream).to eq(connection)
end

end
Expand All @@ -89,34 +89,34 @@ def receive_data data

it "should add no params" do
connect_stream :ssl => false
$recieved_data.should include('/1/statuses/filter.json HTTP')
expect($recieved_data).to include('/1/statuses/filter.json HTTP')
end

it "should add custom params" do
connect_stream :params => {:name => 'test'}, :ssl => false
$recieved_data.should include('?name=test')
expect($recieved_data).to include('?name=test')
end

it "should parse headers" do
connect_stream :ssl => false
stream.code.should == 200
stream.headers.keys.map{|k| k.downcase}.should include('content-type')
expect(stream.code).to eq(200)
expect(stream.headers.keys.map{|k| k.downcase}).to include('content-type')
end

it "should parse headers even after connection close" do
connect_stream :ssl => false
stream.code.should == 200
stream.headers.keys.map{|k| k.downcase}.should include('content-type')
expect(stream.code).to eq(200)
expect(stream.headers.keys.map{|k| k.downcase}).to include('content-type')
end

it "should extract records" do
connect_stream :user_agent => 'TEST_USER_AGENT', :ssl => false
$recieved_data.upcase.should include('USER-AGENT: TEST_USER_AGENT')
expect($recieved_data.upcase).to include('USER-AGENT: TEST_USER_AGENT')
end

it 'should allow custom headers' do
connect_stream :headers => { 'From' => 'twitter-stream' }, :ssl => false
$recieved_data.upcase.should include('FROM: TWITTER-STREAM')
expect($recieved_data.upcase).to include('FROM: TWITTER-STREAM')
end

it "should deliver each item" do
Expand All @@ -128,9 +128,9 @@ def receive_data data
end
# Extract only the tweets from the fixture
tweets = $body.map{|l| l.strip }.select{|l| l =~ /^\{/ }
items.size.should == tweets.size
expect(items.size).to eq(tweets.size)
tweets.each_with_index do |tweet,i|
items[i].should == tweet
expect(items[i]).to eq(tweet)
end
end

Expand Down Expand Up @@ -159,33 +159,33 @@ def receive_data data
shared_examples_for "network failure" do
it "should reconnect on network failure" do
connect_stream do
stream.should_receive(:reconnect)
expect(stream).to receive(:reconnect)
end
end

it "should not reconnect on network failure when not configured to auto reconnect" do
connect_stream(:auto_reconnect => false) do
stream.should_receive(:reconnect).never
expect(stream).to receive(:reconnect).never
end
end

it "should reconnect with 0.25 at base" do
connect_stream do
stream.should_receive(:reconnect_after).with(0.25)
expect(stream).to receive(:reconnect_after).with(0.25)
end
end

it "should reconnect with linear timeout" do
connect_stream do
stream.nf_last_reconnect = 1
stream.should_receive(:reconnect_after).with(1.25)
expect(stream).to receive(:reconnect_after).with(1.25)
end
end

it "should stop reconnecting after 100 times" do
connect_stream do
stream.reconnect_retries = 100
stream.should_not_receive(:reconnect_after)
expect(stream).not_to receive(:reconnect_after)
end
end

Expand All @@ -197,8 +197,8 @@ def receive_data data
end
stream.reconnect_retries = 100
end
timeout.should == 0.25
retries.should == 101
expect(timeout).to eq(0.25)
expect(retries).to eq(101)
end
end

Expand All @@ -211,19 +211,19 @@ def receive_data data

it "should timeout on inactivity" do
connect_stream :stop_in => 1.5 do
stream.should_receive(:reconnect)
expect(stream).to receive(:reconnect)
end
end

it "should not reconnect on inactivity when not configured to auto reconnect" do
connect_stream(:stop_in => 1.5, :auto_reconnect => false) do
stream.should_receive(:reconnect).never
expect(stream).to receive(:reconnect).never
end
end

it "should reconnect with SSL if enabled" do
connect_stream :ssl => true do
stream.should_receive(:start_tls).twice
expect(stream).to receive(:start_tls).twice
end
end

Expand All @@ -240,7 +240,7 @@ def receive_data data
it "should call no data callback after no data received for 90 seconds" do
connect_stream :stop_in => 6 do
stream.last_data_received_at = Time.now - 88
stream.should_receive(:no_data).once
expect(stream).to receive(:no_data).once
end
end

Expand Down Expand Up @@ -271,27 +271,27 @@ def connect_stream_without_server(opts={},&block)

it "should reconnect on application failure 10 at base" do
connect_stream :ssl => false do
stream.should_receive(:reconnect_after).with(10)
expect(stream).to receive(:reconnect_after).with(10)
end
end

it "should not reconnect on application failure 10 at base when not configured to auto reconnect" do
connect_stream :ssl => false, :auto_reconnect => false do
stream.should_receive(:reconnect_after).never
expect(stream).to receive(:reconnect_after).never
end
end

it "should reconnect with exponential timeout" do
connect_stream :ssl => false do
stream.af_last_reconnect = 160
stream.should_receive(:reconnect_after).with(320)
expect(stream).to receive(:reconnect_after).with(320)
end
end

it "should not try to reconnect after limit is reached" do
connect_stream :ssl => false do
stream.af_last_reconnect = 320
stream.should_not_receive(:reconnect_after)
expect(stream).not_to receive(:reconnect_after)
end
end
end
Expand All @@ -312,9 +312,9 @@ def connect_stream_without_server(opts={},&block)
items << item
end
end
items.size.should == 2
items[0].should == '{"screen_name":"user1"}'
items[1].should == '{"id":9876}'
expect(items.size).to eq(2)
expect(items[0]).to eq('{"screen_name":"user1"}')
expect(items[1]).to eq('{"id":9876}')
end

it "should parse full entities even if split" do
Expand All @@ -326,9 +326,9 @@ def connect_stream_without_server(opts={},&block)
items << item
end
end
items.size.should == 2
items[0].should == '{"id":1234}'
items[1].should == '{"id":9876}'
expect(items.size).to eq(2)
expect(items[0]).to eq('{"id":1234}')
expect(items[1]).to eq('{"id":9876}')
end
end

Expand Down
2 changes: 1 addition & 1 deletion twitter-stream.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('eventmachine', ">= 0.12.8")
s.add_runtime_dependency('simple_oauth', '~> 0.2.0')
s.add_runtime_dependency('http_parser.rb', '~> 0.5.1')
s.add_development_dependency('rspec', "~> 2.5.0")
s.add_development_dependency('rspec', ">= 3.0.0")

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down