Module: HaveAPI::Spec::SpecMethods
- Includes:
- Rack::Test::Methods
- Defined in:
- lib/haveapi/spec/spec_methods.rb
Overview
Helper methods for specs.
Instance Method Summary collapse
-
#api_response ⇒ HaveAPI::Spec::ApiResponse
Return parsed API response.
- #app ⇒ Object
-
#call_api(*args) ⇒ Object
Make API request.
-
#login(*credentials) ⇒ Object
Login with HTTP basic auth.
-
#mock_action(r_name, a_name, params, version: nil, user: nil) {|self| ... } ⇒ Object
Mock action call.
Instance Method Details
#api_response ⇒ HaveAPI::Spec::ApiResponse
Return parsed API response.
97 98 99 100 101 102 103 104 105 |
# File 'lib/haveapi/spec/spec_methods.rb', line 97 def api_response if last_response == @last_response @api_response ||= ApiResponse.new(last_response.body) else @last_response = last_response @api_response = ApiResponse.new(last_response.body) end end |
#app ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/haveapi/spec/spec_methods.rb', line 6 def app return @api.app if @api auth = get_opt(:auth_chain) default = get_opt(:default_version) @api = HaveAPI::Server.new(get_opt(:api_module)) @api.auth_chain << auth if auth @api.use_version(get_opt(:versions) || :all) @api.default_version = default if default as = get_opt(:action_state) @api.action_state = as if as @api.mount(get_opt(:mount) || '/') @api.app end |
#call_api(*args) ⇒ Object
Make API request. This method is a wrapper for Rack::Test::Methods. Input parameters are encoded into JSON and sent with a correct Content-Type. Two modes:
http_method, path, params = {}
[resource], action, params, &block
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 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/haveapi/spec/spec_methods.rb', line 33 def call_api(*args, &) if args[0].is_a?(::Array) || args[1].is_a?(::Symbol) r_name, a_name, params = args app action, path = find_action( (params && params[:version]) || @api.default_version, r_name, a_name ) json_body = params && params.to_json env = { 'CONTENT_TYPE' => 'application/json' } if %i[get head options].include?(action.http_method.to_sym) method(action.http_method).call(path, params, env) else env[:input] = json_body if json_body method(action.http_method).call(path, nil, env) end else http_method, path, params = args json_body = params && params.to_json env = { 'CONTENT_TYPE' => 'application/json' } if %i[get head options].include?(http_method.to_sym) method(http_method).call(path, params, env) else env[:input] = json_body if json_body method(http_method).call(path, nil, env) end end end |
#login(*credentials) ⇒ Object
Login with HTTP basic auth.
23 24 25 |
# File 'lib/haveapi/spec/spec_methods.rb', line 23 def login(*credentials) (*credentials) end |
#mock_action(r_name, a_name, params, version: nil, user: nil) {|self| ... } ⇒ Object
Mock action call. Note that this method does not involve rack request/response in any way. It simply creates an instance of specified action and executes it. Provided block is executed in the context of the action instance after ‘exec()` has been called.
If ‘exec()` signals error, the block is not called at all, but RuntimeError is raised instead.
Authentication does not take place. Argument user may be used to provide user object. That will signify that the user is authenticated and it will be passed to Action.authorize.
87 88 89 90 91 92 93 |
# File 'lib/haveapi/spec/spec_methods.rb', line 87 def mock_action(r_name, a_name, params, version: nil, user: nil, &) app v = version || @api.default_version action, path = find_action(v, r_name, a_name) m = MockAction.new(self, @api, action, path, v) m.call(params, user:, &) end |