forked from jnewland/sinatra-rubygems
/
spec_helper.rb
57 lines (50 loc) · 1.61 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'rubygems'
require 'rubygems/server'
require 'sinatra/test'
require 'sinatra/test/unit'
require 'spec'
require 'spec/interop/test'
require 'stringio'
require 'webrick'
require File.expand_path(File.dirname(__FILE__) + "/../lib/rack_rubygems.rb")
Sinatra::Default.set(
:environment => :test,
:run => false,
:raise_errors => true,
:logging => true
)
module RackRubygemsTestHelpers
def should_match_webrick_behavior(url, server_method, method = :get)
#webrick
data = StringIO.new "#{method.to_s.capitalize} #{url} HTTP/1.0\r\n\r\n"
@webrick_request.parse data
@webrick.send(server_method, @webrick_request, @webrick_response)
#sinatra
send(method, url)
@response.should be_ok
#verify
{
:status => @response.status,
:content_type => @response['Content-Type'],
:body_length => ((method == :head) ? nil : @response.body.length )
}.should == {
:status => @webrick_response.status,
:content_type => @webrick_response['Content-Type'],
:body_length => ((method == :head) ? nil : @webrick_response.body.length )
}
end
end
Spec::Runner.configure do |config|
config.before(:each) {
@app = Rack::Builder.new {
use GemsAndRdocs, :urls => ['/cache', '/doc'], :root => Gem.dir
use Rack::Compress
run RackRubygems.new
}
@webrick = Gem::Server.new Gem.dir, (8000 + $$ % 1000), false
@webrick_request = WEBrick::HTTPRequest.new :Logger => nil
@webrick_response = WEBrick::HTTPResponse.new :HTTPVersion => '1.0'
}
config.include Sinatra::Test
config.include RackRubygemsTestHelpers
end