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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
2 changes: 1 addition & 1 deletion lib/serpapi/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions spec/serpapi/client/response_format_spec.rb
Original file line number Diff line number Diff line change
@@ -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('<p>Results</p>', 'text/html ; charset=UTF-8', 'html')

expect(client.search(q: 'coffee', output: 'html')).to eq('<p>Results</p>')
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