def test_multiple_http_mocks_independently_served(): """ Ask for two http mocks and set up different presets. Make calls against each. We should get the correct responses back. """ http_mock_1 = HTTPMock('localhost', 8000) http_mock_2 = HTTPMock('localhost', 8000) fake_client_1 = get_fake_client(http_mock_1) fake_client_2 = get_fake_client(http_mock_2) http_mock_1.reset() http_mock_2.reset() http_mock_1.when('GET /test_mock1_get').reply(b'You tested a get', 201, times=FOREVER) http_mock_2.when('GET /test_mock2_get').reply(b'You tested a get', 200, times=FOREVER) assert_equals(201, fake_client_1.get(url="/test_mock1_get")[0].status) assert_equals(200, fake_client_2.get(url="/test_mock2_get")[0].status) # Check that they both 404 if used against the other url. assert_equals(404, fake_client_1.get(url="/test_mock2_get")[0].status) assert_equals(404, fake_client_2.get(url="/test_mock1_get")[0].status)
class MockServer: def __init__(self, host='localhost', port=8000): self._address = host, port self.server = HTTPMock(host, port) def server_address(self): return self._address def setup_normal(self): self.server.reset() # FIXME: Endpoint is a placeholder! self.server.when("GET /important_data").reply('OK', status=200, times=FOREVER) def setup_error(self): self.server.reset() # FIXME: Endpoint is a placeholder! self.server.when("GET /important_data").reply('ERROR', status=500, times=FOREVER)
return resp def post_with_cassette(self, path_to_cassette, url_to_open, body): with self.vcr.use_cassette(path_to_cassette): resp = requests.post(url_to_open, body) return resp def patch_with_cassette(self, path_to_cassette, url_to_open, body): with self.vcr.use_cassette(path_to_cassette): resp = requests.patch(url_to_open, body) return resp mock = HTTPMock('localhost', 8888, timeout=20, name="zingfit_api") mock.reset() mock.when("GET /important_data$").reply(bytes('{"account": "10000", "outstanding": "10.00"}'), status=200, times=FOREVER) mock.when("GET /important_data2$").reply(bytes('ERROR'), status=500, times=FOREVER) mock.when("GET /important_data3$").reply(bytes('OK'), status=200, times=FOREVER) mock.when("POST /important_data4$", body='{"account": "10000", "outstanding": "10.00"}').reply(bytes('{"id": "10"}'), status=200, times=FOREVER) start_time = time.time() vcr_new = VCR() # res = open_with_cassette(vcr_new, 'iana.json', 'http://www.iana.org/domains/reserved') # assert 'Example domains' in res print("--- %s seconds ---" % (time.time() - start_time)) start_time = time.time() res = vcr_new.get_with_cassette('mock.json', 'http://localhost:8888/mockhttp/zingfit_api/important_data')