11require 'spec_helper'
22
3- describe 'client full code coverage' do
4- before ( :all ) do
5- @ client = SerpApi ::Client . new ( engine : 'google' , api_key : ENV [ 'API_KEY ' ] , timeout : 30 )
3+ describe 'set of client test to archieve full code coverage' do
4+ let ( :client ) do
5+ client = SerpApi ::Client . new ( engine : 'google' , api_key : ENV [ 'SERPAPI_KEY ' ] , timeout : 30 )
66 end
77
88 it 'search for coffee in Austin, TX and receive json results' do
9- data = @ client. search ( q : 'Coffee' , location : 'Austin, TX' )
9+ data = client . search ( q : 'Coffee' , location : 'Austin, TX' )
1010 expect ( data . size ) . to be > 5
1111 expect ( data . class ) . to be Hash
1212 expect ( data . keys . size ) . to be > 5
1313 end
1414
1515 it 'search fir coffee in Austin, TX and receive raw HTML' do
16- data = @ client. html ( q : 'Coffee' , location : 'Austin, TX' )
16+ data = client . html ( q : 'Coffee' , location : 'Austin, TX' )
1717 expect ( data ) . to match ( /coffee/i )
1818 end
1919
2020 it 'missing query' do
2121 begin
22- @ client. search ( { } )
22+ client . search ( { } )
2323 rescue SerpApi ::SerpApiError => e
2424 expect ( e . message ) . to include ( 'Missing query' )
2525 rescue => e
2828 end
2929
3030 it 'get params' do
31- expect ( @ client. params [ :api_key ] ) . to eq ( ENV [ 'API_KEY ' ] )
31+ expect ( client . params [ :api_key ] ) . to eq ( ENV [ 'SERPAPI_KEY ' ] )
3232 end
3333
3434 it 'api_key' do
35- expect ( @ client. api_key ) . to eq ( ENV [ 'API_KEY ' ] )
35+ expect ( client . api_key ) . to eq ( ENV [ 'SERPAPI_KEY ' ] )
3636 end
3737
3838 it 'engine' do
39- expect ( @ client. engine ) . to eq ( 'google' )
39+ expect ( client . engine ) . to eq ( 'google' )
4040 end
4141
4242 it 'timeout' do
43- expect ( @client . timeout ) . to eq ( 30 )
43+ expect ( client . timeout ) . to eq ( 30 )
44+ end
45+
46+ it 'persistent' do
47+ expect ( client . persistent ) . to be true
4448 end
4549
4650 it 'get bad decoder' do
4751 begin
48- @ client. send ( :get , '/search' , :bad , { q : 'hello' } )
52+ client . send ( :get , '/search' , :bad , { q : 'hello' } )
4953 rescue SerpApi ::SerpApiError => e
5054 expect ( e . message ) . to include ( 'not supported decoder' )
5155 rescue => e
5256 raise ( "wrong exception: #{ e } " )
5357 end
5458 end
59+
60+ it 'get endpoint error' do
61+ expect {
62+ client . send ( :get , '/search' , :json , { } )
63+ } . to raise_error ( SerpApi ::SerpApiError ) . with_message ( /HTTP request failed with error: Missing query `q` parameter./ )
64+ end
65+
66+ it 'get bad endpoint' do
67+ begin
68+ client . send ( :get , '/invalid' , :json , { } )
69+ rescue SerpApi ::SerpApiError => e
70+ expect ( e . message ) . to include ( 'JSON parse error' )
71+ rescue => e
72+ raise ( "wrong exception: #{ e } " )
73+ end
74+ end
75+
76+ it 'get bad html endpoint' do
77+ begin
78+ client . send ( :get , '/invalid' , :html , { } )
79+ rescue SerpApi ::SerpApiError => e
80+ expect ( e . message ) . to include ( 'HTTP request failed with response status: 404' )
81+ rescue => e
82+ raise ( "wrong exception: #{ e } " )
83+ end
84+ end
5585end
5686
57- describe 'SerpApi client adapter' do
87+ describe 'SerpApi client with persitency enable' do
88+
5889 let ( :client ) do
59- SerpApi ::Client . new ( engine : 'google' , api_key : ENV [ 'API_KEY ' ] , timeout : 10 , persistency : true )
90+ SerpApi ::Client . new ( engine : 'google' , api_key : ENV [ 'SERPAPI_KEY ' ] , timeout : 10 , persistent : true )
6091 end
6192
62- it 'makes a search request with valid parameters ' do
93+ it 'check socket is open when persistent mode is enabled ' do
6394 expect ( client . socket ) . to_not be_nil
95+ expect ( client . persistent ) . to be true
96+ end
97+
98+ it 'makes a search request with valid parameters' do
6499 response = client . search ( q : 'Coffee' , location : 'Austin, TX' )
65100 expect ( response . size ) . to be > 5
66101 expect ( response . class ) . to be Hash
67102 expect ( response . keys . size ) . to be > 5
68103 expect ( response [ :search_metadata ] [ :id ] ) . not_to be_nil
104+
105+ expect ( client . close ) . to eq ( :clean )
69106 end
70107
71108 it 'handles API errors' do
72109 allow ( client ) . to receive ( :search ) . and_raise ( SerpApi ::SerpApiError )
73110 expect { client . search ( q : 'Invalid Query' ) } . to raise_error ( SerpApi ::SerpApiError )
74111 end
112+
113+ end
114+
115+ describe 'SerpApi client with persitency disabled' do
116+ it 'check socket is closed when persistent mode is disabled' do
117+ client = SerpApi ::Client . new ( engine : 'google' , api_key : ENV [ 'SERPAPI_KEY' ] , timeout : 10 , persistent : false )
118+ expect ( client . persistent ) . to be false
119+ expect ( client . socket ) . to be_nil
120+ expect ( client . close ) . to be_nil
121+
122+ client . search ( q : 'Coffee' , location : 'Austin, TX' )
123+ expect ( client . socket ) . to be_nil
124+ expect ( client . close ) . to be_nil
125+ end
75126end
0 commit comments