Skip to content

Commit

Permalink
Make the streaming API raise exceptions for status codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericroberts committed Apr 28, 2014
1 parent 86cf51a commit b571e03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/twitter/streaming/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def <<(data)
@parser << data
end

def on_headers_complete(headers)
# TODO: handle response codes
p(:status_code => @parser.status_code, :header => headers) unless @parser.status_code == 200
def on_headers_complete(_headers)
error = Twitter::Error.errors[@parser.status_code]
fail error.new if error
end

def on_body(data)
Expand Down
21 changes: 21 additions & 0 deletions spec/twitter/streaming/response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'helper'

describe Twitter::Streaming::Response do
subject { Twitter::Streaming::Response.new }

describe '#on_headers_complete' do
it 'should not error if status code is 200' do
expect do
subject << "HTTP/1.1 200 OK\r\nSome-Header: Woo\r\n\r\n"
end.to_not raise_error
end

Twitter::Error.errors.each do |code, klass|
it "should raise an exception of type #{klass} for status code #{code}" do
expect do
subject << "HTTP/1.1 #{code} NOK\r\nSome-Header: Woo\r\n\r\n"
end.to raise_error(klass)
end
end
end
end

0 comments on commit b571e03

Please sign in to comment.