Skip to content

Instantly share code, notes, and snippets.

@ilnurnasyrov2
Forked from xaviershay/http_client_spec.rb
Created October 30, 2018 14:23
Show Gist options
  • Save ilnurnasyrov2/9bc88b6c416368ef98d9d12ab0805f5c to your computer and use it in GitHub Desktop.
Save ilnurnasyrov2/9bc88b6c416368ef98d9d12ab0805f5c to your computer and use it in GitHub Desktop.
Running a rack app in a thread for integration tests.
require 'integration_helper'
require 'rack'
require 'rack/handler/webrick'
describe HttpClient do
before :all do
@server = WEBrick::HTTPServer.new(
:Port => 9293,
:Logger => Rails.logger,
:AccessLog => Rails.logger
)
@server.mount '/', Rack::Handler::WEBrick, lambda {|env|
requested_response_code = env['REQUEST_PATH'][1..-1].to_i
[requested_response_code, {}, ['{}']]
}
Thread.new do
@server.start
end
start_time = Time.now
while !listen_on_port?(9293) && Time.now < start_time + 1.second
end
raise "Could not start server" if !listen_on_port?(9293)
end
after :all do
@server.shutdown
end
let(:http) { HttpClient.new("http://localhost:9293/") }
shared_examples_for 'an http method' do
it 'raises FailedRequest for non-successful requests' do
lambda { perform("/404") }.should raise_error(HttpClient::FailedRequest)
lambda { perform("/500") }.should raise_error(HttpClient::FailedRequest)
end
it 'does not raise for successful requests' do
lambda { perform("/200") }.should_not raise_error(HttpClient::FailedRequest)
end
end
describe '#get' do
def perform(path)
http.get(path, {})
end
it_should_behave_like 'an http method'
it 'should return response with body' do
perform('/200').body.should == '{}'
end
end
def listen_on_port?(port)
`lsof -i :#{port}`
$? == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment