-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient_spec.rb
More file actions
149 lines (121 loc) · 4.37 KB
/
client_spec.rb
File metadata and controls
149 lines (121 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'spec_helper'
describe 'set of client test to archieve full code coverage' do
let(:client) do
SerpApi::Client.new(engine: 'google', api_key: ENV['SERPAPI_KEY'], timeout: 30)
end
it 'search for coffee in Austin, TX, returns symbolized Hash' do
results = client.search(q: 'Coffee', location: 'Austin, TX')
expect(results.size).to be > 5
expect(results.class).to be Hash
expect(results.keys.size).to be > 5
expect(results.keys).to include(:search_metadata), ':search_metadata should be present in the results'
end
it 'search for coffee in Austin, TX, returns non-symbolized Hash' do
results = client.search(q: 'Coffee', location: 'Austin, TX', symbolize_names: false)
expect(results.size).to be > 5
expect(results.class).to be Hash
expect(results.keys.size).to be > 5
expect(results.keys).to include('search_metadata'), 'search_metadata should be present in the results'
end
it 'search for coffee in Austin, TX and receive raw HTML' do
results = client.html(q: 'Coffee', location: 'Austin, TX')
expect(results).to match(/coffee/i)
end
it 'missing query' do
begin
client.search({})
rescue SerpApi::SerpApiError => e
expect(e.message).to include('Missing query')
rescue => e
raise("wrong exception: #{e}")
end
end
it 'get params' do
expect(client.params[:api_key]).to eq(ENV['SERPAPI_KEY'])
end
it 'api_key' do
expect(client.api_key).to eq(ENV['SERPAPI_KEY'])
end
it 'engine' do
expect(client.engine).to eq('google')
end
it 'timeout' do
expect(client.timeout).to eq(30)
end
it 'persistent' do
expect(client.persistent).to be true
end
it 'get bad decoder' do
begin
client.send(:get, '/search', :bad, {q: 'hello'})
rescue SerpApi::SerpApiError => e
expect(e.message).to include('not supported decoder')
rescue => e
raise("wrong exception: #{e}")
end
end
it 'get endpoint error' do
expect {
client.send(:get, '/search', :json, {})
}.to raise_error(SerpApi::SerpApiError).with_message(/HTTP request failed with status: 400.* error: Missing query `q` parameter./)
end
it 'get bad endpoint' do
begin
client.send(:get, '/invalid', :json, {})
rescue SerpApi::SerpApiError => e
expect(e.message).to include('JSON parse error')
rescue => e
raise("wrong exception: #{e}")
end
end
it 'get bad html endpoint' do
begin
client.send(:get, '/invalid', :html, {})
rescue SerpApi::SerpApiError => e
expect(e.message).to include("HTTP request failed with status: 404")
rescue => e
raise("wrong exception: #{e}")
end
end
it 'should not expose api_key via inspect' do
inspect_str = client.inspect
expect(inspect_str).to_not include(ENV['SERPAPI_KEY'])
end
it 'should gracefully handle api_key values shorter than 8 characters' do
short_key_client = SerpApi::Client.new(engine: 'google', api_key: 'abcdef', timeout: 10)
inspect_str = short_key_client.inspect
expect(inspect_str).to_not include('abcdef')
end
end
describe 'SerpApi client with persitency enable' do
let(:client) do
SerpApi::Client.new(engine: 'google', api_key: ENV['SERPAPI_KEY'], timeout: 10, persistent: true)
end
it 'check socket is open when persistent mode is enabled' do
expect(client.socket).to_not be_nil
expect(client.persistent).to be true
end
it 'makes a search request with valid parameters' do
results = client.search(q: 'Coffee', location: 'Austin, TX')
expect(results.size).to be > 5
expect(results.class).to be Hash
expect(results.keys.size).to be > 5
expect(results[:search_metadata][:id]).not_to be_nil
expect(client.close).to eq(:clean)
end
it 'handles API errors' do
allow(client).to receive(:search).and_raise(SerpApi::SerpApiError)
expect { client.search(q: 'Invalid Query') }.to raise_error(SerpApi::SerpApiError)
end
end
describe 'SerpApi client with persitency disabled' do
it 'check socket is closed when persistent mode is disabled' do
client = SerpApi::Client.new(engine: 'google', api_key: ENV['SERPAPI_KEY'], timeout: 10, persistent: false)
expect(client.persistent).to be false
expect(client.socket).to be_nil
expect(client.close).to be_nil
client.search(q: 'Coffee', location: 'Austin, TX')
expect(client.socket).to be_nil
expect(client.close).to be_nil
end
end