Example #1
0
def test_clear_down_removes_history():
    # Test that when we clear down a pretender, the history is removed.
    # Otherwise we end up slowly creeping up the memory usage!
    http_mock = HTTPMock('localhost', 8000, timeout=5)
    pretender = http_mock.get_pretender()
    pretender_client = get_fake_client(http_mock)
    pretender_client.get(
            url="/some_url"
        )
    assert_equal(http_mock.get_request(0).url, '/some_url')
    time.sleep(STALE_DELETE_FREQUENCY + pretender.timeout_in_secs + 1)
    assert_raises(ResourceNotFound, http_mock.get_pretender)
    # Check that there is no history now!
    req = http_mock.get_request(0)
    assert_equal(req, None)
Example #2
0
def test_clear_down_removes_history():
    # Test that when we clear down a pretender, the history is removed.
    # Otherwise we end up slowly creeping up the memory usage!
    http_mock = HTTPMock('localhost', 8000, timeout=5)
    pretender = http_mock.get_pretender()
    pretender_client = get_fake_client(http_mock)

    pretender_client.get(url="/some_url")

    assert_equal(http_mock.get_request(0).url, '/some_url')

    time.sleep(STALE_DELETE_FREQUENCY + pretender.timeout.seconds + 1)
    assert_raises(ResourceNotFound, http_mock.get_pretender)
    # Check that there is no history now!
    req = http_mock.get_request(0)
    assert_equal(req, None)
Example #3
0
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)
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)
def test_creating_second_mock_server_by_same_name_gives_original_server():
    # Create 1
    h1 = HTTPMock('localhost', 8000, timeout=5, name='duplicate_test')
    # Create another
    h2 = HTTPMock('localhost', 8000, timeout=5, name='duplicate_test_2')
    # Creation of one with a duplicate name should succeed.
    h3 = HTTPMock('localhost', 8000, name='duplicate_test')

    # Requests to h1 should be visible by h3. h2 should only be seen by it.

    get_fake_client(h1).get(url='/h1_get')
    get_fake_client(h2).get(url='/h2_get')
    get_fake_client(h3).get(url='/h3_get')

    # assert that the requests h1 and h3 requests can be seen via both mocks.
    assert_equals(h1.get_request(0).url, h3.get_request(0).url)
    assert_equals(h1.get_request(1).url, h3.get_request(1).url)
    assert_equals(h2.get_request(0).url, '/h2_get')

    h1.delete_mock()
    h2.delete_mock()
def test_creating_second_mock_server_by_same_name_gives_original_server():
    # Create 1
    h1 = HTTPMock('localhost', 8000, timeout=5, name='duplicate_test')
    # Create another
    h2 = HTTPMock('localhost', 8000, timeout=5, name='duplicate_test_2')
    # Creation of one with a duplicate name should succeed.
    h3 = HTTPMock('localhost', 8000, name='duplicate_test')

    # Requests to h1 should be visible by h3. h2 should only be seen by it.

    get_fake_client(h1).get(url='/h1_get')
    get_fake_client(h2).get(url='/h2_get')
    get_fake_client(h3).get(url='/h3_get')

    # assert that the requests h1 and h3 requests can be seen via both mocks.
    assert_equals(h1.get_request(0).url, h3.get_request(0).url)
    assert_equals(h1.get_request(1).url, h3.get_request(1).url)
    assert_equals(h2.get_request(0).url, '/h2_get')

    h1.delete_mock()
    h2.delete_mock()
Example #7
0
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_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_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()