|
| 1 | +require 'spec_helper' |
| 2 | +require 'stringio' |
| 3 | + |
| 4 | +describe 'JSON parser option precedence' do |
| 5 | + let(:transport) { double('HTTP client') } |
| 6 | + let(:response) do |
| 7 | + double( |
| 8 | + status: 200, |
| 9 | + headers: { 'Content-Type' => 'application/json' }, |
| 10 | + body: '{"search_metadata":{"id":"offline"}}', |
| 11 | + flush: nil |
| 12 | + ) |
| 13 | + end |
| 14 | + |
| 15 | + before do |
| 16 | + allow(HTTP).to receive(:timeout).and_return(transport) |
| 17 | + allow(transport).to receive(:get).and_return(response) |
| 18 | + allow(transport).to receive(:post).and_return(response) |
| 19 | + end |
| 20 | + |
| 21 | + [nil, true, false].each do |constructor_option| |
| 22 | + [nil, true, false].each do |call_option| |
| 23 | + it "decodes JSON with constructor #{constructor_option.inspect} and call #{call_option.inspect}" do |
| 24 | + defaults = { persistent: false } |
| 25 | + defaults[:symbolize_names] = constructor_option unless constructor_option.nil? |
| 26 | + params = { q: 'coffee' } |
| 27 | + params[:symbolize_names] = call_option unless call_option.nil? |
| 28 | + client = SerpApi::Client.new(defaults) |
| 29 | + symbolize = call_option.nil? ? constructor_option != false : call_option |
| 30 | + key = symbolize ? :search_metadata : 'search_metadata' |
| 31 | + nested_key = symbolize ? :id : 'id' |
| 32 | + |
| 33 | + expect(client.search(params)).to eq(key => { nested_key => 'offline' }) |
| 34 | + expect(transport).to have_received(:get) do |_url, options| |
| 35 | + expect(options.fetch(:params)).not_to have_key(:symbolize_names) |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + it 'uses constructor defaults for archived searches' do |
| 42 | + client = SerpApi::Client.new(persistent: false, symbolize_names: false) |
| 43 | + |
| 44 | + expect(client.search_archive('offline')).to have_key('search_metadata') |
| 45 | + end |
| 46 | + |
| 47 | + it 'uses constructor defaults and flushes persistent responses' do |
| 48 | + allow(transport).to receive(:persistent).and_return(transport) |
| 49 | + client = SerpApi::Client.new(symbolize_names: false) |
| 50 | + |
| 51 | + expect(client.search(q: 'coffee')).to have_key('search_metadata') |
| 52 | + expect(response).to have_received(:flush) |
| 53 | + end |
| 54 | + |
| 55 | + it 'uses constructor defaults for account responses' do |
| 56 | + client = SerpApi::Client.new(persistent: false, symbolize_names: false) |
| 57 | + |
| 58 | + expect(client.account).to have_key('search_metadata') |
| 59 | + end |
| 60 | + |
| 61 | + it 'uses constructor defaults for image uploads without sending parser options' do |
| 62 | + client = SerpApi::Client.new(persistent: false, symbolize_names: false) |
| 63 | + |
| 64 | + expect(client.upload_image(StringIO.new('image'))).to have_key('search_metadata') |
| 65 | + expect(transport).to have_received(:post) do |_url, options| |
| 66 | + expect(options.fetch(:form)).not_to have_key(:symbolize_names) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + it 'allows an image upload to override the constructor option' do |
| 71 | + client = SerpApi::Client.new(persistent: false, symbolize_names: false) |
| 72 | + |
| 73 | + expect(client.upload_image(StringIO.new('image'), symbolize_names: true)).to have_key(:search_metadata) |
| 74 | + end |
| 75 | + |
| 76 | + { html: 'text/html', md: 'text/markdown' }.each do |format, content_type| |
| 77 | + it "keeps #{format} responses as text" do |
| 78 | + allow(response).to receive(:headers).and_return('Content-Type' => content_type) |
| 79 | + allow(response).to receive(:body).and_return('raw text') |
| 80 | + client = SerpApi::Client.new(persistent: false, symbolize_names: false) |
| 81 | + |
| 82 | + expect(client.public_send(format, q: 'coffee')).to eq('raw text') |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments