Skip to content

Commit 7f2219c

Browse files
committed
Merge branch 'example-test-suite'
2 parents 261f6ab + bdfc4d9 commit 7f2219c

3 files changed

Lines changed: 82 additions & 86 deletions

File tree

spec/examples_spec.rb

Lines changed: 0 additions & 86 deletions
This file was deleted.

spec/test_cases_spec.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
require 'rack/test'
4+
require_relative 'spec_helper'
5+
6+
RSpec.describe 'request/response validation examples' do
7+
include Rack::Test::Methods
8+
9+
Dir.glob(File.join(__dir__, '/test_cases/*.yaml')).each do |filepath|
10+
describe filepath do
11+
YAML.load_file(filepath).each do |example|
12+
context example['description'] do
13+
let(:oad) { example['oad'] }
14+
let(:definition) { OpenapiFirst.parse(oad, filepath:) }
15+
16+
let(:app) do
17+
lambda do |_env|
18+
body = response['body']
19+
body = JSON.generate(response['body']) unless response['body'].is_a?(String)
20+
Rack::Response[
21+
response['status'] || 200,
22+
{ 'Content-Type' => response['content_type'] || 'application/json' },
23+
[body]
24+
].finish
25+
end
26+
end
27+
28+
let(:test_path) do
29+
oad['paths'].keys.first
30+
end
31+
32+
let(:test_method) do
33+
oad['paths'][test_path].keys.first.upcase
34+
end
35+
36+
if example['valid_response']
37+
context 'with valid response' do
38+
let(:response) { example['valid_response'] }
39+
40+
it 'passes validation' do
41+
send(test_method.downcase, test_path)
42+
43+
request = Rack::Request.new(last_request.env)
44+
body = last_response.body.is_a?(String) ? last_response.body : last_response.body.join
45+
response = Rack::Response.new(
46+
body,
47+
last_response.status,
48+
last_response.headers
49+
)
50+
51+
validated = definition.validate_response(request, response)
52+
expect(validated).to be_valid
53+
end
54+
end
55+
end
56+
57+
if example['invalid_response']
58+
context 'with invalid response' do
59+
let(:response) { example['invalid_response'] }
60+
61+
it 'fails validation' do
62+
send(test_method.downcase, test_path)
63+
64+
request = Rack::Request.new(last_request.env)
65+
body = last_response.body.is_a?(String) ? last_response.body : last_response.body.join
66+
response = Rack::Response.new(
67+
body,
68+
last_response.status,
69+
last_response.headers
70+
)
71+
72+
validated = definition.validate_response(request, response)
73+
expect(validated).not_to be_valid
74+
expect(validated.error).not_to be_nil
75+
end
76+
end
77+
end
78+
end
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)