Class: Rack::TestApp::Wrapper
- Inherits:
-
Object
- Object
- Rack::TestApp::Wrapper
- Defined in:
- lib/rack/test_app.rb
Overview
Wrapper class to test Rack application. Use ‘Rack::TestApp.wrap(app)’ instead of ‘Rack::TestApp::Wrapper.new(app)’.
ex:
require 'rack/lint'
require 'rack/test_app'
http = Rack::TestApp.wrap(Rack::Lint.new(app))
https = Rack::TestApp.wrap(Rack::Lint.new(app)), env: {'HTTPS'=>'on'})
resp = http.GET('/api/hello', query: {'name'=>'World'})
assert_equal 200, resp.status
assert_equal "application/json", resp.headers['Content-Type']
assert_equal {"message"=>"Hello World!"}, resp.body_json
Instance Attribute Summary collapse
-
#last_env ⇒ Object
readonly
Returns the value of attribute last_env.
Instance Method Summary collapse
- #DELETE(path, kwargs = {}) ⇒ Object (also: #delete)
- #GET(path, kwargs = {}) ⇒ Object (also: #get)
- #HEAD(path, kwargs = {}) ⇒ Object (also: #head)
-
#initialize(app, env = nil) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #OPTIONS(path, kwargs = {}) ⇒ Object (also: #options)
- #PATCH(path, kwargs = {}) ⇒ Object (also: #patch)
- #POST(path, kwargs = {}) ⇒ Object (also: #post)
- #PUT(path, kwargs = {}) ⇒ Object (also: #put)
- #request(meth, path, query: nil, form: nil, multipart: nil, json: nil, input: nil, headers: nil, cookies: nil, env: nil) ⇒ Object
- #TRACE(path, kwargs = {}) ⇒ Object (also: #trace)
-
#with(headers: nil, cookies: nil, env: nil) {|new_wrapper| ... } ⇒ Object
helper method to create new wrapper object keeping cookies and headers.
Constructor Details
#initialize(app, env = nil) ⇒ Wrapper
Returns a new instance of Wrapper.
408 409 410 411 412 413 |
# File 'lib/rack/test_app.rb', line 408 def initialize(app, env=nil) #; [!zz9yg] takes app and optional env objects. @app = app @env = env @last_env = nil end |
Instance Attribute Details
#last_env ⇒ Object (readonly)
Returns the value of attribute last_env.
415 416 417 |
# File 'lib/rack/test_app.rb', line 415 def last_env @last_env end |
Instance Method Details
#DELETE(path, kwargs = {}) ⇒ Object Also known as: delete
458 |
# File 'lib/rack/test_app.rb', line 458 def DELETE path, kwargs={}; request(:DELETE , path, **kwargs); end |
#GET(path, kwargs = {}) ⇒ Object Also known as: get
455 |
# File 'lib/rack/test_app.rb', line 455 def GET path, kwargs={}; request(:GET , path, **kwargs); end |
#HEAD(path, kwargs = {}) ⇒ Object Also known as: head
459 |
# File 'lib/rack/test_app.rb', line 459 def HEAD path, kwargs={}; request(:HEAD , path, **kwargs); end |
#OPTIONS(path, kwargs = {}) ⇒ Object Also known as: options
461 |
# File 'lib/rack/test_app.rb', line 461 def OPTIONS path, kwargs={}; request(:OPTIONS, path, **kwargs); end |
#PATCH(path, kwargs = {}) ⇒ Object Also known as: patch
460 |
# File 'lib/rack/test_app.rb', line 460 def PATCH path, kwargs={}; request(:PATCH , path, **kwargs); end |
#POST(path, kwargs = {}) ⇒ Object Also known as: post
456 |
# File 'lib/rack/test_app.rb', line 456 def POST path, kwargs={}; request(:POST , path, **kwargs); end |
#PUT(path, kwargs = {}) ⇒ Object Also known as: put
457 |
# File 'lib/rack/test_app.rb', line 457 def PUT path, kwargs={}; request(:PUT , path, **kwargs); end |
#request(meth, path, query: nil, form: nil, multipart: nil, json: nil, input: nil, headers: nil, cookies: nil, env: nil) ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/rack/test_app.rb', line 441 def request(meth, path, query: nil, form: nil, multipart: nil, json: nil, input: nil, headers: nil, cookies: nil, env: nil) #; [!r6sod] merges @env if passed for initializer. env = env ? env.merge(@env) : @env if @env #; [!4xpwa] creates env object and calls app with it. environ = TestApp.new_env(meth, path, query: query, form: form, multipart: multipart, json: json, input: input, headers: headers, cookies: , env: env) @last_env = environ tuple = @app.call(environ) status, headers, body = tuple #; [!eb153] returns Rack::TestApp::Result object. return Result.new(status, headers, body) end |
#TRACE(path, kwargs = {}) ⇒ Object Also known as: trace
462 |
# File 'lib/rack/test_app.rb', line 462 def TRACE path, kwargs={}; request(:TRACE , path, **kwargs); end |
#with(headers: nil, cookies: nil, env: nil) {|new_wrapper| ... } ⇒ Object
helper method to create new wrapper object keeping cookies and headers.
ex:
http = Rack::TestApp.wrap(Rack::Lint.new(app))
r1 = http.POST('/api/login', form: {user: 'user', password: 'pass'})
http.with(cookies: r1., headers: {}) do |http_|
r2 = http_.GET('/api/content') # request with r1.cookies
assert_equal 200, r2.status
end
428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/rack/test_app.rb', line 428 def with(headers: nil, cookies: nil, env: nil) tmp_env = TestApp.new_env(headers: headers, cookies: , env: env) new_env = @env ? @env.dup : {} tmp_env.each do |k, v| new_env[k] = v if k.start_with?('HTTP_') end new_wrapper = self.class.new(@app, new_env) #; [!mkdbu] yields with new wrapper object if block given. yield new_wrapper if block_given? #; [!0bk12] returns new wrapper object, keeping cookies and headers. new_wrapper end |