PythonSTB commited on
Commit
a06131d
·
verified ·
1 Parent(s): 6aef22f

Upload curl-cffi/Test_Curl_Cffi.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. curl-cffi/Test_Curl_Cffi.py +397 -0
curl-cffi/Test_Curl_Cffi.py ADDED
@@ -0,0 +1,397 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ On-device verification for the cross-compiled curl_cffi wheel (C++ / curl-impersonate).
3
+
4
+ Run after installing:
5
+ pip install curl_cffi-<ver>-cp312-cp312-android_24_<ABI>.whl
6
+ where <ABI> = arm64_v8a (real device) or x86_64 (emulator)
7
+
8
+ Usage:
9
+ python Test_curl_cffi.py [--quick]
10
+
11
+ Exit code 0 = everything required PASSed.
12
+ Sections marked [SKIP] are optional (need network).
13
+
14
+ Generated by RIMI
15
+ """
16
+ import sys
17
+
18
+ RESULTS = []
19
+
20
+
21
+ def test(name, fn):
22
+ try:
23
+ fn()
24
+ RESULTS.append((name, "PASS", None))
25
+ except NotImplementedError as exc:
26
+ RESULTS.append((name, "SKIP", str(exc)))
27
+ except Exception as exc:
28
+ RESULTS.append((name, "FAIL", "%s: %s" % (type(exc).__name__, exc)))
29
+ print(" ! %s -> %s: %s" % (name, type(exc).__name__, exc))
30
+
31
+
32
+ def section(title):
33
+ print("=" * 60)
34
+ print(title)
35
+ print("=" * 60)
36
+
37
+
38
+ # ---------------------------------------------------------------------------
39
+ # 1. import / version
40
+ # ---------------------------------------------------------------------------
41
+ def import_curl_cffi():
42
+ import curl_cffi
43
+ print(" curl_cffi", curl_cffi.__version__)
44
+ assert isinstance(curl_cffi.__version__, str)
45
+
46
+
47
+ def import_requests():
48
+ import curl_cffi.requests
49
+ print(" curl_cffi.requests OK")
50
+
51
+
52
+ # ---------------------------------------------------------------------------
53
+ # 2. Session creation
54
+ # ---------------------------------------------------------------------------
55
+ def session_create():
56
+ from curl_cffi.requests import Session
57
+ s = Session()
58
+ assert s is not None
59
+ s.close()
60
+
61
+
62
+ def session_context_manager():
63
+ from curl_cffi.requests import Session
64
+ with Session() as s:
65
+ assert s is not None
66
+
67
+
68
+ # ---------------------------------------------------------------------------
69
+ # 3. Impersonate profiles
70
+ # ---------------------------------------------------------------------------
71
+ def impersonate_chrome():
72
+ from curl_cffi.requests import Session
73
+ s = Session(impersonate="chrome")
74
+ assert s is not None
75
+ s.close()
76
+
77
+
78
+ def impersonate_safari():
79
+ from curl_cffi.requests import Session
80
+ s = Session(impersonate="safari")
81
+ assert s is not None
82
+ s.close()
83
+
84
+
85
+ def impersonate_edge():
86
+ from curl_cffi.requests import Session
87
+ s = Session(impersonate="edge")
88
+ assert s is not None
89
+ s.close()
90
+
91
+
92
+ def impersonate_firefox():
93
+ from curl_cffi.requests import Session
94
+ s = Session(impersonate="firefox")
95
+ assert s is not None
96
+ s.close()
97
+
98
+
99
+ # ---------------------------------------------------------------------------
100
+ # 4. Request methods
101
+ # ---------------------------------------------------------------------------
102
+ def request_methods():
103
+ from curl_cffi.requests import Session
104
+ with Session() as s:
105
+ assert hasattr(s, "get")
106
+ assert hasattr(s, "post")
107
+ assert hasattr(s, "put")
108
+ assert hasattr(s, "delete")
109
+ assert hasattr(s, "patch")
110
+ assert hasattr(s, "head")
111
+
112
+
113
+ # ---------------------------------------------------------------------------
114
+ # 5. Headers
115
+ # ---------------------------------------------------------------------------
116
+ def custom_headers():
117
+ from curl_cffi.requests import Session
118
+ with Session(impersonate="chrome") as s:
119
+ h = {"X-Custom": "value"}
120
+ try:
121
+ r = s.get("https://httpbin.org/headers", headers=h)
122
+ assert r.status_code == 200
123
+ except Exception:
124
+ raise NotImplementedError("network not available")
125
+
126
+
127
+ # ---------------------------------------------------------------------------
128
+ # 6. Cookies
129
+ # ---------------------------------------------------------------------------
130
+ def cookie_handling():
131
+ from curl_cffi.requests import Session
132
+ with Session(impersonate="chrome") as s:
133
+ try:
134
+ r = s.get("https://httpbin.org/cookies/set?name=test&value=123")
135
+ assert r.status_code == 200
136
+ except Exception:
137
+ raise NotImplementedError("network not available")
138
+
139
+
140
+ # ---------------------------------------------------------------------------
141
+ # 7. POST data
142
+ # ---------------------------------------------------------------------------
143
+ def post_form():
144
+ from curl_cffi.requests import Session
145
+ with Session(impersonate="chrome") as s:
146
+ try:
147
+ r = s.post("https://httpbin.org/post", data={"key": "value"})
148
+ assert r.status_code == 200
149
+ except Exception:
150
+ raise NotImplementedError("network not available")
151
+
152
+
153
+ def post_json():
154
+ from curl_cffi.requests import Session
155
+ with Session(impersonate="chrome") as s:
156
+ try:
157
+ r = s.post(
158
+ "https://httpbin.org/post",
159
+ json={"key": "value"},
160
+ )
161
+ assert r.status_code == 200
162
+ except Exception:
163
+ raise NotImplementedError("network not available")
164
+
165
+
166
+ # ---------------------------------------------------------------------------
167
+ # 8. Response attributes
168
+ # ---------------------------------------------------------------------------
169
+ def response_attributes():
170
+ from curl_cffi.requests import Session
171
+ with Session(impersonate="chrome") as s:
172
+ try:
173
+ r = s.get("https://httpbin.org/get")
174
+ assert hasattr(r, "status_code")
175
+ assert hasattr(r, "text")
176
+ assert hasattr(r, "content")
177
+ assert hasattr(r, "headers")
178
+ assert hasattr(r, "url")
179
+ assert r.status_code == 200
180
+ except Exception:
181
+ raise NotImplementedError("network not available")
182
+
183
+
184
+ # ---------------------------------------------------------------------------
185
+ # 9. JSON response
186
+ # ---------------------------------------------------------------------------
187
+ def response_json():
188
+ from curl_cffi.requests import Session
189
+ with Session(impersonate="chrome") as s:
190
+ try:
191
+ r = s.get("https://httpbin.org/json")
192
+ data = r.json()
193
+ assert isinstance(data, dict)
194
+ except Exception:
195
+ raise NotImplementedError("network not available")
196
+
197
+
198
+ # ---------------------------------------------------------------------------
199
+ # 10. Timeout
200
+ # ---------------------------------------------------------------------------
201
+ def timeout_setting():
202
+ from curl_cffi.requests import Session
203
+ with Session(impersonate="chrome", timeout=10) as s:
204
+ assert s is not None
205
+
206
+
207
+ # ---------------------------------------------------------------------------
208
+ # 11. proxies
209
+ # ---------------------------------------------------------------------------
210
+ def proxy_setting():
211
+ from curl_cffi.requests import Session
212
+ with Session(impersonate="chrome") as s:
213
+ assert s is not None
214
+
215
+
216
+ # ---------------------------------------------------------------------------
217
+ # 12. verify SSL
218
+ # ---------------------------------------------------------------------------
219
+ def verify_ssl():
220
+ from curl_cffi.requests import Session
221
+ with Session(impersonate="chrome", verify=False) as s:
222
+ assert s is not None
223
+
224
+
225
+ # ---------------------------------------------------------------------------
226
+ # 13. HTTP/2
227
+ # ---------------------------------------------------------------------------
228
+ def http2_support():
229
+ from curl_cffi.requests import Session
230
+ with Session(impersonate="chrome") as s:
231
+ assert s is not None
232
+
233
+
234
+ # ---------------------------------------------------------------------------
235
+ # 14. Binary content
236
+ # ---------------------------------------------------------------------------
237
+ def binary_content():
238
+ from curl_cffi.requests import Session
239
+ with Session(impersonate="chrome") as s:
240
+ try:
241
+ r = s.get("https://httpbin.org/image/png")
242
+ assert isinstance(r.content, bytes)
243
+ assert len(r.content) > 0
244
+ except Exception:
245
+ raise NotImplementedError("network not available")
246
+
247
+
248
+ # ---------------------------------------------------------------------------
249
+ # 15. Status codes
250
+ # ---------------------------------------------------------------------------
251
+ def status_codes():
252
+ from curl_cffi.requests import Session
253
+ with Session(impersonate="chrome") as s:
254
+ try:
255
+ r = s.get("https://httpbin.org/status/404")
256
+ assert r.status_code == 404
257
+ except Exception:
258
+ raise NotImplementedError("network not available")
259
+
260
+
261
+ # ---------------------------------------------------------------------------
262
+ # 16. Redirects
263
+ # ---------------------------------------------------------------------------
264
+ def redirect_handling():
265
+ from curl_cffi.requests import Session
266
+ with Session(impersonate="chrome") as s:
267
+ try:
268
+ r = s.get("https://httpbin.org/redirect/1", allow_redirects=True)
269
+ assert r.status_code == 200
270
+ except Exception:
271
+ raise NotImplementedError("network not available")
272
+
273
+
274
+ # ---------------------------------------------------------------------------
275
+ # 17. Impersonation list
276
+ # ---------------------------------------------------------------------------
277
+ def impersonate_list():
278
+ from curl_cffi import CurlHttpVersion
279
+ assert CurlHttpVersion.V1_1
280
+ assert CurlHttpVersion.V2_0
281
+
282
+
283
+ # ---------------------------------------------------------------------------
284
+ # 18. CurlError
285
+ # ---------------------------------------------------------------------------
286
+ def curl_error_importable():
287
+ from curl_cffi.requests.errors import CurlError
288
+ assert issubclass(CurlError, Exception)
289
+
290
+
291
+ # ---------------------------------------------------------------------------
292
+ # 19. BYTES response
293
+ # ---------------------------------------------------------------------------
294
+ def response_bytes():
295
+ from curl_cffi.requests import Session
296
+ with Session(impersonate="chrome") as s:
297
+ try:
298
+ r = s.get("https://httpbin.org/bytes/100")
299
+ assert isinstance(r.content, bytes)
300
+ assert len(r.content) == 100
301
+ except Exception:
302
+ raise NotImplementedError("network not available")
303
+
304
+
305
+ # ---------------------------------------------------------------------------
306
+ # 20. impersonate with version
307
+ # ---------------------------------------------------------------------------
308
+ def impersonate_versioned():
309
+ from curl_cffi.requests import Session
310
+ s = Session(impersonate="chrome120")
311
+ assert s is not None
312
+ s.close()
313
+
314
+
315
+ # ---------------------------------------------------------------------------
316
+ def main():
317
+ quick = "--quick" in sys.argv
318
+
319
+ section("1. curl_cffi import / version")
320
+ test("import curl_cffi", import_curl_cffi)
321
+ test("import curl_cffi.requests", import_requests)
322
+
323
+ section("2. Session")
324
+ test("session create + close", session_create)
325
+ test("session context manager", session_context_manager)
326
+
327
+ section("3. Impersonate profiles")
328
+ test("impersonate chrome", impersonate_chrome)
329
+ test("impersonate safari", impersonate_safari)
330
+ test("impersonate edge", impersonate_edge)
331
+ test("impersonate firefox", impersonate_firefox)
332
+
333
+ section("4. Request methods")
334
+ test("GET/POST/PUT/DELETE/PATCH/HEAD", request_methods)
335
+
336
+ section("5. Headers (network)")
337
+ test("custom headers", custom_headers)
338
+
339
+ section("6. Cookies (network)")
340
+ test("cookie handling", cookie_handling)
341
+
342
+ section("7. POST (network)")
343
+ test("POST form data", post_form)
344
+ test("POST JSON", post_json)
345
+
346
+ section("8. Response (network)")
347
+ test("response attributes", response_attributes)
348
+ test("response JSON", response_json)
349
+
350
+ section("9. Session options")
351
+ test("timeout setting", timeout_setting)
352
+ test("proxy setting", proxy_setting)
353
+ test("verify SSL False", verify_ssl)
354
+ test("HTTP/2 support", http2_support)
355
+
356
+ section("10. Binary / Status / Redirect (network)")
357
+ test("binary content", binary_content)
358
+ test("status codes 404", status_codes)
359
+ test("redirect handling", redirect_handling)
360
+
361
+ section("11. HTTP version / Errors")
362
+ test("CurlHttpVersion constants", impersonate_list)
363
+ test("CurlError importable", curl_error_importable)
364
+
365
+ section("12. Misc (network)")
366
+ test("BYTES response", response_bytes)
367
+ test("impersonate chrome120", impersonate_versioned)
368
+
369
+ print()
370
+ print("=" * 60)
371
+ print("SUMMARY")
372
+ print("=" * 60)
373
+ fails = 0
374
+ skips = 0
375
+ for name, status, why in RESULTS:
376
+ mark = " OK" if status == "PASS" else (" SKIP" if status == "SKIP" else "FAIL")
377
+ print("%s %s" % (mark, name))
378
+ if why:
379
+ print(" -> %s" % why)
380
+ if status == "FAIL":
381
+ fails += 1
382
+ elif status == "SKIP":
383
+ skips += 1
384
+ print()
385
+ passed = len(RESULTS) - fails - skips
386
+ print("passed=%d skipped=%d failed=%d" % (passed, skips, fails))
387
+ if fails:
388
+ print("RESULT: FAILED")
389
+ elif skips and not quick:
390
+ print("RESULT: PASSED (with informational skips)")
391
+ else:
392
+ print("RESULT: PASSED")
393
+ sys.exit(1 if fails else 0)
394
+
395
+
396
+ if __name__ == "__main__":
397
+ main()