Class: Jets::SpecHelpers::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/spec_helpers/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#pathObject

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



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

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

#eventObject



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
# File 'lib/jets/spec_helpers/request.rb', line 9

def event
  json = {}
  id_params = path.scan(%r{:([^/]+)}).flatten
  expanded_path = 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?

    expanded_path.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'] = expanded_path
  json['httpMethod'] = method.to_s.upcase
  json['pathParameters'] = path_parameters
  json['headers'] = (headers || {}).stringify_keys

  if method != :get
    json['headers']['Content-Type'] = "multipart/form-data; boundary=#{multipart_boundary}"
    body = +''
    params.body_params.to_a.each do |e|
      key, value = e
      body << multipart_item(name: key, value: value)
    end
    body << multipart_end

    json['body'] = Base64.encode64 body
    json['isBase64Encoded'] = true
  end

  json
end

#find_route!Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/jets/spec_helpers/request.rb', line 74

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

#multipart_boundaryObject



47
48
49
# File 'lib/jets/spec_helpers/request.rb', line 47

def multipart_boundary
  @boundary ||= '-' * 16 + SecureRandom.hex(32)
end

#multipart_endObject



70
71
72
# File 'lib/jets/spec_helpers/request.rb', line 70

def multipart_end
  "--#{multipart_boundary}--"
end

#multipart_file(name:, filename:, data:) ⇒ Object



65
66
67
68
# File 'lib/jets/spec_helpers/request.rb', line 65

def multipart_file(name:, filename:, data:)
  "--#{multipart_boundary}\r\nContent-Disposition: form-data; name=\"#{name}\"; "\
  "filename=\"#{filename}\"\r\n\r\n#{data}\r\n"
end

#multipart_item(name:, value:) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/jets/spec_helpers/request.rb', line 51

def multipart_item(name:, value:)
  if value.is_a? File
    multipart_file(name: name, filename: File.basename(value.path),
                   data: ::IO.read(value.path))
  else
    multipart_text(name: name, text: value)
  end
end

#multipart_text(name:, text:) ⇒ Object



60
61
62
63
# File 'lib/jets/spec_helpers/request.rb', line 60

def multipart_text(name:, text:)
  "--#{multipart_boundary}\r\nContent-Disposition: form-data; name=\"#{name}\"\r\n"\
  "Content-Type: text/plain\r\n\r\n#{text}\r\n"
end