Permalink
Browse files

integration test of chunked streaming

  • Loading branch information...
1 parent 9a6dc18 commit f9f447335d9789bc64e2597565f5a202219f5198 Rob McKinnon committed Sep 26, 2012
Showing with 78 additions and 0 deletions.
  1. +42 −0 lib/goliath/test_helper_streaming.rb
  2. +36 −0 spec/integration/chunked_streaming_spec.rb
@@ -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
+
+

0 comments on commit f9f4473

Please sign in to comment.