dosage/tests/httpmocks.py
Tobias Gruetzmacher 4d2fac1a9c Make it easier to write tests for comic modules
This also adds a simple test for the "turnoff" module to demonstrate
these features.
2019-12-01 22:36:49 +01:00

51 lines
1 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (C) 2017-2019 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
def _file(name):
return os.path.join(os.path.dirname(__file__), '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 page(url, pagename):
add(GET, url, _content(pagename))
def png(url):
add(GET, url, _img(), content_type='image/jpeg')
def jpeg(url):
add(GET, url, _img(), content_type='image/jpeg')
def xkcd():
page('https://xkcd.com/', 'xkcd-1899')
for num in (302, 303, 1898, 1899):
page('https://xkcd.com/%i/' % num, 'xkcd-%i' % num)
png(re.compile(r'https://imgs\.xkcd\.com/.*\.png'))