Integration test of chunked streaming #209

Merged
merged 1 commit into from Sep 28, 2012
Jump to file or symbol
Failed to load files and symbols.
+78 −0
Split
@@ -0,0 +1,42 @@
+require 'em-http-request'
+
+module Goliath
+ module TestHelper
+ class StreamingHelper
+ attr_reader :connection
+ def initialize(url)
+ @queue = EM::Queue.new
+
+ fiber = Fiber.current
+ @connection = EventMachine::HttpRequest.new(url).get
+ @connection.errback do |e|
+ puts "Error encountered during connection: #{e}"
+ EM::stop_event_loop
+ end
+
+ @connection.callback { EM::stop_event_loop }
+
+ @connection.stream { |m| @queue.push(m) }
+
+ Fiber.yield
+ end
+
+ def send(m)
+ @connection.send_msg(m)
+ end
+
+ def receive
+ fiber = Fiber.current
+ @queue.pop {|m| fiber.resume(m) }
+ Fiber.yield
+ end
+ end
+
+ def streaming_client_connect(path, &blk)
+ url = "http://localhost:#{@test_server_port}#{path}"
+ client = StreamingHelper.new(url)
+ blk.call(client) if blk
+ stop
+ end
+ end
+end
@@ -0,0 +1,36 @@
+require 'spec_helper'
+require File.join(File.dirname(__FILE__), '../../', 'lib/goliath/test_helper_streaming')
+require File.join(File.dirname(__FILE__), '../../', 'lib/goliath')
+
+class ChunkedStreaming < Goliath::API
+
+ def response(env)
+
+ EM.next_tick do
+ env.chunked_stream_send("chunked")
+ env.chunked_stream_close
+ end
+
+ headers = { 'Content-Type' => 'text/plain', 'X-Stream' => 'Goliath' }
+ chunked_streaming_response(200, headers)
+ end
+
+ def on_close(env)
+ end
+end
+
+describe "ChunkedStreaming" do
+ include Goliath::TestHelper
+
+ let(:err) { Proc.new { |c| fail "HTTP Request failed #{c.response}" } }
+
+ it "should stream content" do
+ with_api(ChunkedStreaming, {:verbose => true, :log_stdout => true}) do |server|
+ streaming_client_connect('/streaming') do |client|
+ client.receive.should == "chunked"
+ end
+ end
+ end
+end
+
+