def test_search_on_github_cache_terraform_releases_200( tmp_working_dir, terraform_releases_html_after_v0_13_0, # noqa: F811 ): # noqa: D103 with mock.patch("io.BytesIO", AutoclosingBytesIO): with pook.use(): repo = "hashicorp/terraform" releases_url = "https://github.com/{}/releases?after=v0.13.0".format(repo) # A volatile mock that can only be invoked once pook.get( releases_url, reply=200, response_type="text/plain", response_body=terraform_releases_html_after_v0_13_0, response_headers={ "Status": "200 OK", "ETag": 'W/"df0474ebd25f223a95926ba58e11e77b"', "Cache-Control": "max-age=0, private, must-revalidate", "Date": formatdate(usegmt=True), }, times=1, ) patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?" patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14") assert patch == "14" patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "") assert patch == "19" assert pook.isdone() assert not pook.pending_mocks() assert not pook.unmatched_requests()
def test_search_on_github_cache_terraform_releases_does_not_cache_error_429( tmp_working_dir, terraform_releases_html_after_v0_13_0, # noqa: F811 ): # noqa: D103 with mock.patch("io.BytesIO", AutoclosingBytesIO): with pook.use(): repo = "hashicorp/terraform" releases_url = "https://github.com/{}/releases?after=v0.13.0".format( repo) # volatile mocks that can only be invoked once each pook.get( releases_url, reply=429, response_headers={ "Status": "429 Too Many Requests", "Date": formatdate(usegmt=True), "Retry-After": "120" }, times=1, ) pook.get( releases_url, reply=200, response_type="text/plain", response_body=terraform_releases_html_after_v0_13_0, response_headers={ "Status": "200 OK", "ETag": 'W/"df0474ebd25f223a95926ba58e11e77b"', "Cache-Control": "max-age=0, private, must-revalidate", "Date": formatdate(usegmt=True), }, times=1, ) patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?" # pook does not implement urllib3's retry logic so this first request will always return None during tests patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14") assert patch is None patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14") assert patch == "14" patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "") assert patch == "19" assert pook.isdone() assert not pook.pending_mocks() assert not pook.unmatched_requests()
def run(): pook.get('httpbin.org/ip', reply=403, response_headers={'pepe': 'lopez'}, response_json={'error': 'not found'}) res = requests.get('http://httpbin.org/ip') print('Status:', res.status_code) print('Headers:', res.headers) print('Body:', res.json()) print('Is done:', pook.isdone()) print('Pending mocks:', pook.pending_mocks()) print('Unmatched requests:', pook.unmatched_requests())
async def run(): pook.get('httpbin.org/ip', reply=403, response_headers={'pepe': 'lopez'}, response_json={'error': 'not found'}) async with aiohttp.ClientSession(loop=loop) as session: async with session.get('http://httpbin.org/ip') as res: print('Status:', res.status) print('Headers:', res.headers) print('Body:', await res.text()) print('Is done:', pook.isdone()) print('Pending mocks:', pook.pending_mocks()) print('Unmatched requests:', pook.unmatched_requests())
import json import pook import requests # Enable mock engine pook.on() (pook.post('httpbin.org/post') .json({'foo': 'bar'}) .type('json') .header('Client', 'requests') .reply(204) .headers({'server': 'pook'}) .json({'error': 'simulated'})) res = requests.post('http://httpbin.org/post', data=json.dumps({'foo': 'bar'}), headers={'Client': 'requests', 'Content-Type': 'application/json'}) print('Status:', res.status_code) print('Body:', res.json()) print('Is done:', pook.isdone()) print('Pending mocks:', pook.pending_mocks())