Module: Explicit::TestHelper

Defined in:
lib/explicit/test_helper.rb

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Instance Method Details

#ensure_response_matches_spec!(request, response) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/explicit/test_helper.rb', line 36

def ensure_response_matches_spec!(request, response)
  allowed_responses = request.send(:responses)
  response_validator = Explicit::Spec.build([:one_of, *allowed_responses])

  case response_validator.call({ status: response.status, data: response.data.with_indifferent_access })
  in [:ok, _] then :all_good
  in [:error, err] then raise Explicit::Request::InvalidResponseError.new(response, err)
  end
end

#fetch(request, params:, headers: nil) ⇒ Object



8
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
# File 'lib/explicit/test_helper.rb', line 8

def fetch(request, params:, headers: nil)
  route = request.send(:routes).first

  if route.nil?
    raise <<-DESC
    The request #{request} must define at least one route. For example:

      get "/customers/:customer_id"
      post "/article/:article_id/comments"
      put "/user/preferences"

    Important: adding a route to the request does not substitute the entry
    on `routes.rb`.
    DESC
  end

  process(route.method, route.path, params:, headers:)

  response = Response.new(
    status: @response.status,
    data: @response.parsed_body.deep_symbolize_keys
  )

  ensure_response_matches_spec!(request, response)

  response
end