spec/proxy_spec.rb in em-proxy-0.1.1 vs spec/proxy_spec.rb in em-proxy-0.1.2

- old
+ new

@@ -7,11 +7,11 @@ fail end it "should recieve data on port 8080" do EM.run do - EventMachine.add_timer(2) do + EventMachine.add_timer(0.1) do EventMachine::HttpRequest.new('http://127.0.0.1:8080/test').get({:timeout => 1}) end Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn| conn.on_data do |data| @@ -20,13 +20,31 @@ end end end end + it "should call the on_connect callback" do + connected = false + EM.run do + EventMachine.add_timer(0.1) do + EventMachine::HttpRequest.new('http://127.0.0.1:8080/test').get({:timeout => 1}) + end + + Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn| + conn.on_connect do + connected = true + EventMachine.stop + end + end + end + connected.should == true + end + + it "should transparently redirect TCP traffic to google" do EM.run do - EventMachine.add_timer(2) do + EventMachine.add_timer(0.1) do EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1}) end Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn| conn.server :goog, :host => "google.com", :port => 80 @@ -41,11 +59,11 @@ end end it "should duplex TCP traffic to two backends google & yahoo" do EM.run do - EventMachine.add_timer(2) do + EventMachine.add_timer(0.1) do EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1}) end Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn| conn.server :goog, :host => "google.com", :port => 80 @@ -70,11 +88,11 @@ end end it "should intercept & alter response from Google" do EM.run do - EventMachine.add_timer(2) do + EventMachine.add_timer(0.1) do http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1}) http.errback { failed } http.callback { http.response_header.status.should == 404 EventMachine.stop @@ -91,11 +109,11 @@ end end it "should invoke on_finish callback when connection is terminated" do EM.run do - EventMachine.add_timer(2) do + EventMachine.add_timer(0.1) do EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1}) end Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn| conn.server :goog, :host => "google.com", :port => 80 @@ -105,7 +123,43 @@ backend.should == :goog EventMachine.stop end end end + end + + it "should not invoke on_data when :relay_client is passed as server option" do + lambda { + EM.run do + EventMachine.add_timer(0.1) do + http =EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1}) + http.callback { EventMachine.stop } + end + + Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn| + conn.server :goog, :host => "google.com", :port => 80, :relay_client => true + conn.on_data { |data| raise "Should not be here"; data } + conn.on_response { |backend, resp| resp } + + end + end + }.should_not raise_error + end + + it "should not invoke on_response when :relay_server is passed as server option" do + lambda { + EM.run do + EventMachine.add_timer(0.1) do + http =EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1}) + http.callback { EventMachine.stop } + end + + Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn| + conn.server :goog, :host => "google.com", :port => 80, :relay_server => true + conn.on_data { |data| data } + conn.on_response { |backend, resp| raise "Should not be here"; } + + end + end + }.should_not raise_error end end \ No newline at end of file