Skip to content

Commit

Permalink
Add support for validating text responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mkon committed Aug 29, 2024
1 parent 7a7578b commit f58586a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
9 changes: 7 additions & 2 deletions .helix/languages.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[language-server.solargraph]
config.diagnostics=false
config.formatting=false

[[language]]
name = "ruby"
auto-format = true
auto-format = false
language-servers = [
{ name = "rubocop" }
{ name = "rubocop" },
{ name="solargraph" }
]
1 change: 1 addition & 0 deletions lib/openapi_contracts/payload_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ def register(matcher, parser)

PayloadParser.register(%r{(/|\+)json$}, ->(raw) { JSON(raw) })
PayloadParser.register('application/x-www-form-urlencoded', ->(raw) { Rack::Utils.parse_nested_query(raw) })
PayloadParser.register(%r{^text/}, ->(raw) { raw })
end
12 changes: 12 additions & 0 deletions spec/fixtures/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ paths:
$ref: '#/components/responses/GenericError'
'500':
description: Server Error
/html:
get:
operationId: get_html
summary: HTML test
responses:
'200':
description: Empty string
content:
text/html:
schema:
type: string
maxLength: 1
/numbers:
get:
operationId: numbers
Expand Down
20 changes: 20 additions & 0 deletions spec/integration/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,24 @@
it { is_expected.to_not match_openapi_doc(doc, parameters: true) }
end
end

context 'when validating html responses' do
let(:method) { 'GET' }
let(:path) { '/html' }
let(:response_body) { ' ' }
let(:response_headers) do
{
'Content-Type' => 'text/html;charset=utf-8',
'X-Request-Id' => 'some-request-id'
}
end

it { is_expected.to match_openapi_doc(doc).with_http_status(:ok) }

context 'when the response is too long' do
let(:response_body) { 'too long' }

it { is_expected.to_not match_openapi_doc(doc).with_http_status(:ok) }
end
end
end
8 changes: 4 additions & 4 deletions spec/openapi_contracts/coverage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
it 'can generate a report' do
data = subject.as_json
expect(data.dig('meta', 'operations', 'covered')).to eq(3)
expect(data.dig('meta', 'operations', 'total')).to eq(9)
expect(data.dig('meta', 'operations', 'total')).to eq(10)
expect(data.dig('meta', 'responses', 'covered')).to eq(4)
expect(data.dig('meta', 'responses', 'total')).to eq(16)
expect(data.dig('meta', 'responses', 'total')).to eq(17)
end
end

Expand All @@ -58,9 +58,9 @@
it 'can generate a report', :aggregate_failures do
data = subject.as_json
expect(data.dig('meta', 'operations', 'covered')).to eq(3)
expect(data.dig('meta', 'operations', 'total')).to eq(9)
expect(data.dig('meta', 'operations', 'total')).to eq(10)
expect(data.dig('meta', 'responses', 'covered')).to eq(4)
expect(data.dig('meta', 'responses', 'total')).to eq(16)
expect(data.dig('meta', 'responses', 'total')).to eq(17)
end
end
end
2 changes: 1 addition & 1 deletion spec/openapi_contracts/doc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@

it { is_expected.to all be_a(OpenapiContracts::Doc::Response) }

it { is_expected.to have_attributes(count: 16) }
it { is_expected.to have_attributes(count: 17) }
end
end

0 comments on commit f58586a

Please sign in to comment.