rack-contrib 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+
4
+ begin
5
+ require 'tmail'
6
+ require 'rack/contrib/mailexceptions'
7
+
8
+ require File.dirname(__FILE__) + '/mail_settings.rb'
9
+
10
+ class TestError < RuntimeError
11
+ end
12
+
13
+ def test_exception
14
+ raise TestError, 'Suffering Succotash!'
15
+ rescue => boom
16
+ return boom
17
+ end
18
+
19
+ context 'Rack::MailExceptions' do
20
+
21
+ setup do
22
+ @app = lambda { |env| raise TestError, 'Why, I say' }
23
+ @env = Rack::MockRequest.env_for("/foo",
24
+ 'FOO' => 'BAR',
25
+ :method => 'GET',
26
+ :input => 'THE BODY'
27
+ )
28
+ @smtp_settings = {
29
+ :server => 'example.com',
30
+ :domain => 'example.com',
31
+ :port => 500,
32
+ :authentication => :login,
33
+ :user_name => 'joe',
34
+ :password => 'secret'
35
+ }
36
+ end
37
+
38
+ specify 'yields a configuration object to the block when created' do
39
+ called = false
40
+ mailer =
41
+ Rack::MailExceptions.new(@app) do |mail|
42
+ called = true
43
+ mail.to 'foo@example.org'
44
+ mail.from 'bar@example.org'
45
+ mail.subject '[ERROR] %s'
46
+ mail.smtp @smtp_settings
47
+ end
48
+ called.should.be == true
49
+ end
50
+
51
+ specify 'generates a TMail object with configured settings' do
52
+ mailer =
53
+ Rack::MailExceptions.new(@app) do |mail|
54
+ mail.to 'foo@example.org'
55
+ mail.from 'bar@example.org'
56
+ mail.subject '[ERROR] %s'
57
+ mail.smtp @smtp_settings
58
+ end
59
+
60
+ tmail = mailer.send(:generate_mail, test_exception, @env)
61
+ tmail.to.should.equal ['foo@example.org']
62
+ tmail.from.should.equal ['bar@example.org']
63
+ tmail.subject.should.equal '[ERROR] Suffering Succotash!'
64
+ tmail.body.should.not.be.nil
65
+ tmail.body.should.be =~ /FOO:\s+"BAR"/
66
+ tmail.body.should.be =~ /^\s*THE BODY\s*$/
67
+ end
68
+
69
+ specify 'catches exceptions raised from app, sends mail, and re-raises' do
70
+ mailer =
71
+ Rack::MailExceptions.new(@app) do |mail|
72
+ mail.to 'foo@example.org'
73
+ mail.from 'bar@example.org'
74
+ mail.subject '[ERROR] %s'
75
+ mail.smtp @smtp_settings
76
+ end
77
+ lambda { mailer.call(@env) }.should.raise(TestError)
78
+ @env['mail.sent'].should.be == true
79
+ end
80
+
81
+ if TEST_SMTP && ! TEST_SMTP.empty?
82
+ specify 'sends mail' do
83
+ mailer =
84
+ Rack::MailExceptions.new(@app) do |mail|
85
+ mail.config.merge! TEST_SMTP
86
+ end
87
+ lambda { mailer.call(@env) }.should.raise(TestError)
88
+ @env['mail.sent'].should.be == true
89
+ end
90
+ else
91
+ STDERR.puts 'WARN: Skipping SMTP tests (edit test/mail_settings.rb to enable)'
92
+ end
93
+
94
+ end
95
+ rescue LoadError => boom
96
+ STDERR.puts "WARN: Skipping Rack::MailExceptions tests (tmail not installed)"
97
+ end
@@ -0,0 +1,46 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/nested_params'
4
+ require 'rack/methodoverride'
5
+
6
+ context Rack::NestedParams do
7
+
8
+ App = lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env)] }
9
+
10
+ def env_for_post_with_headers(path, headers, body)
11
+ Rack::MockRequest.env_for(path, {:method => "POST", :input => body}.merge(headers))
12
+ end
13
+
14
+ def form_post(params, content_type = 'application/x-www-form-urlencoded')
15
+ params = Rack::Utils.build_query(params) if Hash === params
16
+ env_for_post_with_headers('/', {'CONTENT_TYPE' => content_type}, params)
17
+ end
18
+
19
+ def middleware
20
+ Rack::NestedParams.new(App)
21
+ end
22
+
23
+ specify "should handle requests with POST body Content-Type of application/x-www-form-urlencoded" do
24
+ req = middleware.call(form_post({'foo[bar][baz]' => 'nested'})).last
25
+ req.POST.should.equal({"foo" => { "bar" => { "baz" => "nested" }}})
26
+ end
27
+
28
+ specify "should not parse requests with other Content-Type" do
29
+ req = middleware.call(form_post({'foo[bar][baz]' => 'nested'}, 'text/plain')).last
30
+ req.POST.should.equal({})
31
+ end
32
+
33
+ specify "should work even after another middleware already parsed the request" do
34
+ app = Rack::MethodOverride.new(middleware)
35
+ req = app.call(form_post({'_method' => 'put', 'foo[bar]' => 'nested'})).last
36
+ req.POST.should.equal({'_method' => 'put', "foo" => { "bar" => "nested" }})
37
+ req.put?.should.equal true
38
+ end
39
+
40
+ specify "should make first boolean have precedence even after request already parsed" do
41
+ app = Rack::MethodOverride.new(middleware)
42
+ req = app.call(form_post("foo=1&foo=0")).last
43
+ req.POST.should.equal({"foo" => '1'})
44
+ end
45
+
46
+ end
@@ -0,0 +1,32 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+
4
+ begin
5
+ require 'rack/contrib/post_body_content_type_parser'
6
+
7
+ context "Rack::PostBodyContentTypeParser" do
8
+
9
+ specify "should handle requests with POST body Content-Type of application/json" do
10
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env).POST] }
11
+ env = env_for_post_with_headers('/', {'Content_Type'.upcase => 'application/json'}, {:body => "asdf", :status => "12"}.to_json)
12
+ body = Rack::PostBodyContentTypeParser.new(app).call(env).last
13
+ body['body'].should.equal "asdf"
14
+ body['status'].should.equal "12"
15
+ end
16
+
17
+ specify "should change nothing when the POST body content type isn't application/json" do
18
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env).POST] }
19
+ body = app.call(Rack::MockRequest.env_for("/", :input => "body=asdf&status=12")).last
20
+ body['body'].should.equal "asdf"
21
+ body['status'].should.equal "12"
22
+ end
23
+
24
+ end
25
+
26
+ def env_for_post_with_headers(path, headers, body)
27
+ Rack::MockRequest.env_for(path, {:method => "POST", :input => body}.merge(headers))
28
+ end
29
+ rescue LoadError => e
30
+ # Missing dependency JSON, skipping tests.
31
+ STDERR.puts "WARN: Skipping Rack::PostBodyContentTypeParser tests (json not installed)"
32
+ end
@@ -0,0 +1,26 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/proctitle'
4
+
5
+ context "Rack::ProcTitle" do
6
+ F = ::File
7
+
8
+ progname = File.basename($0)
9
+ appname = F.expand_path(__FILE__).split('/')[-3]
10
+
11
+ def simple_app(body=['Hello World!'])
12
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
13
+ end
14
+
15
+ specify "should set the process title when created" do
16
+ Rack::ProcTitle.new(simple_app)
17
+ $0.should.equal "#{progname} [#{appname}] init ..."
18
+ end
19
+
20
+ specify "should set the process title on each request" do
21
+ app = Rack::ProcTitle.new(simple_app)
22
+ req = Rack::MockRequest.new(app)
23
+ 10.times { req.get('/hello') }
24
+ $0.should.equal "#{progname} [#{appname}/80] (10) GET /hello"
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/profiler'
4
+
5
+ context 'Rack::Profiler' do
6
+
7
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'Oh hai der'] }
8
+ request = Rack::MockRequest.env_for("/", :input => "profile=process_time")
9
+
10
+ specify 'printer defaults to RubyProf::CallTreePrinter' do
11
+ profiler = Rack::Profiler.new(nil)
12
+ profiler.instance_variable_get('@printer').should == RubyProf::CallTreePrinter
13
+ profiler.instance_variable_get('@times').should == 1
14
+ end
15
+
16
+ specify 'CallTreePrinter has correct headers' do
17
+ headers = Rack::Profiler.new(app).call(request)[1]
18
+ headers.should == {"Content-Disposition"=>"attachment; filename=\"/.process_time.tree\"", "Content-Type"=>"application/octet-stream"}
19
+ end
20
+
21
+ specify 'FlatPrinter and GraphPrinter has Content-Type text/plain' do
22
+ %w(flat graph).each do |printer|
23
+ headers = Rack::Profiler.new(app, :printer => printer.to_sym).call(request)[1]
24
+ headers.should == {"Content-Type"=>"text/plain"}
25
+ end
26
+ end
27
+
28
+ specify 'GraphHtmlPrinter has Content-Type text/html' do
29
+ headers = Rack::Profiler.new(app, :printer => :graph_html).call(request)[1]
30
+ headers.should == {"Content-Type"=>"text/html"}
31
+ end
32
+ end
@@ -0,0 +1,86 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/sendfile'
4
+
5
+ context "Rack::File" do
6
+ specify "should respond to #to_path" do
7
+ Rack::File.new(Dir.pwd).should.respond_to :to_path
8
+ end
9
+ end
10
+
11
+ context "Rack::Sendfile" do
12
+ def sendfile_body
13
+ res = ['Hello World']
14
+ def res.to_path ; "/tmp/hello.txt" ; end
15
+ res
16
+ end
17
+
18
+ def simple_app(body=sendfile_body)
19
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
20
+ end
21
+
22
+ def sendfile_app(body=sendfile_body)
23
+ Rack::Sendfile.new(simple_app(body))
24
+ end
25
+
26
+ setup do
27
+ @request = Rack::MockRequest.new(sendfile_app)
28
+ end
29
+
30
+ def request(headers={})
31
+ yield @request.get('/', headers)
32
+ end
33
+
34
+ specify "does nothing when no X-Sendfile-Type header present" do
35
+ request do |response|
36
+ response.should.be.ok
37
+ response.body.should.equal 'Hello World'
38
+ response.headers.should.not.include 'X-Sendfile'
39
+ end
40
+ end
41
+
42
+ specify "sets X-Sendfile response header and discards body" do
43
+ request 'HTTP_X_SENDFILE_TYPE' => 'X-Sendfile' do |response|
44
+ response.should.be.ok
45
+ response.body.should.be.empty
46
+ response.headers['X-Sendfile'].should.equal '/tmp/hello.txt'
47
+ end
48
+ end
49
+
50
+ specify "sets X-Lighttpd-Send-File response header and discards body" do
51
+ request 'HTTP_X_SENDFILE_TYPE' => 'X-Lighttpd-Send-File' do |response|
52
+ response.should.be.ok
53
+ response.body.should.be.empty
54
+ response.headers['X-Lighttpd-Send-File'].should.equal '/tmp/hello.txt'
55
+ end
56
+ end
57
+
58
+ specify "sets X-Accel-Redirect response header and discards body" do
59
+ headers = {
60
+ 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect',
61
+ 'HTTP_X_ACCEL_MAPPING' => '/tmp/=/foo/bar/'
62
+ }
63
+ request headers do |response|
64
+ response.should.be.ok
65
+ response.body.should.be.empty
66
+ response.headers['X-Accel-Redirect'].should.equal '/foo/bar/hello.txt'
67
+ end
68
+ end
69
+
70
+ specify 'writes to rack.error when no X-Accel-Mapping is specified' do
71
+ request 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect' do |response|
72
+ response.should.be.ok
73
+ response.body.should.equal 'Hello World'
74
+ response.headers.should.not.include 'X-Accel-Redirect'
75
+ response.errors.should.include 'X-Accel-Mapping'
76
+ end
77
+ end
78
+
79
+ specify 'does nothing when body does not respond to #to_path' do
80
+ @request = Rack::MockRequest.new(sendfile_app(['Not a file...']))
81
+ request 'HTTP_X_SENDFILE_TYPE' => 'X-Sendfile' do |response|
82
+ response.body.should.equal 'Not a file...'
83
+ response.headers.should.not.include 'X-Sendfile'
84
+ end
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-contrib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - rack-devel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-23 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: test-spec
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: tmail
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "1.2"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: json
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "1.1"
54
+ version:
55
+ description: Contributed Rack Middleware and Utilities
56
+ email: rack-devel@googlegroups.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - README.rdoc
63
+ - COPYING
64
+ files:
65
+ - COPYING
66
+ - README.rdoc
67
+ - Rakefile
68
+ - lib/rack/contrib.rb
69
+ - lib/rack/contrib/bounce_favicon.rb
70
+ - lib/rack/contrib/callbacks.rb
71
+ - lib/rack/contrib/etag.rb
72
+ - lib/rack/contrib/evil.rb
73
+ - lib/rack/contrib/garbagecollector.rb
74
+ - lib/rack/contrib/jsonp.rb
75
+ - lib/rack/contrib/lighttpd_script_name_fix.rb
76
+ - lib/rack/contrib/locale.rb
77
+ - lib/rack/contrib/mailexceptions.rb
78
+ - lib/rack/contrib/nested_params.rb
79
+ - lib/rack/contrib/post_body_content_type_parser.rb
80
+ - lib/rack/contrib/proctitle.rb
81
+ - lib/rack/contrib/profiler.rb
82
+ - lib/rack/contrib/route_exceptions.rb
83
+ - lib/rack/contrib/sendfile.rb
84
+ - lib/rack/contrib/time_zone.rb
85
+ - rack-contrib.gemspec
86
+ - test/mail_settings.rb
87
+ - test/spec_rack_callbacks.rb
88
+ - test/spec_rack_contrib.rb
89
+ - test/spec_rack_etag.rb
90
+ - test/spec_rack_evil.rb
91
+ - test/spec_rack_garbagecollector.rb
92
+ - test/spec_rack_jsonp.rb
93
+ - test/spec_rack_lighttpd_script_name_fix.rb
94
+ - test/spec_rack_mailexceptions.rb
95
+ - test/spec_rack_nested_params.rb
96
+ - test/spec_rack_post_body_content_type_parser.rb
97
+ - test/spec_rack_proctitle.rb
98
+ - test/spec_rack_profiler.rb
99
+ - test/spec_rack_sendfile.rb
100
+ has_rdoc: true
101
+ homepage: http://github.com/rack/rack-contrib/
102
+ post_install_message:
103
+ rdoc_options:
104
+ - --line-numbers
105
+ - --inline-source
106
+ - --title
107
+ - rack-contrib
108
+ - --main
109
+ - README
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: "0"
117
+ version:
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: "0"
123
+ version:
124
+ requirements: []
125
+
126
+ rubyforge_project:
127
+ rubygems_version: 1.3.1
128
+ signing_key:
129
+ specification_version: 2
130
+ summary: Contributed Rack Middleware and Utilities
131
+ test_files:
132
+ - test/spec_rack_callbacks.rb
133
+ - test/spec_rack_contrib.rb
134
+ - test/spec_rack_etag.rb
135
+ - test/spec_rack_evil.rb
136
+ - test/spec_rack_garbagecollector.rb
137
+ - test/spec_rack_jsonp.rb
138
+ - test/spec_rack_lighttpd_script_name_fix.rb
139
+ - test/spec_rack_mailexceptions.rb
140
+ - test/spec_rack_nested_params.rb
141
+ - test/spec_rack_post_body_content_type_parser.rb
142
+ - test/spec_rack_proctitle.rb
143
+ - test/spec_rack_profiler.rb
144
+ - test/spec_rack_sendfile.rb