From 1fd4d6094b514646ce5f406fc702ac639aeaa899 Mon Sep 17 00:00:00 2001 From: JT Olds Date: Fri, 9 Dec 2011 14:39:30 -0700 Subject: [PATCH] allow test http server to receive multiple requests test plan: n/a Change-Id: I1d22e26dbc8bd1511bd91f0605de669034e42e1d Reviewed-on: https://gerrit.instructure.com/7397 Reviewed-by: Cody Cutrer Tested-by: Hudson --- spec/spec_helper.rb | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index efb062ba7b1..94fefb30181 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -507,25 +507,27 @@ Spec::Runner.configure do |config| ActionController::Base.class_eval { alias_method :allow_forgery_protection, :_old_protect } end - def start_test_http_server + def start_test_http_server(requests=1) post_lines = [] server = TCPServer.open(0) port = server.addr[1] post_lines = [] server_thread = Thread.new(server, post_lines) do |server, post_lines| - client = server.accept - content_length = 0 - loop do - line = client.readline - post_lines << line.strip unless line =~ /\AHost: localhost:|\AContent-Length: / - content_length = line.split(":")[1].to_i if line.strip =~ /\AContent-Length: [0-9]+\z/ - if line.strip.blank? - post_lines << client.read(content_length) - break + requests.times do + client = server.accept + content_length = 0 + loop do + line = client.readline + post_lines << line.strip unless line =~ /\AHost: localhost:|\AContent-Length: / + content_length = line.split(":")[1].to_i if line.strip =~ /\AContent-Length: [0-9]+\z/ + if line.strip.blank? + post_lines << client.read(content_length) + break + end end + client.puts("HTTP/1.1 200 OK\nContent-Length: 0\n\n") + client.close end - client.puts("HTTP/1.1 200 OK\nContent-Length: 0\n\n") - client.close server.close end return server, server_thread, post_lines