Skip to content

Commit

Permalink
Use redirected URL, closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
meyt committed Apr 22, 2021
1 parent 02b4d85 commit b022c3c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion linkpreview/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def link_preview(
if content is None:
try:
grabber = LinkGrabber()
content = grabber.get_content(url)
content, url = grabber.get_content(url)
except InvalidMimeTypeError:
content = ''

Expand Down
2 changes: 1 addition & 1 deletion linkpreview/grabber.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def get_content(self, url: str, headers: dict = None):

content += chunk

return content
return content, r.url
19 changes: 19 additions & 0 deletions tests/test_grabber.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ class FakeResponse(Response):
headers={"content-length": "100000"},
)
)
httpserver.expect_request("/redirected").respond_with_response(
FakeResponse(
mimetype="text/html",
response=b"done!",
)
)
redirected_url = "http://%s:%s/redirected" % (httpserver.host, httpserver.port)
httpserver.expect_request("/redirection").respond_with_response(
FakeResponse(
mimetype="text/html",
headers={"location": redirected_url},
status=301
)
)

# success
grabber = LinkGrabber(maxsize=100)
Expand Down Expand Up @@ -80,3 +94,8 @@ class FakeResponse(Response):
grabber = LinkGrabber()
with pytest.raises(exceptions.InvalidContentError):
grabber.get_content(httpserver.url_for("/badmime"))

# redirection
grabber = LinkGrabber()
content, url = grabber.get_content(httpserver.url_for("/redirection"))
assert url == redirected_url
22 changes: 22 additions & 0 deletions tests/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pytest_httpserver import HTTPServer

from werkzeug.wrappers.response import Response

from linkpreview import Link, link_preview
from linkpreview.preview import OpenGraph, TwitterCard, Schema, Generic

Expand Down Expand Up @@ -184,6 +186,18 @@ def test_link_preview(httpserver: HTTPServer):
'{}',
headers={"content-type": "application/json"},
)
httpserver.expect_request("/redirected").respond_with_data(
get_sample("generic/h1-img.html"),
headers={"content-type": "text/html"},
)
redirected_url = "http://%s:%s/redirected" % (httpserver.host, httpserver.port)
httpserver.expect_request("/redirection").respond_with_response(
Response(
mimetype="text/html",
headers={"location": redirected_url},
status=301
)
)

url = httpserver.url_for("/preview1")
preview = link_preview(url)
Expand Down Expand Up @@ -235,3 +249,11 @@ def test_link_preview(httpserver: HTTPServer):
assert preview.image is None
assert preview.absolute_image is None
assert preview.force_title == "Preview 3"

url = httpserver.url_for("/redirection")
preview = link_preview(url)
assert preview.link.url == redirected_url
assert preview.title == "This title is from the first h1 tag."
assert preview.description is None
assert preview.image == "http://localhost:8000/img/heck.jpg"
assert preview.absolute_image == "http://localhost:8000/img/heck.jpg"

0 comments on commit b022c3c

Please sign in to comment.