def mock_images(requests_mock: Mocker): """ Loads the mock images from tests/fixtures/<image> """ expectations = {} url = 'https://www.fbi.gov/wanted/topten/yaser-abdel-said/@@images/image/preview' filename = (os.path.join(os.path.dirname(__file__), 'fixtures', '1ce559cc3fa5dfd70d914a3f083b357c474fdf63.jpg')) with open(filename, 'rb') as file: jpeg = file.read() requests_mock.head(url, headers={ 'Content-Type': 'image/jpeg', 'Content-Length': '23930' }) requests_mock.get(url, content=jpeg, headers={ 'Content-Type': 'image/jpeg', 'Content-Length': '23930' }) expectations[url] = {} expectations[url]['content'] = jpeg return expectations
def expectations(requests_mock: Mocker): """ Loads the mock data from tests/fixtures/<url> """ expectations = { 'https://www.fbi.gov/wanted/topten': { 'content': None, # lazy loaded below 'img_links': [ 'https://www.fbi.gov/wanted/topten/yaser-abdel-said/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/alexis-flores/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/eugene-palmer/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/santiago-mederos/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/rafael-caro-quintero/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/robert-william-fisher/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/bhadreshkumar-chetanbhai-patel/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/alejandro-castillo/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/arnoldo-jimenez/@@images/image/preview', 'https://www.fbi.gov/wanted/topten/jason-derek-brown/@@images/image/preview' ], 'a_tags': [ 'https://www.fbi.gov/wanted/ecap', 'https://www.fbi.gov/wanted/vicap', 'https://www.fbi.gov/wanted/ecap', 'https://www.fbi.gov/wanted/vicap' ], }, 'https://www.interpol.int/en/How-we-work/Notices/View-Red-Notices': { 'content': None, # lazy loaded below 'img_links': [ 'https://www.interpol.int/bundles/interpolfront/images/logo.png', 'https://www.interpol.int/bundles/interpolfront/images/logo-text-only.png', 'https://www.interpol.int/var/interpol/storage/images/1/1/1/6/76111-12-eng-GB/RedNoticeEnLR.jpg', 'https://ws-public.interpol.int/notices/v1/red/2020-26948/images/61775574', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-26190/images/61772589', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-26878/images/61775241', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-26860/images/61775167', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-26825/images/61775048', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-26239/images/61772781', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2015-42824/images/59832553', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-26247/images/61772803', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-26214/images/61772666', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2017-130054/images/61493233', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-25875/images/61771543', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-25867/images/61771519', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-25861/images/61771500', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-25587/images/61770497', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-24211/images/61765539', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-24051/images/61765027', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-24000/images/61764808', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-24062/images/61765096', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-23879/images/61764455', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://ws-public.interpol.int/notices/v1/red/2020-23364/images/61763068', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://www.interpol.int/bundles/interpolfront/images/photo-not-available.png', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://www.interpol.int/bundles/interpolfront/images/rednotice_big.png', 'https://www.interpol.int/bundles/interpolfront/images/photo-not-available.png', 'https://www.interpol.int/bundles/interpolfront/images/rednotice.png', 'https://www.interpol.int/bundles/interpolfront/images/logo-blanc.png' ], 'a_tags': ['https://www.interpol.int/en/Crimes/Cybercrime'], }, } # mock page content headers = {'Content-Type': 'text/html; charset=utf-8'} matcher = re.compile('.gov/|interpol.int/|.com/') for url in expectations.keys(): requests_mock.head(url, headers=headers) filename = (os.path.join(os.path.dirname(__file__), 'fixtures', quote(url, ''))) with open(filename, 'r') as file: content = file.read() assert len(content) > 0, "content mock must have data" expectations[url]['content'] = content.encode('utf-8') requests_mock.get(url, text=content, headers=headers) requests_mock.register_uri('HEAD', matcher, additional_matcher=match_request_url, headers=headers) requests_mock.register_uri('GET', matcher, text=content, additional_matcher=match_request_url, headers=headers) # TODO add test coverage for 429 rate limit status_code with custom matcher return expectations