kjvarga-rack 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +18 -0
- data/KNOWN-ISSUES.rdoc +18 -0
- data/Manifest +117 -0
- data/README.rdoc +357 -0
- data/Rakefile +164 -0
- data/bin/rackup +176 -0
- data/contrib/rack_logo.svg +111 -0
- data/lib/rack.rb +90 -0
- data/lib/rack/adapter/camping.rb +22 -0
- data/lib/rack/auth/abstract/handler.rb +37 -0
- data/lib/rack/auth/abstract/request.rb +37 -0
- data/lib/rack/auth/basic.rb +58 -0
- data/lib/rack/auth/digest/md5.rb +124 -0
- data/lib/rack/auth/digest/nonce.rb +51 -0
- data/lib/rack/auth/digest/params.rb +55 -0
- data/lib/rack/auth/digest/request.rb +40 -0
- data/lib/rack/auth/openid.rb +487 -0
- data/lib/rack/builder.rb +63 -0
- data/lib/rack/cascade.rb +41 -0
- data/lib/rack/chunked.rb +49 -0
- data/lib/rack/commonlogger.rb +52 -0
- data/lib/rack/conditionalget.rb +47 -0
- data/lib/rack/content_length.rb +29 -0
- data/lib/rack/content_type.rb +23 -0
- data/lib/rack/deflater.rb +96 -0
- data/lib/rack/directory.rb +153 -0
- data/lib/rack/file.rb +88 -0
- data/lib/rack/handler.rb +69 -0
- data/lib/rack/handler/cgi.rb +61 -0
- data/lib/rack/handler/evented_mongrel.rb +8 -0
- data/lib/rack/handler/fastcgi.rb +88 -0
- data/lib/rack/handler/lsws.rb +60 -0
- data/lib/rack/handler/mongrel.rb +87 -0
- data/lib/rack/handler/scgi.rb +62 -0
- data/lib/rack/handler/swiftiplied_mongrel.rb +8 -0
- data/lib/rack/handler/thin.rb +18 -0
- data/lib/rack/handler/webrick.rb +71 -0
- data/lib/rack/head.rb +19 -0
- data/lib/rack/lint.rb +546 -0
- data/lib/rack/lobster.rb +65 -0
- data/lib/rack/lock.rb +16 -0
- data/lib/rack/methodoverride.rb +27 -0
- data/lib/rack/mime.rb +205 -0
- data/lib/rack/mock.rb +187 -0
- data/lib/rack/recursive.rb +57 -0
- data/lib/rack/reloader.rb +109 -0
- data/lib/rack/request.rb +248 -0
- data/lib/rack/response.rb +183 -0
- data/lib/rack/rewindable_input.rb +100 -0
- data/lib/rack/session/abstract/id.rb +142 -0
- data/lib/rack/session/cookie.rb +91 -0
- data/lib/rack/session/memcache.rb +109 -0
- data/lib/rack/session/pool.rb +100 -0
- data/lib/rack/showexceptions.rb +349 -0
- data/lib/rack/showstatus.rb +106 -0
- data/lib/rack/static.rb +38 -0
- data/lib/rack/urlmap.rb +55 -0
- data/lib/rack/utils.rb +528 -0
- data/rack.gemspec +140 -0
- data/test/cgi/lighttpd.conf +20 -0
- data/test/cgi/test +9 -0
- data/test/cgi/test.fcgi +8 -0
- data/test/cgi/test.ru +7 -0
- data/test/multipart/binary +0 -0
- data/test/multipart/empty +10 -0
- data/test/multipart/file1.txt +1 -0
- data/test/multipart/ie +6 -0
- data/test/multipart/nested +10 -0
- data/test/multipart/none +9 -0
- data/test/multipart/text +10 -0
- data/test/spec_rack_auth_basic.rb +73 -0
- data/test/spec_rack_auth_digest.rb +226 -0
- data/test/spec_rack_auth_openid.rb +84 -0
- data/test/spec_rack_builder.rb +84 -0
- data/test/spec_rack_camping.rb +51 -0
- data/test/spec_rack_cascade.rb +48 -0
- data/test/spec_rack_cgi.rb +89 -0
- data/test/spec_rack_chunked.rb +62 -0
- data/test/spec_rack_commonlogger.rb +61 -0
- data/test/spec_rack_conditionalget.rb +41 -0
- data/test/spec_rack_content_length.rb +43 -0
- data/test/spec_rack_content_type.rb +30 -0
- data/test/spec_rack_deflater.rb +127 -0
- data/test/spec_rack_directory.rb +61 -0
- data/test/spec_rack_fastcgi.rb +89 -0
- data/test/spec_rack_file.rb +75 -0
- data/test/spec_rack_handler.rb +43 -0
- data/test/spec_rack_head.rb +30 -0
- data/test/spec_rack_lint.rb +521 -0
- data/test/spec_rack_lobster.rb +45 -0
- data/test/spec_rack_lock.rb +38 -0
- data/test/spec_rack_methodoverride.rb +60 -0
- data/test/spec_rack_mock.rb +243 -0
- data/test/spec_rack_mongrel.rb +189 -0
- data/test/spec_rack_recursive.rb +77 -0
- data/test/spec_rack_request.rb +504 -0
- data/test/spec_rack_response.rb +218 -0
- data/test/spec_rack_rewindable_input.rb +118 -0
- data/test/spec_rack_session_cookie.rb +82 -0
- data/test/spec_rack_session_memcache.rb +250 -0
- data/test/spec_rack_session_pool.rb +172 -0
- data/test/spec_rack_showexceptions.rb +21 -0
- data/test/spec_rack_showstatus.rb +72 -0
- data/test/spec_rack_static.rb +37 -0
- data/test/spec_rack_thin.rb +91 -0
- data/test/spec_rack_urlmap.rb +185 -0
- data/test/spec_rack_utils.rb +467 -0
- data/test/spec_rack_webrick.rb +130 -0
- data/test/testrequest.rb +57 -0
- data/test/unregistered_handler/rack/handler/unregistered.rb +7 -0
- data/test/unregistered_handler/rack/handler/unregistered_long_one.rb +7 -0
- metadata +175 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'test/spec'
|
2
|
+
|
3
|
+
begin
|
4
|
+
# requires the ruby-openid gem
|
5
|
+
require 'rack/auth/openid'
|
6
|
+
|
7
|
+
context "Rack::Auth::OpenID" do
|
8
|
+
OID = Rack::Auth::OpenID
|
9
|
+
host = 'host'
|
10
|
+
subd = 'sub.host'
|
11
|
+
wild = '*.host'
|
12
|
+
path = 'path'
|
13
|
+
long = 'path/long'
|
14
|
+
scheme = 'http://'
|
15
|
+
realm = scheme+host+'/'+path
|
16
|
+
|
17
|
+
specify 'realm uri should be valid' do
|
18
|
+
lambda{OID.new('/'+path)}.should.raise ArgumentError
|
19
|
+
lambda{OID.new('/'+long)}.should.raise ArgumentError
|
20
|
+
lambda{OID.new(scheme+host)}.should.not.raise
|
21
|
+
lambda{OID.new(scheme+host+'/')}.should.not.raise
|
22
|
+
lambda{OID.new(scheme+host+'/'+path)}.should.not.raise
|
23
|
+
lambda{OID.new(scheme+subd)}.should.not.raise
|
24
|
+
lambda{OID.new(scheme+subd+'/')}.should.not.raise
|
25
|
+
lambda{OID.new(scheme+subd+'/'+path)}.should.not.raise
|
26
|
+
end
|
27
|
+
|
28
|
+
specify 'should be able to check if a uri is within the realm' do
|
29
|
+
end
|
30
|
+
|
31
|
+
specify 'return_to should be valid' do
|
32
|
+
uri = '/'+path
|
33
|
+
lambda{OID.new(realm, :return_to=>uri)}.should.raise ArgumentError
|
34
|
+
uri = '/'+long
|
35
|
+
lambda{OID.new(realm, :return_to=>uri)}.should.raise ArgumentError
|
36
|
+
uri = scheme+host
|
37
|
+
lambda{OID.new(realm, :return_to=>uri)}.should.raise ArgumentError
|
38
|
+
uri = scheme+host+'/'+path
|
39
|
+
lambda{OID.new(realm, :return_to=>uri)}.should.not.raise
|
40
|
+
uri = scheme+subd+'/'+path
|
41
|
+
lambda{OID.new(realm, :return_to=>uri)}.should.raise ArgumentError
|
42
|
+
uri = scheme+host+'/'+long
|
43
|
+
lambda{OID.new(realm, :return_to=>uri)}.should.not.raise
|
44
|
+
uri = scheme+subd+'/'+long
|
45
|
+
lambda{OID.new(realm, :return_to=>uri)}.should.raise ArgumentError
|
46
|
+
end
|
47
|
+
|
48
|
+
specify 'extensions should have required constants defined' do
|
49
|
+
badext = Rack::Auth::OpenID::BadExtension
|
50
|
+
ext = Object.new
|
51
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
52
|
+
ext = Module.new
|
53
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
54
|
+
ext::Request = nil
|
55
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
56
|
+
ext::Response = nil
|
57
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
58
|
+
ext::NS_URI = nil
|
59
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
60
|
+
end
|
61
|
+
|
62
|
+
specify 'extensions should have Request and Response defined and inherit from OpenID::Extension' do
|
63
|
+
$-w, w = nil, $-w # yuck
|
64
|
+
badext = Rack::Auth::OpenID::BadExtension
|
65
|
+
ext = Module.new
|
66
|
+
ext::Request = nil
|
67
|
+
ext::Response = nil
|
68
|
+
ext::NS_URI = nil
|
69
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
70
|
+
ext::Request = Class.new()
|
71
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
72
|
+
ext::Response = Class.new()
|
73
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
74
|
+
ext::Request = Class.new(::OpenID::Extension)
|
75
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
76
|
+
ext::Response = Class.new(::OpenID::Extension)
|
77
|
+
lambda{OID.new(realm).add_extension(ext)}.should.raise(badext)
|
78
|
+
$-w = w
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
rescue LoadError
|
83
|
+
$stderr.puts "Skipping Rack::Auth::OpenID tests (ruby-openid 2 is required). `gem install ruby-openid` and try again."
|
84
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'test/spec'
|
2
|
+
|
3
|
+
require 'rack/builder'
|
4
|
+
require 'rack/mock'
|
5
|
+
require 'rack/showexceptions'
|
6
|
+
require 'rack/auth/basic'
|
7
|
+
|
8
|
+
context "Rack::Builder" do
|
9
|
+
specify "chains apps by default" do
|
10
|
+
app = Rack::Builder.new do
|
11
|
+
use Rack::ShowExceptions
|
12
|
+
run lambda { |env| raise "bzzzt" }
|
13
|
+
end.to_app
|
14
|
+
|
15
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
16
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
17
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
18
|
+
end
|
19
|
+
|
20
|
+
specify "has implicit #to_app" do
|
21
|
+
app = Rack::Builder.new do
|
22
|
+
use Rack::ShowExceptions
|
23
|
+
run lambda { |env| raise "bzzzt" }
|
24
|
+
end
|
25
|
+
|
26
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
27
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
28
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
29
|
+
end
|
30
|
+
|
31
|
+
specify "supports blocks on use" do
|
32
|
+
app = Rack::Builder.new do
|
33
|
+
use Rack::ShowExceptions
|
34
|
+
use Rack::Auth::Basic do |username, password|
|
35
|
+
'secret' == password
|
36
|
+
end
|
37
|
+
|
38
|
+
run lambda { |env| [200, {}, ['Hi Boss']] }
|
39
|
+
end
|
40
|
+
|
41
|
+
response = Rack::MockRequest.new(app).get("/")
|
42
|
+
response.should.be.client_error
|
43
|
+
response.status.should.equal 401
|
44
|
+
|
45
|
+
# with auth...
|
46
|
+
response = Rack::MockRequest.new(app).get("/",
|
47
|
+
'HTTP_AUTHORIZATION' => 'Basic ' + ["joe:secret"].pack("m*"))
|
48
|
+
response.status.should.equal 200
|
49
|
+
response.body.to_s.should.equal 'Hi Boss'
|
50
|
+
end
|
51
|
+
|
52
|
+
specify "has explicit #to_app" do
|
53
|
+
app = Rack::Builder.app do
|
54
|
+
use Rack::ShowExceptions
|
55
|
+
run lambda { |env| raise "bzzzt" }
|
56
|
+
end
|
57
|
+
|
58
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
59
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
60
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
61
|
+
end
|
62
|
+
|
63
|
+
specify "apps are initialized once" do
|
64
|
+
app = Rack::Builder.new do
|
65
|
+
class AppClass
|
66
|
+
def initialize
|
67
|
+
@called = 0
|
68
|
+
end
|
69
|
+
def call(env)
|
70
|
+
raise "bzzzt" if @called > 0
|
71
|
+
@called += 1
|
72
|
+
[200, {'Content-Type' => 'text/plain'}, ['OK']]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
use Rack::ShowExceptions
|
77
|
+
run AppClass.new
|
78
|
+
end
|
79
|
+
|
80
|
+
Rack::MockRequest.new(app).get("/").status.should.equal 200
|
81
|
+
Rack::MockRequest.new(app).get("/").should.be.server_error
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test/spec'
|
2
|
+
require 'stringio'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'rack/mock'
|
7
|
+
|
8
|
+
$-w, w = nil, $-w # yuck
|
9
|
+
require 'camping'
|
10
|
+
require 'rack/adapter/camping'
|
11
|
+
|
12
|
+
Camping.goes :CampApp
|
13
|
+
module CampApp
|
14
|
+
module Controllers
|
15
|
+
class HW < R('/')
|
16
|
+
def get
|
17
|
+
@headers["X-Served-By"] = URI("http://rack.rubyforge.org")
|
18
|
+
"Camping works!"
|
19
|
+
end
|
20
|
+
|
21
|
+
def post
|
22
|
+
"Data: #{input.foo}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
$-w = w
|
28
|
+
|
29
|
+
context "Rack::Adapter::Camping" do
|
30
|
+
specify "works with GET" do
|
31
|
+
res = Rack::MockRequest.new(Rack::Adapter::Camping.new(CampApp)).
|
32
|
+
get("/")
|
33
|
+
|
34
|
+
res.should.be.ok
|
35
|
+
res["Content-Type"].should.equal "text/html"
|
36
|
+
res["X-Served-By"].should.equal "http://rack.rubyforge.org"
|
37
|
+
|
38
|
+
res.body.should.equal "Camping works!"
|
39
|
+
end
|
40
|
+
|
41
|
+
specify "works with POST" do
|
42
|
+
res = Rack::MockRequest.new(Rack::Adapter::Camping.new(CampApp)).
|
43
|
+
post("/", :input => "foo=bar")
|
44
|
+
|
45
|
+
res.should.be.ok
|
46
|
+
res.body.should.equal "Data: bar"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
rescue LoadError
|
50
|
+
$stderr.puts "Skipping Rack::Adapter::Camping tests (Camping is required). `gem install camping` and try again."
|
51
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test/spec'
|
2
|
+
|
3
|
+
require 'rack/cascade'
|
4
|
+
require 'rack/mock'
|
5
|
+
|
6
|
+
require 'rack/urlmap'
|
7
|
+
require 'rack/file'
|
8
|
+
|
9
|
+
context "Rack::Cascade" do
|
10
|
+
docroot = File.expand_path(File.dirname(__FILE__))
|
11
|
+
app1 = Rack::File.new(docroot)
|
12
|
+
|
13
|
+
app2 = Rack::URLMap.new("/crash" => lambda { |env| raise "boom" })
|
14
|
+
|
15
|
+
app3 = Rack::URLMap.new("/foo" => lambda { |env|
|
16
|
+
[200, { "Content-Type" => "text/plain"}, [""]]})
|
17
|
+
|
18
|
+
specify "should dispatch onward on 404 by default" do
|
19
|
+
cascade = Rack::Cascade.new([app1, app2, app3])
|
20
|
+
Rack::MockRequest.new(cascade).get("/cgi/test").should.be.ok
|
21
|
+
Rack::MockRequest.new(cascade).get("/foo").should.be.ok
|
22
|
+
Rack::MockRequest.new(cascade).get("/toobad").should.be.not_found
|
23
|
+
Rack::MockRequest.new(cascade).get("/cgi/../bla").should.be.forbidden
|
24
|
+
end
|
25
|
+
|
26
|
+
specify "should dispatch onward on whatever is passed" do
|
27
|
+
cascade = Rack::Cascade.new([app1, app2, app3], [404, 403])
|
28
|
+
Rack::MockRequest.new(cascade).get("/cgi/../bla").should.be.not_found
|
29
|
+
end
|
30
|
+
|
31
|
+
specify "should return 404 if empty" do
|
32
|
+
Rack::MockRequest.new(Rack::Cascade.new([])).get('/').should.be.not_found
|
33
|
+
end
|
34
|
+
|
35
|
+
specify "should append new app" do
|
36
|
+
cascade = Rack::Cascade.new([], [404, 403])
|
37
|
+
Rack::MockRequest.new(cascade).get('/').should.be.not_found
|
38
|
+
cascade << app2
|
39
|
+
Rack::MockRequest.new(cascade).get('/cgi/test').should.be.not_found
|
40
|
+
Rack::MockRequest.new(cascade).get('/cgi/../bla').should.be.not_found
|
41
|
+
cascade << app1
|
42
|
+
Rack::MockRequest.new(cascade).get('/cgi/test').should.be.ok
|
43
|
+
Rack::MockRequest.new(cascade).get('/cgi/../bla').should.be.forbidden
|
44
|
+
Rack::MockRequest.new(cascade).get('/foo').should.be.not_found
|
45
|
+
cascade << app3
|
46
|
+
Rack::MockRequest.new(cascade).get('/foo').should.be.ok
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'test/spec'
|
2
|
+
require 'testrequest'
|
3
|
+
|
4
|
+
context "Rack::Handler::CGI" do
|
5
|
+
include TestRequest::Helpers
|
6
|
+
|
7
|
+
setup do
|
8
|
+
@host = '0.0.0.0'
|
9
|
+
@port = 9203
|
10
|
+
end
|
11
|
+
|
12
|
+
# Keep this first.
|
13
|
+
specify "startup" do
|
14
|
+
$pid = fork {
|
15
|
+
Dir.chdir(File.join(File.dirname(__FILE__), "..", "test", "cgi"))
|
16
|
+
exec "lighttpd -D -f lighttpd.conf"
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
specify "should respond" do
|
21
|
+
sleep 1
|
22
|
+
lambda {
|
23
|
+
GET("/test")
|
24
|
+
}.should.not.raise
|
25
|
+
end
|
26
|
+
|
27
|
+
specify "should be a lighttpd" do
|
28
|
+
GET("/test")
|
29
|
+
status.should.be 200
|
30
|
+
response["SERVER_SOFTWARE"].should =~ /lighttpd/
|
31
|
+
response["HTTP_VERSION"].should.equal "HTTP/1.1"
|
32
|
+
response["SERVER_PROTOCOL"].should.equal "HTTP/1.1"
|
33
|
+
response["SERVER_PORT"].should.equal @port.to_s
|
34
|
+
response["SERVER_NAME"].should =~ @host
|
35
|
+
end
|
36
|
+
|
37
|
+
specify "should have rack headers" do
|
38
|
+
GET("/test")
|
39
|
+
response["rack.version"].should.equal [1,0]
|
40
|
+
response["rack.multithread"].should.be false
|
41
|
+
response["rack.multiprocess"].should.be true
|
42
|
+
response["rack.run_once"].should.be true
|
43
|
+
end
|
44
|
+
|
45
|
+
specify "should have CGI headers on GET" do
|
46
|
+
GET("/test")
|
47
|
+
response["REQUEST_METHOD"].should.equal "GET"
|
48
|
+
response["SCRIPT_NAME"].should.equal "/test"
|
49
|
+
response["REQUEST_PATH"].should.equal "/"
|
50
|
+
response["PATH_INFO"].should.be.nil
|
51
|
+
response["QUERY_STRING"].should.equal ""
|
52
|
+
response["test.postdata"].should.equal ""
|
53
|
+
|
54
|
+
GET("/test/foo?quux=1")
|
55
|
+
response["REQUEST_METHOD"].should.equal "GET"
|
56
|
+
response["SCRIPT_NAME"].should.equal "/test"
|
57
|
+
response["REQUEST_PATH"].should.equal "/"
|
58
|
+
response["PATH_INFO"].should.equal "/foo"
|
59
|
+
response["QUERY_STRING"].should.equal "quux=1"
|
60
|
+
end
|
61
|
+
|
62
|
+
specify "should have CGI headers on POST" do
|
63
|
+
POST("/test", {"rack-form-data" => "23"}, {'X-test-header' => '42'})
|
64
|
+
status.should.equal 200
|
65
|
+
response["REQUEST_METHOD"].should.equal "POST"
|
66
|
+
response["SCRIPT_NAME"].should.equal "/test"
|
67
|
+
response["REQUEST_PATH"].should.equal "/"
|
68
|
+
response["QUERY_STRING"].should.equal ""
|
69
|
+
response["HTTP_X_TEST_HEADER"].should.equal "42"
|
70
|
+
response["test.postdata"].should.equal "rack-form-data=23"
|
71
|
+
end
|
72
|
+
|
73
|
+
specify "should support HTTP auth" do
|
74
|
+
GET("/test", {:user => "ruth", :passwd => "secret"})
|
75
|
+
response["HTTP_AUTHORIZATION"].should.equal "Basic cnV0aDpzZWNyZXQ="
|
76
|
+
end
|
77
|
+
|
78
|
+
specify "should set status" do
|
79
|
+
GET("/test?secret")
|
80
|
+
status.should.equal 403
|
81
|
+
response["rack.url_scheme"].should.equal "http"
|
82
|
+
end
|
83
|
+
|
84
|
+
# Keep this last.
|
85
|
+
specify "shutdown" do
|
86
|
+
Process.kill 15, $pid
|
87
|
+
Process.wait($pid).should.equal $pid
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rack/mock'
|
2
|
+
require 'rack/chunked'
|
3
|
+
require 'rack/utils'
|
4
|
+
|
5
|
+
context "Rack::Chunked" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@env = Rack::MockRequest.
|
9
|
+
env_for('/', 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET')
|
10
|
+
end
|
11
|
+
|
12
|
+
specify 'chunks responses with no Content-Length' do
|
13
|
+
app = lambda { |env| [200, {}, ['Hello', ' ', 'World!']] }
|
14
|
+
response = Rack::MockResponse.new(*Rack::Chunked.new(app).call(@env))
|
15
|
+
response.headers.should.not.include 'Content-Length'
|
16
|
+
response.headers['Transfer-Encoding'].should.equal 'chunked'
|
17
|
+
response.body.should.equal "5\r\nHello\r\n1\r\n \r\n6\r\nWorld!\r\n0\r\n\r\n"
|
18
|
+
end
|
19
|
+
|
20
|
+
specify 'chunks empty bodies properly' do
|
21
|
+
app = lambda { |env| [200, {}, []] }
|
22
|
+
response = Rack::MockResponse.new(*Rack::Chunked.new(app).call(@env))
|
23
|
+
response.headers.should.not.include 'Content-Length'
|
24
|
+
response.headers['Transfer-Encoding'].should.equal 'chunked'
|
25
|
+
response.body.should.equal "0\r\n\r\n"
|
26
|
+
end
|
27
|
+
|
28
|
+
specify 'does not modify response when Content-Length header present' do
|
29
|
+
app = lambda { |env| [200, {'Content-Length'=>'12'}, ['Hello', ' ', 'World!']] }
|
30
|
+
status, headers, body = Rack::Chunked.new(app).call(@env)
|
31
|
+
status.should.equal 200
|
32
|
+
headers.should.not.include 'Transfer-Encoding'
|
33
|
+
headers.should.include 'Content-Length'
|
34
|
+
body.join.should.equal 'Hello World!'
|
35
|
+
end
|
36
|
+
|
37
|
+
specify 'does not modify response when client is HTTP/1.0' do
|
38
|
+
app = lambda { |env| [200, {}, ['Hello', ' ', 'World!']] }
|
39
|
+
@env['HTTP_VERSION'] = 'HTTP/1.0'
|
40
|
+
status, headers, body = Rack::Chunked.new(app).call(@env)
|
41
|
+
status.should.equal 200
|
42
|
+
headers.should.not.include 'Transfer-Encoding'
|
43
|
+
body.join.should.equal 'Hello World!'
|
44
|
+
end
|
45
|
+
|
46
|
+
specify 'does not modify response when Transfer-Encoding header already present' do
|
47
|
+
app = lambda { |env| [200, {'Transfer-Encoding' => 'identity'}, ['Hello', ' ', 'World!']] }
|
48
|
+
status, headers, body = Rack::Chunked.new(app).call(@env)
|
49
|
+
status.should.equal 200
|
50
|
+
headers['Transfer-Encoding'].should.equal 'identity'
|
51
|
+
body.join.should.equal 'Hello World!'
|
52
|
+
end
|
53
|
+
|
54
|
+
[100, 204, 304].each do |status_code|
|
55
|
+
specify "does not modify response when status code is #{status_code}" do
|
56
|
+
app = lambda { |env| [status_code, {}, []] }
|
57
|
+
status, headers, body = Rack::Chunked.new(app).call(@env)
|
58
|
+
status.should.equal status_code
|
59
|
+
headers.should.not.include 'Transfer-Encoding'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test/spec'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
require 'rack/commonlogger'
|
5
|
+
require 'rack/lobster'
|
6
|
+
require 'rack/mock'
|
7
|
+
|
8
|
+
context "Rack::CommonLogger" do
|
9
|
+
app = lambda { |env|
|
10
|
+
[200,
|
11
|
+
{"Content-Type" => "text/html", "Content-Length" => length.to_s},
|
12
|
+
[obj]]}
|
13
|
+
app_without_length = lambda { |env|
|
14
|
+
[200,
|
15
|
+
{"Content-Type" => "text/html"},
|
16
|
+
[]]}
|
17
|
+
app_with_zero_length = lambda { |env|
|
18
|
+
[200,
|
19
|
+
{"Content-Type" => "text/html", "Content-Length" => "0"},
|
20
|
+
[]]}
|
21
|
+
|
22
|
+
specify "should log to rack.errors by default" do
|
23
|
+
res = Rack::MockRequest.new(Rack::CommonLogger.new(app)).get("/")
|
24
|
+
|
25
|
+
res.errors.should.not.be.empty
|
26
|
+
res.errors.should =~ /"GET \/ " 200 #{length} /
|
27
|
+
end
|
28
|
+
|
29
|
+
specify "should log to anything with +write+" do
|
30
|
+
log = StringIO.new
|
31
|
+
res = Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")
|
32
|
+
|
33
|
+
log.string.should =~ /"GET \/ " 200 #{length} /
|
34
|
+
end
|
35
|
+
|
36
|
+
specify "should log - content length if header is missing" do
|
37
|
+
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_without_length)).get("/")
|
38
|
+
|
39
|
+
res.errors.should.not.be.empty
|
40
|
+
res.errors.should =~ /"GET \/ " 200 - /
|
41
|
+
end
|
42
|
+
|
43
|
+
specify "should log - content length if header is zero" do
|
44
|
+
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_with_zero_length)).get("/")
|
45
|
+
|
46
|
+
res.errors.should.not.be.empty
|
47
|
+
res.errors.should =~ /"GET \/ " 200 - /
|
48
|
+
end
|
49
|
+
|
50
|
+
def length
|
51
|
+
self.class.length
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.length
|
55
|
+
123
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.obj
|
59
|
+
"hello world"
|
60
|
+
end
|
61
|
+
end
|