spec/twitter/json_stream_spec.rb in twitter-stream-0.1.14 vs spec/twitter/json_stream_spec.rb in twitter-stream-0.1.15
- old
+ new
@@ -1,7 +1,6 @@
-$:.unshift "."
-require File.dirname(__FILE__) + '/../spec_helper.rb'
+require 'spec_helper.rb'
require 'twitter/json_stream'
include Twitter
Host = "127.0.0.1"
@@ -22,22 +21,22 @@
describe JSONStream do
context "authentication" do
it "should connect with basic auth credentials" do
- connect_stream :auth => "username:password"
+ connect_stream :auth => "username:password", :ssl => false
$recieved_data.should include('Authorization: Basic')
end
it "should connect with oauth credentials" do
oauth = {
:consumer_key => '1234567890',
:consumer_secret => 'abcdefghijklmnopqrstuvwxyz',
:access_key => 'ohai',
:access_secret => 'ohno'
}
- connect_stream :oauth => oauth
+ connect_stream :oauth => oauth, :ssl => false
$recieved_data.should include('Authorization: OAuth')
end
end
context "on create" do
@@ -49,11 +48,11 @@
end
it "should define default properties" do
EM.should_receive(:connect).with do |host, port, handler, opts|
host.should == 'stream.twitter.com'
- port.should == 80
+ port.should == 443
opts[:path].should == '/1/statuses/filter.json'
opts[:method].should == 'GET'
end
stream = JSONStream.connect {}
end
@@ -61,63 +60,70 @@
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 == 80
+ opts[:port].should == 443
opts[:proxy].should == 'http://my-proxy:8080'
end
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)
+ stream = JSONStream.connect(:ssl => true)
+ stream.should == connection
+ end
+
end
context "on valid stream" do
attr_reader :stream
before :each do
$body = File.readlines(fixture_path("twitter/tweets.txt"))
$body.each {|tweet| tweet.strip!; tweet << "\r" }
- $data_to_send = http_response(200,"OK",{},$body)
+ $data_to_send = http_response(200, "OK", {}, $body)
$recieved_data = ''
$close_connection = false
end
it "should add no params" do
- connect_stream
+ connect_stream :ssl => false
$recieved_data.should include('/1/statuses/filter.json HTTP')
end
it "should add custom params" do
- connect_stream :params => {:name => 'test'}
+ connect_stream :params => {:name => 'test'}, :ssl => false
$recieved_data.should include('?name=test')
end
it "should parse headers" do
- connect_stream
+ connect_stream :ssl => false
stream.code.should == 200
stream.headers.keys.map{|k| k.downcase}.should include('content-type')
end
it "should parse headers even after connection close" do
- connect_stream
+ connect_stream :ssl => false
stream.code.should == 200
stream.headers.keys.map{|k| k.downcase}.should include('content-type')
end
it "should extract records" do
- connect_stream :user_agent => 'TEST_USER_AGENT'
+ connect_stream :user_agent => 'TEST_USER_AGENT', :ssl => false
$recieved_data.upcase.should include('USER-AGENT: TEST_USER_AGENT')
end
it 'should allow custom headers' do
- connect_stream :headers => { 'From' => 'twitter-stream' }
+ connect_stream :headers => { 'From' => 'twitter-stream' }, :ssl => false
$recieved_data.upcase.should include('FROM: TWITTER-STREAM')
end
it "should deliver each item" do
items = []
- connect_stream do
+ connect_stream :ssl => false do
stream.each_item do |item|
items << item
end
end
# Extract only the tweets from the fixture
@@ -196,10 +202,16 @@
connect_stream(:stop_in => 1.5, :auto_reconnect => false) do
stream.should_receive(:reconnect).never
end
end
+ it "should reconnect with SSL if enabled" do
+ connect_stream :ssl => true do
+ stream.should_receive(:start_tls).twice
+ end
+ end
+
it_should_behave_like "network failure"
end
context "on server unavailable" do
@@ -223,30 +235,30 @@
$data_to_send = "HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm=\"Firehose\"\r\n\r\n"
$close_connection = false
end
it "should reconnect on application failure 10 at base" do
- connect_stream do
+ connect_stream :ssl => false do
stream.should_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(:auto_reconnect => false) do
+ connect_stream :ssl => false, :auto_reconnect => false do
stream.should_receive(:reconnect_after).never
end
end
it "should reconnect with exponential timeout" do
- connect_stream do
+ connect_stream :ssl => false do
stream.af_last_reconnect = 160
stream.should_receive(:reconnect_after).with(320)
end
end
it "should not try to reconnect after limit is reached" do
- connect_stream do
+ connect_stream :ssl => false do
stream.af_last_reconnect = 320
stream.should_not_receive(:reconnect_after)
end
end
end
@@ -260,11 +272,11 @@
it "should ignore empty lines" do
body_chunks = ["{\"screen"+"_name\"",":\"user1\"}\r\r\r{","\"id\":9876}\r\r"]
$data_to_send = http_response(200,"OK",{},body_chunks)
items = []
- connect_stream do
+ connect_stream :ssl => false do
stream.each_item do |item|
items << item
end
end
items.size.should == 2
@@ -274,10 +286,10 @@
it "should parse full entities even if split" do
body_chunks = ["{\"id\"",":1234}\r{","\"id\":9876}"]
$data_to_send = http_response(200,"OK",{},body_chunks)
items = []
- connect_stream do
+ connect_stream :ssl => false do
stream.each_item do |item|
items << item
end
end
items.size.should == 2