class SharkOnLambda::RSpec::EnvBuilder
Attributes
env[R]
Public Class Methods
new(**options)
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 6 def initialize(**options) @options = options end
Public Instance Methods
build()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 10 def build initialize_env add_headers add_request_body_as_json if body? && jsonable_params? && json_request? env.deep_stringify_keys end
Private Instance Methods
action()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 21 def action @options.fetch(:action) end
add_headers()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 25 def add_headers headers.each_pair do |name, value| name = name.upcase.tr('-', '_') key = case name when 'CONTENT_LENGTH', 'CONTENT_TYPE' then name else "HTTP_#{name}" end env[key] = value.to_s end end
add_request_body_as_json()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 36 def add_request_body_as_json body = params.to_json env['rack.input'] = StringIO.new(body).set_encoding(Encoding::BINARY) env['CONTENT_TYPE'] = headers['content-type'] env['CONTENT_LENGTH'] = env['rack.input'].length.to_s end
as()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 44 def as @options.fetch(:as, :json) end
body?()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 48 def body? !%w[GET HEAD OPTIONS].include?(env['REQUEST_METHOD']) end
controller()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 60 def controller @options.fetch(:controller, nil) end
headers()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 64 def headers return @headers if defined?(@headers) @headers = @options.fetch(:headers, {}).deep_stringify_keys @headers.transform_keys!(&:downcase) @headers end
initialize_env()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 52 def initialize_env @env = Rack::MockRequest.env_for( request_uri.to_s, method: method, params: params ) end
json_request?()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 72 def json_request? as == :json end
jsonable_params?()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 76 def jsonable_params? params.is_a?(Hash) end
method()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 80 def method @options.fetch(:method).to_s.upcase end
params()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 84 def params @options.fetch(:params, {}).deep_stringify_keys end
path_from_routes()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 88 def path_from_routes path_params = { controller: controller.name.underscore.sub(/_controller$/, ''), action: action, only_path: true } url = SharkOnLambda.application.routes.url_for(path_params, nil) URI.parse(url).path end
request_uri()
click to toggle source
# File lib/shark_on_lambda/rspec/env_builder.rb, line 98 def request_uri return @request_uri if defined?(@request_uri) path = action.is_a?(String) ? action : path_from_routes @request_uri = URI.join('https://localhost:9292', path) end