From d98ff0b15c12c6f412afb73b406ba44c01fecc25 Mon Sep 17 00:00:00 2001 From: Suyash Bhattarai <133975972+ItsM0rty@users.noreply.github.com> Date: Wed, 2 Dec 2026 07:40:01 +0545 Subject: [PATCH] Normalize response media types before decoding --- CHANGELOG.md | 1 + lib/serpapi/client.rb | 2 +- spec/serpapi/client/response_format_spec.rb | 33 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 spec/serpapi/client/response_format_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index dd9676f..69c0e15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # Changelog + * [Unreleased] Decode response media types case-insensitively and allow whitespace before parameters * [Unreleased] Identify the library, Ruby runtime, and platform in the `User-Agent` header (e.g. `serpapi-ruby/1.1.0 ruby/3.4.1 (darwin; arm64)`) instead of the `source` query parameter * [Unreleased] **BREAKING:** JSON responses with HTTP 200 and a top-level `error` key no longer raise `SerpApiError`. SerpApi returns searches with empty results this way (e.g. "Google hasn't returned any results for this query."), so `search`, `search_archive`, and `extract` now return them as a Hash with the message in `results[:error]`. `SerpApiError` is raised only for non-200 responses. Code that rescued `SerpApiError` to detect empty results must check `results[:error]` instead. With `symbolize_names: false`, these responses were already returned, with the message in `results["error"]` #17 * [Unreleased] Set `SerpApiError#serpapi_error` and `#search_id` when using `symbolize_names: false` diff --git a/lib/serpapi/client.rb b/lib/serpapi/client.rb index a454e1f..e14700a 100644 --- a/lib/serpapi/client.rb +++ b/lib/serpapi/client.rb @@ -356,7 +356,7 @@ def http_client end def response_decoder(response, default) - content_type = response.headers['Content-Type'].to_s.split(';').first + content_type = response.headers['Content-Type'].to_s.partition(';').first.strip.downcase CONTENT_TYPE_DECODERS.fetch(content_type, default) end diff --git a/spec/serpapi/client/response_format_spec.rb b/spec/serpapi/client/response_format_spec.rb new file mode 100644 index 0000000..e6acdab --- /dev/null +++ b/spec/serpapi/client/response_format_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe 'Response formats' do + let(:socket) { double } + let(:client) { SerpApi::Client.new(api_key: 'test-key') } + + before do + allow(HTTP).to receive(:timeout).and_return(double(headers: double(persistent: socket))) + end + + def stub_response(body, content_type, output) + response = double(status: 200, body: body, headers: { 'Content-Type' => content_type }, flush: nil) + allow(socket).to receive(:get).with('/search', params: { api_key: 'test-key', q: 'coffee', output: output }).and_return(response) + end + + it 'returns Markdown when the media type uses mixed case' do + stub_response('# Results', 'Text/Markdown; charset=UTF-8', 'md') + + expect(client.search(q: 'coffee', output: 'md')).to eq('# Results') + end + + it 'returns HTML when the media type has whitespace before the parameter' do + stub_response('

Results

', 'text/html ; charset=UTF-8', 'html') + + expect(client.search(q: 'coffee', output: 'html')).to eq('

Results

') + end + + it 'keeps the requested decoder when there is no recognized media type' do + stub_response('# Results', nil, 'md') + + expect(client.md(q: 'coffee')).to eq('# Results') + end +end