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)
def get_mock(): mock = HTTPMock('127.0.0.1', 50000) mock.when('GET /blockchain/memberships/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')\ .reply(body=bytes(json.dumps(bma_memberships_empty_array), "utf-8"), times=FOREVER, headers={'Content-Type': 'application/json'}) return mock
def mock(server): # Create the mock server httpmock = HTTPMock("localhost", 8000) # Common responses for both tests httpmock.when("POST /login").reply( '"security-token"', headers={"Content-Type": "application/json"}, times=FOREVER) yield httpmock
class NapiprojektMock(object): DEFAULT_ADDRESS = 'napiserver' DEFAULT_PORT = 8000 def __init__(self, address=DEFAULT_ADDRESS, port=DEFAULT_PORT): self.address = address self.port = port self.http = HTTPMock(self.address, self.port) self.defaultHeaders = { 'Content-Type': 'text/xml; charset=UTF-8', } def getUrl(self): return self.http.pretend_url def getRequest(self, n=0): return self.http.get_request(n) def programPlainRequest(self, blob=None, status=None, times=1): body = None data = blob.getData() if blob else '' if not status: status = 200 if blob else 404 self.http.when('GET /unit_napisy/dl.php', body=body).reply(data, status=status, headers=self.defaultHeaders, times=times) def programXmlRequest(self, media, subtitles=None, cover=None, movieDetails=None, times=1): status = 200 # pretenders matches body from the beginning of the string - due to # that body filtering at the moment is out of question body = None self.http.when('POST /api/api-napiprojekt3.php', body=body).reply(xml_result.XmlResult( subtitles, cover, movieDetails).toString(), status=status, headers=self.defaultHeaders, times=times)
def test_multiple_mock_servers_only_see_their_presets_and_history(): first_mock = HTTPMock('localhost', 8000, timeout=30) second_mock = HTTPMock('localhost', 8000, timeout=30) first_mock_response_body = b"a 1st mock fake response" second_mock_response_body = b"a 2nd mock fake response" # Set up the two mocks to respond differently: # Set up first mock to respond with a 200 twice. for i in range(2): first_mock.when('POST /someplace').reply(first_mock_response_body, 200) second_mock.when('POST /someplace').reply(second_mock_response_body, 601) # create some fake clients that will post to the mock servers. first_fake_client = get_fake_client(first_mock) second_fake_client = get_fake_client(second_mock) # Make some requests using the clients # We alternate between the client calls, asserting that the responses match # those set up above. for i in range(2): post_body = "first_mock_{0}".format(i).encode() response = first_fake_client.post(url='/someplace', body=post_body) assert_response_equal(response, first_mock_response_body, 200) # Check that the historical values match those requested. request = first_mock.get_request(i) assert_equals(request.method, 'POST') assert_equals(request.url, '/someplace') assert_equals(request.body, post_body) for i in range(2): post_body = "second_mock_{0}".format(i).encode() response = second_fake_client.post(url='/someplace', body=post_body) assert_response_equal(response, second_mock_response_body, 601) # Check that the historical values match those requested. request = second_mock.get_request(i) assert_equals(request.method, 'POST') assert_equals(request.url, '/someplace') assert_equals(request.body, post_body)
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)
def test_check_single_model_access(tmpdir, test_data_path): mock = HTTPMock('localhost', 8000) mock.when('POST /login').reply( '"security-token"', headers={'Content-Type': 'application/json'}, times=FOREVER) mock.when('POST /access/list', body=".+\"test_user\".+", headers={ 'Authorization': 'Bearer security-token' }).reply('[true]', headers={'Content-Type': 'application/json'}, times=FOREVER) mock.when('POST /access/list', body=".+\"non_granted_user\".+", headers={ 'Authorization': 'Bearer security-token' }).reply('[false]', headers={'Content-Type': 'application/json'}, times=FOREVER) test_props = create_local_testdb(db_path=tmpdir, data_path=test_data_path / 'testdb', auth_url=mock.pretend_url) mp = ixmp.Platform(dbprops=test_props) mp.set_log_level('DEBUG') granted = mp.check_access('test_user', 'test_model') assert granted granted = mp.check_access('non_granted_user', 'test_model') assert not granted granted = mp.check_access('non_existing_user', 'test_model') assert not granted
def test_get_mock_server_by_name(): http_mock = HTTPMock('localhost', 8000, timeout=5, name='fred') # Check that we are using the name path assert_equals(http_mock.pretend_access_point, "localhost:8000") assert_equals(http_mock.pretend_access_path, "/mockhttp/fred") # Set up a rule http_mock.when('POST /someplace').reply(b"something interesting", 200) # Perform a post from a pretend application fake_client = get_fake_client(http_mock) response = fake_client.post(url='/someplace', body="anything".encode()) # check the app would receive back the response from the rule we set up. assert_equals(response.status, 200) assert_equals(response.read(), b'something interesting') # finally, check that we can look at the history via the http_mock. req = http_mock.get_request(0) assert_equals(req.method, 'POST') assert_equals(req.url, '/someplace') http_mock.delete_mock()
def test_pretender_expired_add_preset_404(): """ Test that an expired pretender cannot have a preset assigned. This tries to test a race condition: The maintainer deletes expired Mocks. It does this by polling. Between polls, it is possible that a mock has expired and someone tries to apply a preset. In such a case, we should get a 404. """ http_mock = HTTPMock('localhost', 8000, timeout=0.1) time.sleep(0.3) preset = http_mock.when('POST /fred/test/one') assert_raises(ConfigurationError, preset.reply, b'You tested fred well', 200)
def test_get_mock_server_by_name(): http_mock = HTTPMock('localhost', 8000, timeout=5, name='fred') # Check that we are using the name path assert_equals(http_mock.pretend_access_point, "localhost:8000") assert_equals(http_mock.pretend_access_path, "/mockhttp/fred") # Set up a rule http_mock.when('POST /someplace').reply(b"something interesting", 200) # Perform a post from a pretend application fake_client = get_fake_client(http_mock) response, data = fake_client.post(url='/someplace', body="anything".encode()) # check the app would receive back the response from the rule we set up. assert_equals(response.status, 200) assert_equals(data, b'something interesting') # finally, check that we can look at the history via the http_mock. req = http_mock.get_request(0) assert_equals(req.method, 'POST') assert_equals(req.url, '/someplace') http_mock.delete_mock()
def test_pretender_expired_add_preset_404(): """ Test that an expired pretender cannot have a preset assigned. This tries to test a race condition: The maintainer deletes expired Mocks. It does this by polling. Between polls, it is possible that a mock has expired and someone tries to apply a preset. In such a case, we should get a 404. """ http_mock = HTTPMock('localhost', 8000, timeout=0.1) time.sleep(0.3) preset = http_mock.when('POST /fred/test/one') assert_raises( ConfigurationError, preset.reply, b'You tested fred well', 200 )
def get_mock(): mock = HTTPMock('127.0.0.1', 50000) mock.when('GET /network/peering') \ .reply(body=bytes(json.dumps(bma_peering), "utf-8"), times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/parameters') \ .reply(body=bytes(json.dumps(bma_parameters), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/with/[UD|ud]') \ .reply(body=bytes(json.dumps(bma_with_ud), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/current') \ .reply(body=bytes(json.dumps(bma_blockchain_current), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/block/0') \ .reply(body=bytes(json.dumps(bma_blockchain_0), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/block/15') \ .reply(body=bytes(json.dumps(bma_blockchain_current), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /tx/history/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ/blocks/0/99') \ .reply(body=bytes(json.dumps(bma_txhistory_john), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /tx/sources/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ') \ .reply(body=bytes(json.dumps(bma_txsources_john), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /ud/history/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ') \ .reply(body=bytes(json.dumps(bma_udhistory_john), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/certifiers-of/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ') \ .reply(body=bytes(json.dumps(bma_certifiers_of_john), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/certified-by/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ') \ .reply(body=bytes(json.dumps(bma_certified_by_john), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/john') \ .reply(body=bytes(json.dumps(bma_lookup_john), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ') \ .reply(body=bytes(json.dumps(bma_lookup_john), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/doe') \ .reply(body=bytes(json.dumps(bma_lookup_doe), "utf-8"), status=200, times=1, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn') \ .reply(body=bytes(json.dumps(bma_lookup_doe), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/memberships/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ') \ .reply(body=bytes(json.dumps(bma_membership_john), "utf-8"), status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/certifiers-of/FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn') \ .reply(body=b"No member matching this pubkey or uid", status=404, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/memberships/FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn') \ .reply(body=b"No member matching this pubkey or uid", status=404, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('POST /tx/process') \ .reply(body=b"", status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) return mock
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') print("--- %s seconds ---" % (time.time() - start_time)) print(res)
def get_mock(): mock = HTTPMock('127.0.0.1', 50000) mock.when('GET /network/peering')\ .reply(body=bma_peering, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/block/0')\ .reply(body=b"Block not found", status=404, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/current')\ .reply(body=b"Block not found", status=404, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/certifiers-of/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')\ .reply(body=b"No member matching this pubkey or uid", status=404, times=1, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/john')\ .reply(body=bma_lookup_test_john, status=200, times=1, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')\ .reply(body=bma_lookup_test_john, status=200, times=1, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/doe')\ .reply(body=bma_lookup_test_doe, status=200, times=1, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn')\ .reply(body=bma_lookup_test_doe, status=200, times=1, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/patrick')\ .reply(body=bma_lookup_test_patrick, status=200, times=1, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn')\ .reply(body=bma_lookup_test_patrick, status=200, times=1, headers={'Content-Type': 'application/json'}) mock.when('POST /wot/add.*')\ .reply(body=b"{}", status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) return mock
def get_mock(): mock = HTTPMock('127.0.0.1', 50000) mock.when('GET /network/peering')\ .reply(body=bma_peering, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/block/0')\ .reply(body=b"Block not found", status=404, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /blockchain/current')\ .reply(body=b"Block not found", status=404, times=FOREVER, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/certifiers-of/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')\ .reply(body=b"No member matching this pubkey or uid", status=404, times=1, headers={'Content-Type': 'application/json'}) mock.when('GET /wot/lookup/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')\ .reply(body=b"No member matching this pubkey or uid", status=404, times=1, headers={'Content-Type': 'application/json'}) mock.when('POST /wot/add.*')\ .reply(body=bma_wot_add, status=200, times=FOREVER, headers={'Content-Type': 'application/json'}) return mock