Class: Jets::SpecHelpers::Request
- Inherits:
-
Object
- Object
- Jets::SpecHelpers::Request
- Defined in:
- lib/jets/spec_helpers/request.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #dispatch! ⇒ Object
- #event ⇒ Object
- #find_route! ⇒ Object
-
#initialize(method, path, headers = {}, params = {}) ⇒ Request
constructor
A new instance of Request.
Constructor Details
#initialize(method, path, headers = {}, params = {}) ⇒ Request
Returns a new instance of Request.
5 6 7 |
# File 'lib/jets/spec_helpers/request.rb', line 5 def initialize(method, path, headers={}, params={}) @method, @path, @headers, @params = method, path, headers, params end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
4 5 6 |
# File 'lib/jets/spec_helpers/request.rb', line 4 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
4 5 6 |
# File 'lib/jets/spec_helpers/request.rb', line 4 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
4 5 6 |
# File 'lib/jets/spec_helpers/request.rb', line 4 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/jets/spec_helpers/request.rb', line 4 def path @path end |
Instance Method Details
#dispatch! ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jets/spec_helpers/request.rb', line 65 def dispatch! route = find_route! controller = Object.const_get(route.controller_name).new(event, {}, route.action_name) response = controller.dispatch! if !response.is_a?(Array) || response.size != 3 raise "Expected response to be an array of size 3. Are you rendering correctly?" end Response.new(response[0].to_i, response[2].read) end |
#event ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jets/spec_helpers/request.rb', line 9 def event json = {} id_params = path.scan(%r{:([^/]+)}).flatten = path.dup path_parameters = {} id_params.each do |id_param| raise "missing param: :#{id_param}" unless params.path_params.include? id_param.to_sym path_param_value = params.path_params[id_param.to_sym] raise "Path param :#{id_param} value cannot be blank" if path_param_value.blank? .gsub!(":#{id_param}", path_param_value.to_s) path_parameters.deep_merge!(id_param => path_param_value.to_s) end json['resource'] = path json['path'] = json['httpMethod'] = method.to_s.upcase json['pathParameters'] = path_parameters json['headers'] = (headers || {}).stringify_keys if method != :get json['headers']['Content-Type'] = 'application/x-www-form-urlencoded' body = Rack::Multipart.build_multipart(params.body_params) if body json['headers']['Content-Length'] ||= body.length.to_s json['headers']['Content-Type'] = "multipart/form-data; boundary=#{Rack::Multipart::MULTIPART_BOUNDARY}" else body = Rack::Utils.build_nested_query(params.body_params) end json['body'] = Base64.encode64(body) json['isBase64Encoded'] = true end params.query_params.each do |key, value| json['queryStringParameters'] ||= {} json['queryStringParameters'][key.to_s] = value.to_s end json end |
#find_route! ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jets/spec_helpers/request.rb', line 54 def find_route! path = self.path path = path[0..-2] if path.end_with? '/' path = path[1..-1] if path.start_with? '/' route = Jets::Router.routes.find { |r| r.path == path && r.method == method.to_s.upcase } raise "Route not found: #{method.to_s.upcase} #{path}" if route.blank? route end |