From 0418ea0a41191d8c5af8baaf09721daa815ab483 Mon Sep 17 00:00:00 2001 From: Pere Urbon-Bayes Date: Tue, 24 Nov 2015 15:05:21 +0100 Subject: [PATCH] fix the connection string for the streaming api when using either a proxy 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 --- lib/twitter/streaming/connection.rb | 20 +++++++--- spec/twitter/streaming/connection_spec.rb | 48 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/lib/twitter/streaming/connection.rb b/lib/twitter/streaming/connection.rb index 6c9aa0db5..3f75d1fbc 100644 --- a/lib/twitter/streaming/connection.rb +++ b/lib/twitter/streaming/connection.rb @@ -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 diff --git a/spec/twitter/streaming/connection_spec.rb b/spec/twitter/streaming/connection_spec.rb index 838a2d8a9..57d48a8cd 100644 --- a/spec/twitter/streaming/connection_spec.rb +++ b/spec/twitter/streaming/connection_spec.rb @@ -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