forked from Instapaper/instaparser-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
481 lines (390 loc) · 18.6 KB
/
test_client.py
File metadata and controls
481 lines (390 loc) · 18.6 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import io
import json
from unittest.mock import Mock
from urllib.error import HTTPError, URLError
import pytest
from instaparser import InstaparserClient
from instaparser.client import _encode_multipart_formdata
from instaparser.exceptions import (
InstaparserAPIError,
InstaparserAuthenticationError,
InstaparserRateLimitError,
InstaparserValidationError,
)
from instaparser.models import Article
API_KEY = "test-api-key-12345"
BASE_URL = "https://api.test.instaparser.com"
ARTICLE_DATA = {
"url": "https://example.com/article",
"title": "Test Article Title",
"site_name": "Example Site",
"author": "John Doe",
"date": 1609459200,
"description": "This is a test article description",
"thumbnail": "https://example.com/thumb.jpg",
"words": 500,
"is_rtl": False,
"images": ["https://example.com/image1.jpg"],
"videos": [],
"html": "<p>This is the article body in HTML format.</p>",
}
SUMMARY_DATA = {
"key_sentences": [
"This is the first key sentence.",
"This is the second key sentence.",
"This is the third key sentence.",
],
"overview": "This is a comprehensive overview of the article content.",
}
PDF_DATA = {
"url": "https://example.com/document.pdf",
"title": "Test PDF Document",
"site_name": "Example Site",
"author": "Jane Smith",
"date": 1609459200,
"description": "This is a test PDF document",
"thumbnail": None,
"words": 1000,
"is_rtl": False,
"images": [],
"videos": [],
"html": "<p>This is the PDF content in HTML format.</p>",
}
ERROR_CODES = [
(400, InstaparserValidationError),
(401, InstaparserAuthenticationError),
(403, InstaparserAPIError),
(409, InstaparserAPIError),
(429, InstaparserRateLimitError),
(500, InstaparserAPIError),
]
@pytest.fixture
def client():
return InstaparserClient(api_key=API_KEY, base_url=BASE_URL)
@pytest.fixture
def mock_request(client, monkeypatch):
mock = Mock()
monkeypatch.setattr(client, "_request", mock)
return mock
def make_response(*, json_data=None, text=None):
"""Mock a successful HTTPResponse."""
response = Mock()
response.status = 200
if json_data is not None:
response.read.return_value = json.dumps(json_data).encode()
elif text is not None:
response.read.return_value = text.encode()
else:
response.read.return_value = b"{}"
return response
def make_streaming_response(lines):
"""Mock a streaming HTTPResponse that yields *lines*."""
response = Mock()
response.status = 200
response.__iter__ = Mock(return_value=iter(lines))
return response
def make_error(status_code, *, json_data=None, text=None):
"""Create an HTTPError with a readable body."""
if json_data is not None:
body = json.dumps(json_data).encode()
elif text is not None:
body = text.encode()
else:
body = b"{}"
return HTTPError("https://api.example.com", status_code, "", None, io.BytesIO(body))
class TestClientInit:
def test_base_url(self):
client = InstaparserClient(api_key=API_KEY, base_url=BASE_URL)
assert client.base_url == BASE_URL
def test_headers(self):
client = InstaparserClient(api_key=API_KEY)
assert client.headers["Authorization"] == f"Bearer {API_KEY}"
class TestReadJson:
def test_valid_json(self, client):
assert client._read_json(make_response(json_data={"key": "value"})) == {"key": "value"}
def test_non_json(self, client):
assert client._read_json(make_response(text="Plain text")) == {"raw": "Plain text"}
class TestArticle:
def test_basic_parse(self, client, mock_request):
mock_request.return_value = make_response(json_data=ARTICLE_DATA)
article = client.article(url="https://example.com/article")
assert article.title == "Test Article Title"
assert article.url == "https://example.com/article"
assert article.body == "<p>This is the article body in HTML format.</p>"
_method, _path = mock_request.call_args[0]
payload = mock_request.call_args[1]["json_data"]
assert _method == "POST"
assert _path == "/api/1/article"
assert payload["url"] == "https://example.com/article"
assert payload["output"] == "html"
assert "use_cache" not in payload
def test_text_output(self, client, mock_request):
data = {**ARTICLE_DATA, "html": None, "text": "Plain text body."}
mock_request.return_value = make_response(json_data=data)
article = client.article(url="u", output="text")
assert article.body == "Plain text body."
assert mock_request.call_args[1]["json_data"]["output"] == "text"
def test_markdown_output(self, client, mock_request):
mock_request.return_value = make_response(json_data={"url": "u", "markdown": "# MD"})
article = client.article(url="u", output="markdown")
assert article.markdown == "# MD"
assert article.body == "# MD"
def test_with_content(self, client, mock_request):
mock_request.return_value = make_response(json_data=ARTICLE_DATA)
client.article(url="u", content="<html>hi</html>")
assert mock_request.call_args[1]["json_data"]["content"] == "<html>hi</html>"
def test_use_cache_false(self, client, mock_request):
mock_request.return_value = make_response(json_data=ARTICLE_DATA)
client.article(url="u", use_cache=False)
assert mock_request.call_args[1]["json_data"]["use_cache"] is False
def test_invalid_output(self, client):
with pytest.raises(InstaparserValidationError, match="output must be"):
client.article(url="u", output="invalid")
def test_malformed_json_response(self, client, mock_request):
mock_request.return_value = make_response(text="Not valid JSON {")
article = client.article(url="u")
assert isinstance(article, Article)
class TestSummary:
def test_basic_summary(self, client, mock_request):
mock_request.return_value = make_response(json_data=SUMMARY_DATA)
summary = client.summary(url="https://example.com/article")
assert len(summary.key_sentences) == 3
assert summary.overview == "This is a comprehensive overview of the article content."
assert mock_request.call_args[1]["json_data"]["stream"] is False
def test_with_content(self, client, mock_request):
mock_request.return_value = make_response(json_data=SUMMARY_DATA)
client.summary(url="u", content="<html>hi</html>")
assert mock_request.call_args[1]["json_data"]["content"] == "<html>hi</html>"
def test_use_cache_false(self, client, mock_request):
mock_request.return_value = make_response(json_data=SUMMARY_DATA)
client.summary(url="u", use_cache=False)
assert mock_request.call_args[1]["json_data"]["use_cache"] is False
def test_empty_response(self, client, mock_request):
mock_request.return_value = make_response(json_data={})
summary = client.summary(url="u")
assert summary.key_sentences == []
assert summary.overview == ""
def test_streaming_callback(self, client, mock_request):
mock_request.return_value = make_streaming_response(
[
b'key_sentences: ["Sentence 1", "Sentence 2"]\r\n',
b"delta: This is\r\n",
b"delta: a streaming\r\n",
b"delta: overview.\r\n",
]
)
received = []
summary = client.summary(url="u", stream_callback=received.append)
assert len(received) == 4
assert received[0] == 'key_sentences: ["Sentence 1", "Sentence 2"]'
assert summary.key_sentences == ["Sentence 1", "Sentence 2"]
assert summary.overview == "This is a streaming overview."
assert mock_request.call_args[1]["json_data"]["stream"] is True
def test_streaming_empty_lines_filtered(self, client, mock_request):
mock_request.return_value = make_streaming_response(
[
b"key_sentences: []\r\n",
b"\r\n",
b"delta: Content\r\n",
b"\r\n",
]
)
summary = client.summary(url="u", stream_callback=lambda _: None)
assert summary.overview == "Content"
def test_streaming_malformed_key_sentences(self, client, mock_request):
mock_request.return_value = make_streaming_response(
[
b"key_sentences: not valid json\r\n",
b"delta: Content\r\n",
]
)
with pytest.raises(InstaparserAPIError) as exc_info:
client.summary(url="u", stream_callback=lambda _: None)
assert exc_info.value.status_code == 412
assert "Unable to generate key sentences" in str(exc_info.value)
def test_streaming_error_response(self, client, mock_request):
mock_request.side_effect = make_error(401, json_data={"reason": "Invalid API key"})
with pytest.raises(InstaparserAuthenticationError):
client.summary(url="u", stream_callback=lambda _: None)
class TestPDF:
def test_from_url(self, client, mock_request):
mock_request.return_value = make_response(json_data=PDF_DATA)
pdf = client.pdf(url="https://example.com/document.pdf")
assert pdf.title == "Test PDF Document"
assert pdf.is_rtl is False
assert pdf.videos == []
_method, _path = mock_request.call_args[0]
assert _method == "GET"
assert _path == "/api/1/pdf"
assert mock_request.call_args[1]["params"]["url"] == "https://example.com/document.pdf"
def test_from_file(self, client, mock_request):
pdf_file = Mock()
mock_request.return_value = make_response(json_data=PDF_DATA)
pdf = client.pdf(file=pdf_file)
assert pdf.title == "Test PDF Document"
assert mock_request.call_args[0][0] == "POST"
assert mock_request.call_args[1]["multipart_files"]["file"] is pdf_file
def test_from_bytes(self, client, mock_request):
mock_request.return_value = make_response(json_data=PDF_DATA)
client.pdf(file=b"PDF content")
assert mock_request.call_args[1]["multipart_files"]["file"] == b"PDF content"
def test_file_and_url(self, client, mock_request):
"""When both file and url are given, POST with url in form fields."""
pdf_file = Mock()
mock_request.return_value = make_response(json_data=PDF_DATA)
client.pdf(url="https://example.com/document.pdf", file=pdf_file)
assert mock_request.call_args[1]["multipart_fields"]["url"] == "https://example.com/document.pdf"
@pytest.mark.parametrize("output", ["text", "markdown"])
def test_output_formats_url(self, client, mock_request, output):
data = {**PDF_DATA, "html": None, output: f"content in {output}"}
mock_request.return_value = make_response(json_data=data)
client.pdf(url="https://example.com/document.pdf", output=output)
assert mock_request.call_args[1]["params"]["output"] == output
def test_use_cache_false_url(self, client, mock_request):
mock_request.return_value = make_response(json_data=PDF_DATA)
client.pdf(url="https://example.com/document.pdf", use_cache=False)
assert mock_request.call_args[1]["params"]["use_cache"] == "false"
def test_use_cache_false_file(self, client, mock_request):
mock_request.return_value = make_response(json_data=PDF_DATA)
client.pdf(file=Mock(), use_cache=False)
assert mock_request.call_args[1]["multipart_fields"]["use_cache"] == "false"
def test_invalid_output(self, client):
with pytest.raises(InstaparserValidationError, match="output must be"):
client.pdf(url="u", output="invalid")
def test_no_url_or_file(self, client):
with pytest.raises(InstaparserValidationError, match="Either 'url' or 'file'"):
client.pdf()
class TestTransportErrors:
def test_url_error(self, client, mock_request):
mock_request.side_effect = URLError("Connection failed")
with pytest.raises(URLError):
client.article(url="u")
def test_timeout_error(self, client, mock_request):
mock_request.side_effect = TimeoutError("timed out")
with pytest.raises(TimeoutError):
client.article(url="u")
class TestURLConstruction:
@pytest.mark.parametrize("base", ["https://api.test.com", "https://api.test.com/"])
def test_base_url_joining(self, base, monkeypatch):
client = InstaparserClient(api_key=API_KEY, base_url=base)
mock_urlopen = Mock(return_value=make_response(json_data={}))
monkeypatch.setattr("instaparser.client.urlopen", mock_urlopen)
client.article(url="u")
req = mock_urlopen.call_args[0][0]
assert req.full_url.startswith("https://api.test.com/api/1/article")
class TestDeprecatedAliases:
def test_article_alias(self, client, mock_request):
mock_request.return_value = make_response(json_data=ARTICLE_DATA)
with pytest.warns(DeprecationWarning, match="client.Article.*deprecated.*client.article"):
article = client.Article(url="https://example.com/article")
assert article.title == "Test Article Title"
def test_pdf_alias(self, client, mock_request):
mock_request.return_value = make_response(json_data=PDF_DATA)
with pytest.warns(DeprecationWarning, match="client.PDF.*deprecated.*client.pdf"):
pdf = client.PDF(url="https://example.com/document.pdf")
assert pdf.title == "Test PDF Document"
def test_summary_alias(self, client, mock_request):
mock_request.return_value = make_response(json_data=SUMMARY_DATA)
with pytest.warns(DeprecationWarning, match="client.Summary.*deprecated.*client.summary"):
summary = client.Summary(url="https://example.com/article")
assert summary.overview == SUMMARY_DATA["overview"]
class TestMultipleClients:
def test_independent_instances(self):
c1 = InstaparserClient(api_key="key1", base_url="https://api1.com")
c2 = InstaparserClient(api_key="key2", base_url="https://api2.com")
assert c1.api_key != c2.api_key
assert c1.base_url != c2.base_url
def test_client_reuse(self, client, mock_request):
mock_request.return_value = make_response(json_data=ARTICLE_DATA)
a1 = client.article(url="u1")
a2 = client.article(url="u2")
assert mock_request.call_count == 2
assert a1.title == a2.title == "Test Article Title"
class TestErrorPropagation:
@pytest.mark.parametrize("status, exc_cls", ERROR_CODES)
def test_article_errors(self, client, mock_request, status, exc_cls):
mock_request.side_effect = make_error(status, json_data={"reason": f"Error {status}"})
with pytest.raises(exc_cls) as exc_info:
client.article(url="u")
assert exc_info.value.status_code == status
@pytest.mark.parametrize("status, exc_cls", ERROR_CODES)
def test_summary_errors(self, client, mock_request, status, exc_cls):
mock_request.side_effect = make_error(status, json_data={"reason": f"Error {status}"})
with pytest.raises(exc_cls):
client.summary(url="u")
@pytest.mark.parametrize("status, exc_cls", ERROR_CODES)
def test_pdf_errors(self, client, mock_request, status, exc_cls):
mock_request.side_effect = make_error(status, json_data={"reason": f"Error {status}"})
with pytest.raises(exc_cls):
client.pdf(url="u")
def test_error_without_reason_field(self, client, mock_request):
mock_request.side_effect = make_error(500, json_data={})
with pytest.raises(InstaparserAPIError, match="API request failed"):
client.article(url="u")
def test_error_plain_text_body(self, client, mock_request):
mock_request.side_effect = make_error(500, text="Error message")
with pytest.raises(InstaparserAPIError, match="Error message"):
client.article(url="u")
def test_error_empty_body(self, client, mock_request):
mock_request.side_effect = make_error(500, text="")
with pytest.raises(InstaparserAPIError) as exc_info:
client.article(url="u")
assert exc_info.value.status_code == 500
class TestOutputFormats:
@pytest.mark.parametrize(
"output, field, body_text",
[
("html", "html", "<p>HTML content</p>"),
("text", "text", "Text content"),
("markdown", "markdown", "# Markdown content"),
],
)
def test_article_output_formats(self, client, mock_request, output, field, body_text):
mock_request.return_value = make_response(json_data={"url": "u", field: body_text})
article = client.article(url="u", output=output)
assert article.body == body_text
@pytest.mark.parametrize(
"output, field, body_text",
[
("html", "html", "<p>PDF HTML</p>"),
("text", "text", "PDF Text"),
("markdown", "markdown", "# PDF markdown"),
],
)
def test_pdf_output_formats(self, client, mock_request, output, field, body_text):
mock_request.return_value = make_response(json_data={"url": "u", field: body_text})
pdf = client.pdf(url="u", output=output)
assert pdf.body == body_text
class TestEncodeMultipartFormdata:
def test_fields_only(self):
body, content_type = _encode_multipart_formdata({"key": "value"}, {})
assert content_type.startswith("multipart/form-data; boundary=")
assert b'name="key"' in body
assert b"value" in body
assert body.endswith(b"--\r\n")
def test_file_from_bytes(self):
body, content_type = _encode_multipart_formdata({}, {"file": b"PDF content"})
assert b'name="file"' in body
assert b'filename="upload"' in body
assert b"application/octet-stream" in body
assert b"PDF content" in body
def test_file_from_file_object(self):
f = io.BytesIO(b"file data")
body, _ = _encode_multipart_formdata({}, {"file": f})
assert b"file data" in body
def test_fields_and_files(self):
body, content_type = _encode_multipart_formdata(
{"output": "html", "use_cache": "false"},
{"file": b"data"},
)
boundary = content_type.split("boundary=")[1]
parts = body.split(f"--{boundary}".encode())
# parts: ['', field1, field2, file, '--\r\n']
assert len(parts) == 5
assert b'name="output"' in parts[1]
assert b'name="use_cache"' in parts[2]
assert b'name="file"' in parts[3]
def test_boundary_is_unique(self):
_, ct1 = _encode_multipart_formdata({}, {})
_, ct2 = _encode_multipart_formdata({}, {})
assert ct1 != ct2