dosage/tests/httpmocks.py

45 lines
936 B
Python
Raw Normal View History

# SPDX-License-Identifier: MIT
2019-11-03 23:16:25 +00:00
# Copyright (C) 2017-2019 Tobias Gruetzmacher
import gzip
import os.path
import re
from functools 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()
2020-01-04 14:51:01 +00:00
def _img(name):
with open(_file(name + '.png'), 'rb') as f:
return f.read()
def page(url, pagename):
add(GET, url, content(pagename))
2020-01-04 14:51:01 +00:00
def png(url, name='empty'):
add(GET, url, _img(name), content_type='image/jpeg')
2020-01-04 14:51:01 +00:00
def jpeg(url, name='empty'):
add(GET, url, _img(name), content_type='image/jpeg')
2019-11-03 23:16:25 +00:00
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'))