Class: Jets::SpecHelpers::Controllers::Request

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/spec_helpers/controllers/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



5
6
7
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5

def method
  @method
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5

def params
  @params
end

#pathObject

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



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jets/spec_helpers/controllers/request.rb', line 83

def dispatch!
  klass = Object.const_get(route.controller_name)
  controller = klass.new(event, {}, route.action_name)
  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

#eventObject



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
58
59
60
# File 'lib/jets/spec_helpers/controllers/request.rb', line 10

def event
  json = {}
  id_params = route.path.scan(%r{:([^/]+)}).flatten
  expanded_path = 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)
    expanded_path.gsub!(":#{id_param}", escaped_path_param_value)
    path_parameters.deep_merge!(id_param => escaped_path_param_value)
  end

  json['resource'] = escape_path(path)
  json['path'] = expanded_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'

    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

  params.query_params.each do |key, value|
    json['queryStringParameters'] ||= {}
    json['queryStringParameters'][key.to_s] = value.deep_dup
  end

  json
end

#extract_parametersObject



67
68
69
# File 'lib/jets/spec_helpers/controllers/request.rb', line 67

def extract_parameters
  route.extract_parameters(normalized_path).symbolize_keys
end

#normalized_pathObject



71
72
73
74
75
76
# File 'lib/jets/spec_helpers/controllers/request.rb', line 71

def normalized_path
  path = self.path
  path = path[0..-2] if path.end_with? '/'
  path = path[1..-1] if path.start_with? '/'
  path
end

#path_paramsObject



62
63
64
# File 'lib/jets/spec_helpers/controllers/request.rb', line 62

def path_params
  params.path_params.reverse_merge(extract_parameters)
end

#routeObject



78
79
80
# File 'lib/jets/spec_helpers/controllers/request.rb', line 78

def route
  Jets::Router::Finder.new(normalized_path, method).run
end