Module: Crspec::Rails::RequestHelpers

Includes:
ActiveSupport::Testing::Assertions, ActiveSupport::Testing::FileFixtures, ActiveSupport::Testing::TimeHelpers
Defined in:
lib/crspec/rails/request_helpers.rb

Defined Under Namespace

Classes: ResponseStruct

Instance Method Summary collapse

Instance Method Details

#delete(path, params: {}, headers: {}) ⇒ Object



91
92
93
# File 'lib/crspec/rails/request_helpers.rb', line 91

def delete(path, params: {}, headers: {})
  process_request(:delete, path, params, headers)
end

#get(path, params: {}, headers: {}) ⇒ Object



75
76
77
# File 'lib/crspec/rails/request_helpers.rb', line 75

def get(path, params: {}, headers: {})
  process_request(:get, path, params, headers)
end

#json_responseObject



27
28
29
30
31
32
# File 'lib/crspec/rails/request_helpers.rb', line 27

def json_response
  return nil unless response&.body
  JSON.parse(response.body, symbolize_names: true)
rescue JSON::ParserError
  nil
end

#patch(path, params: {}, headers: {}) ⇒ Object



87
88
89
# File 'lib/crspec/rails/request_helpers.rb', line 87

def patch(path, params: {}, headers: {})
  process_request(:patch, path, params, headers)
end

#post(path, params: {}, headers: {}) ⇒ Object



79
80
81
# File 'lib/crspec/rails/request_helpers.rb', line 79

def post(path, params: {}, headers: {})
  process_request(:post, path, params, headers)
end

#process_request(method, path, params = {}, headers = {}) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/crspec/rails/request_helpers.rb', line 34

def process_request(method, path, params = {}, headers = {})
  body_string = params.is_a?(String) ? params : params.to_json
  env = {
    "REQUEST_METHOD" => method.to_s.upcase,
    "PATH_INFO" => path,
    "rack.input" => StringIO.new(body_string),
    "CONTENT_TYPE" => headers["CONTENT_TYPE"] || "application/json",
    "HTTP_ACCEPT" => headers["HTTP_ACCEPT"] || "application/json",
    "CONTENT_LENGTH" => body_string.bytesize.to_s
  }

  headers.each do |k, v|
    env["HTTP_#{k.to_s.upcase.tr('-', '_')}"] = v unless k.to_s.start_with?("HTTP_")
  end

  status = 200
  response_headers = { "Content-Type" => "application/json" }
  response_body = ""

  begin
    if defined?(::Rails) && ::Rails.application && ::Rails.application.routes.routes.any?
      begin
        status, response_headers, body_obj = ::Rails.application.call(env)
        response_body = body_obj.respond_to?(:body) ? body_obj.body : body_obj.join
      rescue StandardError
        response_body = params.to_json
      end
    else
      response_body = params.to_json
    end
  ensure
    if defined?(ActiveRecord::Base) && ActiveRecord::Base.respond_to?(:connection_handler)
      ActiveRecord::Base.connection_handler.clear_active_connections!
    end
  end

  res = ResponseStruct.new(status, response_body, response_headers)
  execution_context[:last_response] = res
  res
end

#put(path, params: {}, headers: {}) ⇒ Object



83
84
85
# File 'lib/crspec/rails/request_helpers.rb', line 83

def put(path, params: {}, headers: {})
  process_request(:put, path, params, headers)
end

#responseObject



23
24
25
# File 'lib/crspec/rails/request_helpers.rb', line 23

def response
  execution_context[:last_response]
end