Class: Jets::SpecHelpers::Controllers::Request
- Inherits:
-
Object
- Object
- Jets::SpecHelpers::Controllers::Request
- Extended by:
- Memoist
- Defined in:
- lib/jets/spec_helpers/controllers/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
- #extract_parameters ⇒ Object
-
#initialize(method, path, headers = {}, params = {}) ⇒ Request
constructor
A new instance of Request.
- #normalized_path ⇒ Object
- #path_params ⇒ Object
- #request ⇒ Object
- #route ⇒ Object
- #strip_query_string(path) ⇒ Object
Constructor Details
#initialize(method, path, headers = {}, params = {}) ⇒ Request
Returns a new instance of Request.
6 7 8 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 6 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.
5 6 7 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
5 6 7 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5 def path @path end |
Instance Method Details
#dispatch! ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 93 def dispatch! klass = Object.const_get(route.controller_name) context = Jets::Controller::Middleware::Mimic::LambdaContext.new rack_env = Jets::Controller::RackAdapter::Env.new(event, context).convert controller = klass.new(event, context, route.action_name, rack_env) response = controller.process! # response is API Gateway Hash unless response['statusCode'] && response['body'] raise "Expected response to an API Gateway Hash Structure. Are you rendering correctly?" end Response.new(response) # converts APIGW hash to prettier object end |
#event ⇒ Object
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 53 54 55 56 57 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 10 def event json = {} id_params = route.path.scan(%r{:([^/]+)}).flatten = route.escape_path(path) path_parameters = {} id_params.each do |id_param| raise "missing param: :#{id_param}" unless path_params.include? id_param.to_sym path_param_value = path_params[id_param.to_sym] raise "Path param :#{id_param} value cannot be blank" if path_param_value.blank? escaped_path_param_value = CGI.escape(path_param_value.to_s) = .gsub(":#{id_param}", escaped_path_param_value) path_parameters.deep_merge!(id_param => escaped_path_param_value) end json['resource'] = strip_query_string(route.path.gsub(/:(\w+)/, '{\1}')) json['path'] = strip_query_string() 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' if params.body_params.is_a? String body = params.body_params json['headers']['Content-Length'] ||= body.length.to_s else 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 end json['body'] = Base64.encode64(body) json['isBase64Encoded'] = true end json['queryStringParameters'] = params.query_params if params.query_params.present? json end |
#extract_parameters ⇒ Object
64 65 66 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 64 def extract_parameters route.extract_parameters(normalized_path).symbolize_keys end |
#normalized_path ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 68 def normalized_path path = self.path path = path[0..-2] if path.end_with? '/' path = path[1..-1] if path.start_with? '/' path = strip_query_string(path) path end |
#path_params ⇒ Object
59 60 61 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 59 def path_params params.path_params.reverse_merge(extract_parameters) end |
#request ⇒ Object
88 89 90 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 88 def request Jets::Controller::Request.new(event: event) end |
#route ⇒ Object
80 81 82 83 84 85 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 80 def route Jets::Router::Matcher.new(Jets.application.routes).find_by_env( "REQUEST_METHOD" => method.to_s.upcase, "PATH_INFO" => path, ) end |
#strip_query_string(path) ⇒ Object
76 77 78 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 76 def strip_query_string(path) path.sub(/\?.*/, '') end |