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
-
#assert_api_response(method, expected_status, params: {}, headers: {}, body: nil, path_params: {}, &block) ⇒ Object
Minitest-style assertion: looks up the api_path context, makes the request, validates the response status + body, then yields to the block for additional expectations.
- #assert_openapi_response(metadata) ⇒ Object
- #parsed_body ⇒ Object
-
#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).
Instance Method Details
#assert_api_response(method, expected_status, params: {}, headers: {}, body: nil, path_params: {}, &block) ⇒ Object
Minitest-style assertion: looks up the api_path context, makes the request, validates the response status + body, then yields to the block for additional expectations.
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 169 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/openapi_ruby/adapters/rspec.rb', line 126 def assert_api_response(method, expected_status, params: {}, headers: {}, body: nil, path_params: {}, &block) = ::RSpec.current_example. context = find_api_context_for(, method, path_params) raise OpenapiRuby::Error, "No api_path defined for #{method.upcase} in this example group" unless context operation = context.operations[method.to_s] raise OpenapiRuby::Error, "No #{method.upcase} operation defined" unless operation response_ctx = operation.responses[expected_status.to_s] raise OpenapiRuby::Error, "No response #{expected_status} defined for #{method.upcase}" unless response_ctx base_path = resolve_base_path(context.schema_name) path = "#{base_path}#{(context.path_template, params.merge(path_params))}" # Resolve security scheme parameters resolve_security_params(operation, ).each do |param| val = params[param[:name].to_sym] || params[param[:name]] next if val.nil? 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 headers["Accept"] ||= "application/json" path_param_names = context.path_parameters.map { |p| p["name"] } query_params = params.reject { |k, _| path_param_names.include?(k.to_s) } 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 if query_params.any? path = "#{path}?#{Rack::Utils.build_nested_query(query_params)}" end # Validate the request against the declared operation (skip for error responses, # since those tests intentionally send invalid data) if OpenapiRuby.configuration.test_request_validation && expected_status < 400 document_hash = OpenapiRuby::Adapters::RSpec.validation_document_for(context.schema_name) req_errors = Testing::RequestValidator.new(document_hash).validate( operation: operation, path_context: context, params: params, headers: headers, body: body, path_params: path_params ) raise "Request validation failed:\n#{req_errors.join("\n")}" unless req_errors.empty? end send(method, path, **request_args) unless response.status == expected_status raise "Expected status #{expected_status}, got #{response.status}\nResponse body: #{response.body}" end if response_ctx.schema_definition validator = Testing::ResponseValidator.new( OpenapiRuby::Adapters::RSpec.validation_document_for(context.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 instance_eval(&block) if block end |
#assert_openapi_response(metadata) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/openapi_ruby/adapters/rspec.rb', line 309 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 |
#parsed_body ⇒ Object
213 214 215 |
# File 'lib/openapi_ruby/adapters/rspec.rb', line 213 def parsed_body parsed_response_body 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)
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/openapi_ruby/adapters/rspec.rb', line 219 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 # Validate the request against the declared operation (skip for error responses, # since those tests intentionally send invalid data) response_ctx = (, :openapi_response) expected_status = response_ctx&.status_code.to_i if OpenapiRuby.configuration.test_request_validation && operation && expected_status < 400 path_ctx = (, :openapi_path_context) schema_name = (, :openapi_schema_name) # Collect resolved path param values for validation path_param_values = {} (path_ctx&.path_parameters || []).each do |param| name = param["name"] next unless name val = resolve_let(name.to_sym) path_param_values[name] = val if val end document_hash = OpenapiRuby::Adapters::RSpec.validation_document_for(schema_name) req_errors = Testing::RequestValidator.new(document_hash).validate( operation: operation, path_context: path_ctx, params: params, headers: headers, body: body, path_params: path_param_values ) raise "Request validation failed:\n#{req_errors.join("\n")}" unless req_errors.empty? end send(method.to_sym, path, **request_args) end |