Module: Minitest::OpenAPI::Recorder
- Defined in:
- lib/minitest/openapi/recorder.rb
Overview
Shared engine behind both DSLs: performs the HTTP request through the integration test, validates the response body against the declared schema, and records the operation into the document.
Class Method Summary collapse
- .normalize_response(response) ⇒ Object
- .parse_body(actual) ⇒ Object
-
.run(test:, verb:, request_path:, response:, doc_path: nil, summary: nil, operation_id: nil, description: nil, tags: nil, parameters: nil, params: nil, headers: nil, body: nil, request_body: nil, assert_status: false) ⇒ Object
test - the ActionDispatch::IntegrationTest instance verb - :get/:post/:patch/:put/:delete request_path - the URL to request doc_path - templated path recorded in the document (default: request_path) response - an Integer status, or { status:, schema:, description: } assert_status - when true, fails the test if the status differs.
Class Method Details
.normalize_response(response) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/minitest/openapi/recorder.rb', line 56 def normalize_response(response) case response when Integer {status: response, schema: nil, description: nil} when Hash {status: Integer(response.fetch(:status)), schema: response[:schema], description: response[:description]} else raise ArgumentError, "response: must be a status Integer or a Hash with :status" end end |
.parse_body(actual) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/minitest/openapi/recorder.rb', line 68 def parse_body(actual) JSON.parse(actual.body) rescue JSON::ParserError raise Validator::ResponseMismatch, "response body was not valid JSON (HTTP #{actual.status})" end |
.run(test:, verb:, request_path:, response:, doc_path: nil, summary: nil, operation_id: nil, description: nil, tags: nil, parameters: nil, params: nil, headers: nil, body: nil, request_body: nil, assert_status: false) ⇒ Object
test - the ActionDispatch::IntegrationTest instance verb - :get/:post/:patch/:put/:delete request_path - the URL to request doc_path - templated path recorded in the document (default: request_path) response - an Integer status, or { status:, schema:, description: } assert_status - when true, fails the test if the status differs
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/minitest/openapi/recorder.rb', line 17 def run(test:, verb:, request_path:, response:, doc_path: nil, summary: nil, operation_id: nil, description: nil, tags: nil, parameters: nil, params: nil, headers: nil, body: nil, request_body: nil, assert_status: false) spec = normalize_response(response) doc_path ||= request_path = {} payload = body.nil? ? params : body [:params] = payload unless payload.nil? [:headers] = headers if headers [:as] = :json unless body.nil? test.public_send(verb, request_path, **) actual = test.response Minitest::OpenAPI.document.record( verb: verb, path: doc_path, status: spec[:status], schema: spec[:schema], summary: summary, operation_id: operation_id, description: description, tags: , parameters: parameters, request_body: request_body, response_description: spec[:description] ) if assert_status test.assert_equal spec[:status], actual.status, "expected #{verb.to_s.upcase} #{request_path} to respond #{spec[:status]}, " \ "got #{actual.status}" end if Minitest::OpenAPI.configuration.validate_responses && spec[:schema] && actual.status == spec[:status] Validator.new(Minitest::OpenAPI.components).validate!( spec[:schema], parse_body(actual), context: "#{verb.to_s.upcase} #{doc_path} -> #{spec[:status]}" ) end actual end |