Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions spec/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- description: nullable array items in response body
oad:
openapi: 3.0.3
info:
title: Response Validation Middleware Example
version: 1.0.0
paths:
/:
get:
responses:
'200':
content:
application/json:
schema:
properties:
path:
type: array
items:
type: string
nullable: true
valid_response:
content_type: application/json
body: { "path": ["1", "2", null] }
invalid_response:
content_type: application/json
body: { "path": ["1", "2", 3] }
86 changes: 86 additions & 0 deletions spec/examples_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# frozen_string_literal: true

require 'rack/test'
require_relative 'spec_helper'

RSpec.describe 'request/response validation examples' do
include Rack::Test::Methods

# Load all examples from the YAML file
examples = YAML.load_file(File.join(__dir__, 'examples.yaml'))

examples.each do |example|
context example['description'] do
let(:oad) { example['oad'] }
let(:definition) { OpenapiFirst.parse(oad) }

let(:app) do
lambda do |_env|
body = if response['body'].is_a?(String)
response['body']
else
JSON.generate(response['body'])
end
[
response['status'] || 200,
{ 'Content-Type' => response['content_type'] || 'application/json' },
[body]
]
end
end

# Get the first path from the OAD
let(:test_path) do
oad['paths'].keys.first
end

# Get the HTTP method for the test path
let(:test_method) do
oad['paths'][test_path].keys.first.upcase
end

if example['valid_response']
context 'with valid response' do
let(:response) { example['valid_response'] }

it 'passes validation' do
send(test_method.downcase, test_path)

request = Rack::Request.new(last_request.env)
body = last_response.body.is_a?(String) ? last_response.body : last_response.body.join
response = Rack::Response.new(
body,
last_response.status,
last_response.headers
)

validated = definition.validate_response(request, response)
expect(validated).to be_valid
end
end
end

if example['invalid_response']
context 'with invalid response' do
let(:response) { example['invalid_response'] }

it 'fails validation' do
send(test_method.downcase, test_path)

request = Rack::Request.new(last_request.env)
body = last_response.body.is_a?(String) ? last_response.body : last_response.body.join
response = Rack::Response.new(
body,
last_response.status,
last_response.headers
)

validated = definition.validate_response(request, response)
expect(validated).not_to be_valid
expect(validated.error).not_to be_nil
end
end
end
end
end
end
Loading