From f58586ac4fdd77fdd747c9d14c1a97a12b41e787 Mon Sep 17 00:00:00 2001 From: Konstantin Munteanu Date: Thu, 29 Aug 2024 23:49:01 +0200 Subject: [PATCH] Add support for validating text responses --- .helix/languages.toml | 9 +++++++-- lib/openapi_contracts/payload_parser.rb | 1 + spec/fixtures/openapi/openapi.yaml | 12 ++++++++++++ spec/integration/rspec_spec.rb | 20 ++++++++++++++++++++ spec/openapi_contracts/coverage_spec.rb | 8 ++++---- spec/openapi_contracts/doc_spec.rb | 2 +- 6 files changed, 45 insertions(+), 7 deletions(-) diff --git a/.helix/languages.toml b/.helix/languages.toml index cb12431..d8ae770 100644 --- a/.helix/languages.toml +++ b/.helix/languages.toml @@ -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" } ] diff --git a/lib/openapi_contracts/payload_parser.rb b/lib/openapi_contracts/payload_parser.rb index a91e773..288f2db 100644 --- a/lib/openapi_contracts/payload_parser.rb +++ b/lib/openapi_contracts/payload_parser.rb @@ -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 diff --git a/spec/fixtures/openapi/openapi.yaml b/spec/fixtures/openapi/openapi.yaml index a4d217e..db99d5e 100644 --- a/spec/fixtures/openapi/openapi.yaml +++ b/spec/fixtures/openapi/openapi.yaml @@ -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 diff --git a/spec/integration/rspec_spec.rb b/spec/integration/rspec_spec.rb index a613a3c..ef3ae98 100644 --- a/spec/integration/rspec_spec.rb +++ b/spec/integration/rspec_spec.rb @@ -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 diff --git a/spec/openapi_contracts/coverage_spec.rb b/spec/openapi_contracts/coverage_spec.rb index 1eee892..28d181e 100644 --- a/spec/openapi_contracts/coverage_spec.rb +++ b/spec/openapi_contracts/coverage_spec.rb @@ -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 @@ -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 diff --git a/spec/openapi_contracts/doc_spec.rb b/spec/openapi_contracts/doc_spec.rb index 32a93c1..905b1f0 100644 --- a/spec/openapi_contracts/doc_spec.rb +++ b/spec/openapi_contracts/doc_spec.rb @@ -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