11"""
2- Shared fixtures for the SDK's test suite
2+ Shared fixtures for integration and unit tests.
33"""
44
55import pytest
6- import httpx
76from scrape_do .models import RequestParameters , PreparedScrapeDoRequest
8- from unittest .mock import MagicMock
9- from scrape_do .client import ScrapeDoClient
10-
11-
12- @pytest .fixture
13- def example_url () -> str :
14- """
15- Provides a valid fake url to be used for model testing
16-
17- Returns:
18- A valid fake url
19- """
20- return "https://example.com/"
21-
22-
23- @pytest .fixture
24- def mock_json_payload () -> dict :
25- """
26- Provides a fake JSON dictionary including all keys that can be present
27- in the JSON returned by Scrape.do when `returnJSON=true`.
28-
29- Returns:
30- The fake JSON dictionary
31- """
32- return {
33- "statusCode" : 200 ,
34- "content" : "<html>Target Data</html>" ,
35- "networkRequests" : [{
36- "url" : "https://example.com/api" ,
37- "method" : "POST" ,
38- "status" : 204 ,
39- "request_headers" : {},
40- "request_body" : "{\" req\" : \" data\" }" ,
41- "response_body" : "" ,
42- "response_headers" : {}
43- }],
44- "websocketRequests" : [{
45- "type" : "received" ,
46- "event" : {
47- "requestId" : "586051.322" ,
48- "timestamp" : 21815567.089025 ,
49- "response" : {
50- "opcode" : 1 ,
51- "mask" : False ,
52- "payloadData" : "{\" live_price\" : 65000.00}"
53- }
54- }
55- }],
56- "actionResults" : [
57- {
58- "action" : "Click" ,
59- "index" : 0 ,
60- "success" : False ,
61- "error" : "Element not found" ,
62- "response" : None
63- },
64- {
65- "action" : "Wait" ,
66- "index" : 1 ,
67- "success" : True
68- }
69- ],
70- "screenShots" : [{
71- "type" : "FullScreenShot" ,
72- "image" : "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HA" ,
73- "error" : None
74- }],
75- "frames" : [{
76- "url" : "https://example.com/iframe" ,
77- "content" : "<html>Iframe Content</html>"
78- }]
79- }
80-
81-
82- @pytest .fixture
83- def mock_headers () -> httpx .Headers :
84- """
85- Provides a fake httpx.Headers object including all headers returned by
86- Scrape.do.
87-
88- Returns:
89- The fake httpx.Headers object
90- """
91- headers = {
92- "server" : "cloudflare" ,
93- "x-frame-options" : "DENY" ,
94- "transfer-encoding" : "chunked" ,
95- "scrape.do-auth" : "0" ,
96- "scrape.do-cookies" : "cookie1=value1;cookie2=value2" ,
97- "scrape.do-initial-status-code" : "200" ,
98- "scrape.do-rate" : "0:0" ,
99- "scrape.do-remaining-credits" : "300000" ,
100- "scrape.do-request-cost" : "25" ,
101- "scrape.do-request-id" : "123e4567-e89b-12d3-a456-426614174000" ,
102- "scrape.do-resolved-url" : "https://example.com/final" ,
103- "scrape.do-rid" : "node-123" ,
104- "scrape.do-target-url" : "https://example.com"
105- }
106-
107- return httpx .Headers (headers )
1087
1098
1109@pytest .fixture
@@ -120,51 +19,3 @@ def _make(
12019 params = RequestParameters (url = url , ** kwargs )
12120 return PreparedScrapeDoRequest (api_params = params , method = method )
12221 return _make
123-
124-
125- @pytest .fixture
126- def make_response ():
127- """
128- Factory to generate strictly controlled httpx.Response mocks.
129- """
130- def _make (
131- status_code : int ,
132- json_data : dict = None ,
133- text : str = None ,
134- proxy_status_header : str = None
135- ):
136- headers = {}
137- if proxy_status_header is not None :
138- headers ["scrape.do-initial-status-code" ] = str (proxy_status_header )
139-
140- if json_data is not None :
141- return httpx .Response (status_code , headers = headers , json = json_data )
142- return httpx .Response (status_code , headers = headers , text = text or "" )
143- return _make
144-
145-
146- @pytest .fixture
147- def mock_sync_client ():
148- """
149- Yields a cleanly initialized ScrapeDoClient for testing.
150- """
151- with ScrapeDoClient (api_token = 'dummy_token' ) as client :
152- yield client
153-
154-
155- @pytest .fixture
156- def mock_env_vars (monkeypatch : pytest .MonkeyPatch ):
157- """Clears the SCRAPE_DO_API_KEY from the environment for a test function
158-
159- Guarantees that tests do not accidentally inherit a real
160- developer token from the local machine's environment variables
161- """
162- monkeypatch .delenv ("SCRAPE_DO_API_KEY" , raising = False )
163-
164-
165- @pytest .fixture
166- def mock_sleep (mocker ) -> MagicMock :
167- """
168- Mocks `time.sleep` across an entire test function.
169- """
170- return mocker .patch ("time.sleep" , return_value = None )
0 commit comments