url_preview: Discard url in oembed if server returns invalid json.

This fixes the scenario where we'd get errors in the
FetchLinksEmbedData queue processor if oembed got invalid json from the
URL.
This commit is contained in:
Mateusz Mandera 2020-04-11 13:24:06 +02:00 committed by Tim Abbott
parent b8552e9524
commit 770086f983
2 changed files with 12 additions and 1 deletions

View File

@ -1,12 +1,13 @@
from typing import Optional, Dict, Any
from pyoembed import oEmbed, PyOembedException
import json
def get_oembed_data(url: str,
maxwidth: Optional[int]=640,
maxheight: Optional[int]=480) -> Optional[Dict[str, Any]]:
try:
data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight)
except PyOembedException:
except (PyOembedException, json.decoder.JSONDecodeError):
return None
oembed_resource_type = data.get('type', '')

View File

@ -114,6 +114,16 @@ class OembedTestCase(ZulipTestCase):
data = get_oembed_data(url)
self.assertIsNone(data)
@mock.patch('pyoembed.requests.get')
def test_invalid_json_in_response(self, get: Any) -> None:
get.return_value = response = mock.Mock()
response.headers = {'content-type': 'application/json'}
response.ok = True
response.text = '{invalid json}'
url = 'http://instagram.com/p/BLtI2WdAymy'
data = get_oembed_data(url)
self.assertIsNone(data)
def test_oembed_html(self) -> None:
html = '<iframe src="//www.instagram.com/embed.js"></iframe>'
stripped_html = strip_cdata(html)