From 0f868d77f1b56e8400040e6dd9e50b855c65267f Mon Sep 17 00:00:00 2001 From: HenrikOssipoff Date: Sat, 13 Jun 2015 12:08:09 +0200 Subject: [PATCH] Add tests to fully cover initial implementation --- setup.py | 2 +- tests/test_opengraph.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a6e24a6..be64a3a 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ __versionstr__ = '.'.join([str(v) for v in VERSION]) install_requires = ['beautifulsoup4>=4.3', 'requests>=2.7'] -test_require = install_requires + ['coverage>=3.7', 'nose>=1.3'] +test_require = install_requires + ['coverage>=3.7', 'nose>=1.3', 'responses'] setup( name='python-opengraph', diff --git a/tests/test_opengraph.py b/tests/test_opengraph.py index 77e64f7..14cf840 100644 --- a/tests/test_opengraph.py +++ b/tests/test_opengraph.py @@ -4,6 +4,10 @@ from unittest import TestCase from opengraph import OpenGraph +from threading import local +import responses + +data = local() class TestOpenGraph(TestCase): @@ -14,6 +18,15 @@ def setUp(self): # NOQA """ + @responses.activate + def test_loading_from_url(self): + url = 'http://foo.bar.com/' + responses.add( + responses.GET, url, body=self.test_document, + status=200, content_type='text/html') + og = OpenGraph(url=url) + self.assertEqual(og.title, 'Test title') + def test_get_attr(self): og = OpenGraph(html=self.test_document) self.assertEqual(og.title, 'Test title') @@ -28,3 +41,21 @@ def test_str_repr(self): og = OpenGraph(html=self.test_document) text_of_data = og.__data__.__str__() self.assertEqual(str(og), text_of_data) + + @responses.activate + def test_loading_from_url(self): + def http_callback(request): + # Ugly, but using thread locals in order to capture the request + # headers in the callback, to assert that it's being set correctly + data.headers = request.headers + return (200, {'content-type': 'text/html'}, self.test_document) + + url = 'http://foo.bar.com/' + useragent = 'python-opengraph/0.0' + responses.add_callback( + responses.GET, url, callback=http_callback, + content_type='text/html') + og = OpenGraph(url=url, useragent=useragent) + headers = data.headers + self.assertEqual(og.title, 'Test title') + self.assertEqual(headers['user-agent'], useragent)