forked from serpapi/serpapi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timeout.py
More file actions
39 lines (30 loc) · 1.41 KB
/
test_timeout.py
File metadata and controls
39 lines (30 loc) · 1.41 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
import pytest
import requests
from serpapi import Client
def test_client_timeout_setting():
"""Test that timeout can be set on the client and is passed to the request."""
client = Client(api_key="test_key", timeout=10)
assert client.timeout == 10
def test_request_timeout_override(monkeypatch):
"""Test that timeout can be overridden in the search method."""
client = Client(api_key="test_key", timeout=10)
def mock_request(method, url, params, headers, timeout, **kwargs):
assert timeout == 5
# Return a mock response object
mock_response = requests.Response()
mock_response.status_code = 200
mock_response._content = b'{"search_metadata": {"id": "123"}}'
return mock_response
monkeypatch.setattr(client.session, "request", mock_request)
client.search(q="coffee", timeout=5)
def test_request_default_timeout(monkeypatch):
"""Test that the client's default timeout is used if none is provided in search."""
client = Client(api_key="test_key", timeout=10)
def mock_request(method, url, params, headers, timeout, **kwargs):
assert timeout == 10
mock_response = requests.Response()
mock_response.status_code = 200
mock_response._content = b'{"search_metadata": {"id": "123"}}'
return mock_response
monkeypatch.setattr(client.session, "request", mock_request)
client.search(q="coffee")