Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Integration test of chunked streaming #209

Merged
merged 1 commit into from

2 participants

@robmckinnon
  • adds integration test of chunked streaming
  • thought others might find it useful to have an example of how to test chunked streaming
  • spec and setup is very similar to websocket_spec
@igrigorik igrigorik merged commit 0110cd8 into postrank-labs:master
@igrigorik
Owner

Nice - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 26, 2012
  1. integration test of chunked streaming

    Rob McKinnon authored
This page is out of date. Refresh to see the latest.
View
42 lib/goliath/test_helper_streaming.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
View
36 spec/integration/chunked_streaming_spec.rb
@@ -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
+
+
Something went wrong with that request. Please try again.