def test_multiple_mock_servers_only_see_their_presets_and_history(): first_mock = HTTPMock('localhost', 8000, pretender_timeout=30) second_mock = HTTPMock('localhost', 8000, pretender_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)
def test_start_http_pretender(): """ Test that the http client kicks off an server via a call to the boss. TODO: This will need updating when we change the server to return dynamic port configurations for the mock server. """ new_mock = HTTPMock('localhost', 8000) assert_true(new_mock.pretend_access_point != "localhost:8000") assert_true( int(new_mock.pretend_access_point.split(':')[1]) in PRETEND_PORT_RANGE)
def test_clear_down_of_stale_mock_servers_taking_place(): #Test that stale mock servers are cleared out # TODO: Once the timeout specification can be dictated by the client # the sleep in this test can be reduced. http_mock = HTTPMock('localhost', 8000, pretender_timeout=5) pretender = http_mock.get_pretender() assert_equal(http_mock.pretend_access_point_id, pretender.uid) # Sleep for enough time for the maintainer to have run and killed the # process. which means the total of STALE_DELETE_FREQUENCY + timeout # time.sleep(STALE_DELETE_FREQUENCY + pretender.timeout_in_secs) assert_raises(ResourceNotFound, http_mock.get_pretender)
def test_clear_down_only_happens_if_no_request_for_timeout_period(): # Test that stale mock servers are not cleared if they # recently made a request. # TODO: Once the timeout specification can be dictated by the client # the sleep in this test can be reduced. http_mock = HTTPMock('localhost', 8000, pretender_timeout=5) pretender = http_mock.get_pretender() timeout_server = pretender.timeout_in_secs assert_equal(pretender.last_call, pretender.start) for i in range(3): # Sleep for a while, check that the server is still running and then # make a call to the mock server. time.sleep(timeout_server / 2) # Check that we are still running pretender = http_mock.get_pretender() # Make a call to the mock server. pretender_client = APIHelper( HTTPConnection(http_mock.pretend_access_point), '') pretender_client.http(method="GET", url="/some_url")
from nose.tools import assert_equals, assert_true from pretenders.http.client import HTTPMock from pretenders.http.tests.integration import FakeClient from pretenders.http.tests.integration import get_fake_client http_mock = HTTPMock('localhost', 8000) def assert_response_equal(response, body, status): assert_equals(response.status, status) assert_equals(response.read(), body) def test_multiple_mock_servers_only_see_their_presets_and_history(): first_mock = HTTPMock('localhost', 8000, pretender_timeout=30) second_mock = HTTPMock('localhost', 8000, pretender_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)