diff --git a/dosagelib/plugins/x.py b/dosagelib/plugins/x.py index 4e8faf22d..df46e30d5 100644 --- a/dosagelib/plugins/x.py +++ b/dosagelib/plugins/x.py @@ -11,7 +11,7 @@ from ..helpers import bounceStarter class Xkcd(_ParserScraper): name = 'xkcd' - url = 'http://xkcd.com/' + url = 'https://xkcd.com/' starter = bounceStarter stripUrl = url + '%s/' firstStripUrl = stripUrl % '1' diff --git a/tests/httpmocks.py b/tests/httpmocks.py new file mode 100644 index 000000000..e91c82a03 --- /dev/null +++ b/tests/httpmocks.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2017 Tobias Gruetzmacher + +from __future__ import absolute_import, division, print_function + +import gzip +import os.path +import re + +try: + from functools import lru_cache +except ImportError: + from backports.functools_lru_cache import lru_cache + +from responses import add, GET, POST + + +_basedir = os.path.dirname(__file__) + + +def _file(name): + return os.path.join(_basedir, 'responses', name) + + +@lru_cache() +def _content(name): + with gzip.open(_file(name + '.html.gz'), 'r') as f: + return f.read() + + +@lru_cache() +def _img(): + with open(_file('empty.png'), 'rb') as f: + return f.read() + + +def xkcd(): + add(GET, 'https://xkcd.com/', _content('xkcd-1899')) + for page in (302, 303, 1898, 1899): + add(GET, 'https://xkcd.com/%i/' % page, _content('xkcd-%i' % page)) + add(GET, re.compile(r'https://imgs\.xkcd\.com/.*\.png'), _img(), content_type='image/png') + + +def bloomingfaeries(): + add(GET, 'http://www.bloomingfaeries.com/', _content('bf-home')) + add(GET, 'http://www.bloomingfaeries.com/comic/public/bloomin-faeries-405/', _content('bf-405')) + + add(GET, re.compile(r'http://www\.bloomingfaeries\.com/.*\.jpg'), _img(), content_type='image/jpeg') + + +def vote(): + add(POST, 'http://gaecounter.appspot.com/count/', 'no') diff --git a/tests/responses/bf-405.html.gz b/tests/responses/bf-405.html.gz new file mode 100644 index 000000000..b7e928b76 Binary files /dev/null and b/tests/responses/bf-405.html.gz differ diff --git a/tests/responses/bf-home.html.gz b/tests/responses/bf-home.html.gz new file mode 100644 index 000000000..1f927dec3 Binary files /dev/null and b/tests/responses/bf-home.html.gz differ diff --git a/tests/responses/empty.png b/tests/responses/empty.png new file mode 100644 index 000000000..91a99b94e Binary files /dev/null and b/tests/responses/empty.png differ diff --git a/tests/responses/xkcd-1898.html.gz b/tests/responses/xkcd-1898.html.gz new file mode 100644 index 000000000..a26acc5cd Binary files /dev/null and b/tests/responses/xkcd-1898.html.gz differ diff --git a/tests/responses/xkcd-1899.html.gz b/tests/responses/xkcd-1899.html.gz new file mode 100644 index 000000000..4ada9f36e Binary files /dev/null and b/tests/responses/xkcd-1899.html.gz differ diff --git a/tests/responses/xkcd-302.html.gz b/tests/responses/xkcd-302.html.gz new file mode 100644 index 000000000..ec4bdceee Binary files /dev/null and b/tests/responses/xkcd-302.html.gz differ diff --git a/tests/responses/xkcd-303.html.gz b/tests/responses/xkcd-303.html.gz new file mode 100644 index 000000000..fea0974d4 Binary files /dev/null and b/tests/responses/xkcd-303.html.gz differ diff --git a/tests/test_dosage.py b/tests/test_dosage.py index 88e244ff7..50aff45ee 100644 --- a/tests/test_dosage.py +++ b/tests/test_dosage.py @@ -6,20 +6,25 @@ from __future__ import absolute_import, division, print_function import pytest -import sys +import responses import dosagelib.cmd +import httpmocks + def cmd(*options): """'Fake' run dosage with given options.""" return dosagelib.cmd.main(('--allow-multiple',) + options) + def cmd_ok(*options): assert cmd(*options) == 0 + def cmd_err(*options): assert cmd(*options) == 1 + class TestDosage(object): """Test the dosage commandline client.""" @@ -48,14 +53,20 @@ class TestDosage(object): def test_multiple_comics_match(self): cmd_err('Garfield') + @responses.activate def test_fetch_html_and_rss_json(self, tmpdir): + httpmocks.xkcd() cmd_ok("-n", "2", "-v", "-b", str(tmpdir), "-o", "html", "-o", "rss", - "-o", "json", "xkcd") + "-o", "json", "xkcd") + @responses.activate def test_fetch_html_and_rss_2(self, tmpdir): + httpmocks.bloomingfaeries() cmd_ok("--numstrips", "2", "--baseurl", "bla", "--basepath", - str(tmpdir), "--output", "rss", "--output", "html", "--adult", - "BloomingFaeries") + str(tmpdir), "--output", "rss", "--output", "html", "--adult", + "BloomingFaeries") + @responses.activate def test_fetch_indexed(self, tmpdir): + httpmocks.xkcd() cmd_ok("-n", "2", "-v", "-b", str(tmpdir), "xkcd:303") diff --git a/tests/test_vote.py b/tests/test_vote.py index 8dac29aec..801897db3 100644 --- a/tests/test_vote.py +++ b/tests/test_vote.py @@ -1,11 +1,14 @@ # -*- coding: utf-8 -*- # Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs # Copyright (C) 2012-2014 Bastian Kleineidam -# Copyright (C) 2015-2016 Tobias Gruetzmacher +# Copyright (C) 2015-2017 Tobias Gruetzmacher from __future__ import absolute_import, division, print_function +import responses + from dosagelib import scraper +import httpmocks class ATestScraper(scraper._BasicScraper): @@ -14,6 +17,8 @@ class ATestScraper(scraper._BasicScraper): class TestVote(object): + @responses.activate def test_vote(self): + httpmocks.vote() answer = ATestScraper('Test_Test').vote() assert answer in ('counted', 'no'), 'invalid answer %r' % answer