dosage/tests/httpmocks.py

51 lines
1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-11-03 23:16:25 +00:00
# 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')
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'))