spec/twitter/json_stream_spec.rb in twitter-stream-0.1.13 vs spec/twitter/json_stream_spec.rb in twitter-stream-0.1.14

- old
+ new

@@ -72,11 +72,13 @@ end context "on valid stream" do attr_reader :stream before :each do - $data_to_send = read_fixture('twitter/basic_http.txt') + $body = File.readlines(fixture_path("twitter/tweets.txt")) + $body.each {|tweet| tweet.strip!; tweet << "\r" } + $data_to_send = http_response(200,"OK",{},$body) $recieved_data = '' $close_connection = false end it "should add no params" do @@ -90,24 +92,44 @@ end it "should parse headers" do connect_stream stream.code.should == 200 - stream.headers[0].downcase.should include('content-type') + stream.headers.keys.map{|k| k.downcase}.should include('content-type') end it "should parse headers even after connection close" do connect_stream stream.code.should == 200 - stream.headers[0].downcase.should include('content-type') + stream.headers.keys.map{|k| k.downcase}.should include('content-type') end it "should extract records" do connect_stream :user_agent => 'TEST_USER_AGENT' $recieved_data.upcase.should include('USER-AGENT: TEST_USER_AGENT') end + it 'should allow custom headers' do + connect_stream :headers => { 'From' => 'twitter-stream' } + $recieved_data.upcase.should include('FROM: TWITTER-STREAM') + end + + it "should deliver each item" do + items = [] + connect_stream do + stream.each_item do |item| + items << item + end + end + # Extract only the tweets from the fixture + tweets = $body.map{|l| l.strip }.select{|l| l =~ /^\{/ } + items.size.should == tweets.size + tweets.each_with_index do |tweet,i| + items[i].should == tweet + end + end + it "should send correct user agent" do connect_stream end end @@ -116,10 +138,16 @@ connect_stream do stream.should_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 + end + end + it "should reconnect with 0.25 at base" do connect_stream do stream.should_receive(:reconnect_after).with(0.25) end end @@ -162,10 +190,16 @@ connect_stream :stop_in => 1.5 do stream.should_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 + end + end + it_should_behave_like "network failure" end context "on server unavailable" do @@ -184,20 +218,26 @@ end context "on application failure" do attr_reader :stream before :each do - $data_to_send = 'HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm="Firehose"\r\n\r\n1' - $close_connection = true + $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 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 + stream.should_receive(:reconnect_after).never + end + end + it "should reconnect with exponential timeout" do connect_stream do stream.af_last_reconnect = 160 stream.should_receive(:reconnect_after).with(320) end @@ -208,6 +248,43 @@ stream.af_last_reconnect = 320 stream.should_not_receive(:reconnect_after) end end end + + context "on stream with chunked transfer encoding" do + attr_reader :stream + before :each do + $recieved_data = '' + $close_connection = false + end + + 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 + stream.each_item do |item| + items << item + end + end + items.size.should == 2 + items[0].should == '{"screen_name":"user1"}' + items[1].should == '{"id":9876}' + end + + 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 + stream.each_item do |item| + items << item + end + end + items.size.should == 2 + items[0].should == '{"id":1234}' + items[1].should == '{"id":9876}' + end + end + end