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_openapi_response(metadata) ⇒ 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_openapi_response(metadata) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/openapi_ruby/adapters/rspec.rb', line 169 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 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)
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 |
# File 'lib/openapi_ruby/adapters/rspec.rb', line 110 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"] 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" 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 # Append query params to path when body is present if params.any? query_string = params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&") path = "#{path}?#{query_string}" end else request_args = {params: params, headers: headers} end send(method.to_sym, path, **request_args) end |