Skip to content

Commit

Permalink
fix the connection string for the streaming api when using either a p…
Browse files Browse the repository at this point in the history
…roxy or without as the uri.port method skips standard ports we should use socket references that pull que raw data, also formated a bit to include test for it
  • Loading branch information
Pere Urbon-Bayes committed Nov 24, 2015
1 parent dd6457d commit 0418ea0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/twitter/streaming/connection.rb
Expand Up @@ -13,16 +13,26 @@ def initialize(options = {})
end

def stream(request, response)
client_context = OpenSSL::SSL::SSLContext.new
client = @tcp_socket_class.new(Resolv.getaddress(request.uri.host), request.uri.port)
ssl_client = @ssl_socket_class.new(client, client_context)

ssl_client.connect
ssl_client = connect(request)
request.stream(ssl_client)
while body = ssl_client.readpartial(1024) # rubocop:disable AssignmentInCondition
response << body
end
end

def connect(request)
client_context = OpenSSL::SSL::SSLContext.new
client = new_tcp_socket(request.socket_host, request.socket_port)
ssl_client = @ssl_socket_class.new(client, client_context)

ssl_client.connect
end

private

def new_tcp_socket(host, port)
@tcp_socket_class.new(Resolv.getaddress(host), port)
end
end
end
end
48 changes: 48 additions & 0 deletions spec/twitter/streaming/connection_spec.rb
Expand Up @@ -25,4 +25,52 @@ class DummySSLSocket; end
end
end
end

describe "connection" do

class DummyResponse
def initiailze(&block)
yield
end

def <<(data)

end
end

subject(:connection) do
Twitter::Streaming::Connection.new(tcp_socket_class: DummyTCPSocket, ssl_socket_class: DummySSLSocket)
end

let(:method) { :get }
let(:uri) { 'https://stream.twitter.com:443/1.1/statuses/sample.json' }
let(:ssl_socket) { double("ssl_socket") }

let(:request) { HTTP::Request.new(method, uri, {}) }

it 'requests via the proxy' do
expect(connection.ssl_socket_class).to receive(:new).and_return(ssl_socket)
allow(ssl_socket).to receive(:connect)

expect(connection).to receive(:new_tcp_socket).with('stream.twitter.com', 443)
connection.connect(request)
end


context "when using a proxy" do

let(:proxy) { {proxy_address: '127.0.0.1', proxy_port: 3328} }
let(:request) { HTTP::Request.new(method, uri, {}, proxy) }

it 'requests via the proxy' do
expect(connection.ssl_socket_class).to receive(:new).and_return(ssl_socket)
allow(ssl_socket).to receive(:connect)

expect(connection).to receive(:new_tcp_socket).with('127.0.0.1', 3328)
connection.connect(request)
end

end
end

end

0 comments on commit 0418ea0

Please sign in to comment.