Module: OpenapiRuby::Adapters::RSpec::ExampleHelpers

Defined in:
lib/openapi_ruby/adapters/rspec.rb

Overview

Instance-level helper methods mixed into RSpec examples

Instance Method Summary collapse

Instance Method Details

#assert_openapi_response(metadata) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/openapi_ruby/adapters/rspec.rb', line 170

def assert_openapi_response()
  response_ctx = (, :openapi_response)

  expected_status = response_ctx.status_code.to_i
  actual_status = response.status

  unless actual_status == expected_status
    raise "Response validation failed:\n" \
      "Expected status #{expected_status}, got #{actual_status}\n" \
      "Response body: #{response.body}"
  end

  if response_ctx.schema_definition
    schema_name = (, :openapi_schema_name)
    validator = Testing::ResponseValidator.new(OpenapiRuby::Adapters::RSpec.validation_document_for(schema_name))
    errors = validator.validate(
      response_body: parsed_response_body,
      status_code: response.status,
      response_context: response_ctx
    )
    unless errors.empty?
      raise "Response body validation failed:\n#{errors.join("\n")}\nResponse body: #{response.body}"
    end
  end
end

#submit_openapi_request(metadata) ⇒ Object

submit_openapi_request is public so specs can call it directly (e.g., for rate limiting tests that need multiple requests)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/openapi_ruby/adapters/rspec.rb', line 109

def submit_openapi_request()
  path = resolve_path()
  operation = (, :openapi_operation)

  params = resolve_let(:request_params) || {}
  headers = resolve_let(:request_headers) || {}
  body = resolve_let(:request_body)

  # Merge individual parameter let values
  operation&.parameters&.each do |param|
    name = param["name"]
    next unless name
    val = resolve_let(name.to_sym)
    next if val.nil?

    case param["in"]
    when "query" then params[name] = val
    when "header" then headers[name] = val
    end
  end

  # Resolve security scheme parameters from let variables
  resolve_security_params(operation, ).each do |param|
    val = resolve_let(param[:name].to_sym)
    next unless val

    case param[:in].to_s
    when "header" then headers[param[:name]] = val
    when "query" then params[param[:name]] = val
    when "cookie" then headers["Cookie"] = "#{param[:name]}=#{val}"
    end
  end

  method = operation&.verb || "get"
  # Accept header: use let(:Accept) if defined, otherwise default to JSON
  accept = resolve_let(:Accept)
  headers["Accept"] = accept || "application/json"

  # Always append query params to the URL so the middleware sees them
  # (Rails sends params as request body for non-GET methods).
  if params.any?
    path = "#{path}?#{Rack::Utils.build_nested_query(params)}"
  end

  if body
    content_type = operation&.request_body_definition&.dig("content")&.keys&.first || "application/json"
    request_args = if content_type.include?("form-data") || content_type.include?("x-www-form-urlencoded")
      {params: body, headers: headers}
    else
      {
        params: body.is_a?(String) ? body : body.to_json,
        headers: headers.merge("Content-Type" => content_type)
      }
    end
  else
    request_args = {headers: headers}
  end

  send(method.to_sym, path, **request_args)
end