class Rack::TestApp::Wrapper
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
Attributes
last_env[R]
Public Class Methods
new(app, env=nil)
click to toggle source
# File lib/rack/test_app.rb, line 407 def initialize(app, env=nil) #; [!zz9yg] takes app and optional env objects. @app = app @env = env @last_env = nil end
Public Instance Methods
DELETE(path, kwargs={})
click to toggle source
# File lib/rack/test_app.rb, line 457 def DELETE path, kwargs={}; request(:DELETE , path, **kwargs); end
Also aliased as: delete
GET(path, kwargs={})
click to toggle source
# File lib/rack/test_app.rb, line 454 def GET path, kwargs={}; request(:GET , path, **kwargs); end
Also aliased as: get
HEAD(path, kwargs={})
click to toggle source
# File lib/rack/test_app.rb, line 458 def HEAD path, kwargs={}; request(:HEAD , path, **kwargs); end
Also aliased as: head
OPTIONS(path, kwargs={})
click to toggle source
# File lib/rack/test_app.rb, line 460 def OPTIONS path, kwargs={}; request(:OPTIONS, path, **kwargs); end
Also aliased as: options
PATCH(path, kwargs={})
click to toggle source
# File lib/rack/test_app.rb, line 459 def PATCH path, kwargs={}; request(:PATCH , path, **kwargs); end
Also aliased as: patch
POST(path, kwargs={})
click to toggle source
# File lib/rack/test_app.rb, line 455 def POST path, kwargs={}; request(:POST , path, **kwargs); end
Also aliased as: post
PUT(path, kwargs={})
click to toggle source
# File lib/rack/test_app.rb, line 456 def PUT path, kwargs={}; request(:PUT , path, **kwargs); end
Also aliased as: put
TRACE(path, kwargs={})
click to toggle source
# File lib/rack/test_app.rb, line 461 def TRACE path, kwargs={}; request(:TRACE , path, **kwargs); end
Also aliased as: trace
get(path, kwargs={})
define aliases because ruby programmer prefers get()
rather than #GET().
Alias for: GET
request(meth, path, query: nil, form: nil, multipart: nil, json: nil, input: nil, headers: nil, cookies: nil, env: nil)
click to toggle source
# File lib/rack/test_app.rb, line 440 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: 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
with(headers: nil, cookies: nil, env: nil) { |new_wrapper| ... }
click to toggle source
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.cookies, headers: {}) do |http_| r2 = http_.GET('/api/content') # request with r1.cookies assert_equal 200, r2.status end
# File lib/rack/test_app.rb, line 427 def with(headers: nil, cookies: nil, env: nil) tmp_env = TestApp.new_env(headers: headers, cookies: 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