Beispiel #1
0
def test_request_matched_hostname_only():
    with TestMock() as mock_test:
        with NetworkBlocker():
            response = urllib.request.urlopen('http://127.0.0.1', timeout=0)
            response.read()
        assert response.getcode() == 200
        mock_test.test.request_mock.assert_not_called()
Beispiel #2
0
def test_mode_strict():
    with NetworkBlocker(mode=NetworkBlocker.Modes.STRICT):
        try:
            send()
            fail('Should fail')
        except NetworkBlockException:
            pass
Beispiel #3
0
def test_request_unmatched():
    with TestMock():
        try:
            with NetworkBlocker():
                urllib.request.urlopen('http://localhost', timeout=0).read()
            fail('Request should not be prevented by HttpApiMock')
        except NetworkBlockException:
            pass
def test_request_ignore():
    with HttpMock(mode=TestMock.Modes.MOCK) as send_mock:
        try:
            with NetworkBlocker():
                send_request()
            fail('Request should not be prevented by HttpMock')
        except NetworkBlockException:
            pass
        send_mock.assert_not_called()
def test_request_mode_disabled():
    with TestMock(mode=TestMock.Modes.DISABLED) as send_mock:
        try:
            with NetworkBlocker():
                send_request()
            fail('Request should not be prevented by HttpMock')
        except NetworkBlockException:
            pass
        send_mock.assert_not_called()
Beispiel #6
0
def test_mode_warning(capsys):
    capman = PytestIntegration.capman
    PytestIntegration.capman = None
    try:
        with NetworkBlocker(mode=NetworkBlocker.Modes.WARNING):
            send()
        err = capsys.readouterr().err
        assert len(err) > 0
    finally:
        PytestIntegration.capman = capman
Beispiel #7
0
def test_request_override_response():
    with TestMock() as mock_test:
        mock_test.test.response = lambda groups: (204, None)
        with NetworkBlocker():
            response = urllib.request.urlopen(
                'http://127.0.0.1/test/abc/', timeout=0
            )
            assert response.read() is None
        assert mock_test.test.request_mock.called_once()
        assert response.getcode() == 204
def test_request_mode_watch():
    with TestMock(mode=TestMock.Modes.WATCH) as send_mock:
        try:
            with NetworkBlocker():
                send_request()
            fail('Request should not be prevented by HttpMock')
        except NetworkBlockException:
            pass
        send_mock.assert_called_once()
        assert send_mock.call_args[0][1].startswith(b'GET /')
Beispiel #9
0
def test_request_override_removed_nested_mock():
    with TestMock() as mock_test1:
        with TestMock() as mock_test2:
            mock_test2.test.response = lambda groups: (204, None)
            with NetworkBlocker():
                urllib.request.urlopen('http://127.0.0.1/test/abc/',
                                       timeout=0).read()

        try:
            with NetworkBlocker():
                urllib.request.urlopen('http://127.0.0.1/test/abc/',
                                       timeout=0).read()
            fail('Request should raise HTTPError')
        except urllib.error.HTTPError as e:
            assert e.code == 418
            assert e.read() == b'{"id": "abc"}'
        mock_test1.test.request_mock.assert_called_once()
        assert mock_test1.test.request_mock.call_args[0][0] == {
            'test_id': 'abc'
        }
Beispiel #10
0
def test_request_matched_endpoint_requests():
    with TestMock() as mock_test:
        with NetworkBlocker():
            response = requests.get('http://127.0.0.1/test/abc/')
        assert response.status_code == 418
        assert response.content == b'{"id": "abc"}'

        mock_test.test.request_mock.assert_called_once()
        assert mock_test.test.request_mock.call_args[0][0] == {
            'test_id': 'abc'
        }
Beispiel #11
0
def test_request_matched_endpoint():
    with TestMock() as mock_test:
        try:
            with NetworkBlocker():
                urllib.request.urlopen('http://127.0.0.1/test/abc/',
                                       timeout=0).read()
            fail('Request should raise HTTPError')
        except urllib.error.HTTPError as e:
            assert e.code == 418
            assert e.read() == b'{"id": "abc"}'
        mock_test.test.request_mock.assert_called_once()
        assert mock_test.test.request_mock.call_args[0][0] == {
            'test_id': 'abc'
        }
Beispiel #12
0
def test_mode_disabled(capsys):
    with NetworkBlocker(mode=NetworkBlocker.Modes.DISABLED):
        send()
    err = capsys.readouterr().err
    assert len(err) == 0
Beispiel #13
0
def test_mode_warning_disables_pytest_capture(capsys):
    with NetworkBlocker(mode=NetworkBlocker.Modes.WARNING):
        send()
    err = capsys.readouterr().err
    assert len(err) == 0
def test_request_mode_mock():
    with TestMock(mode=TestMock.Modes.MOCK) as send_mock:
        with NetworkBlocker():
            send_request()
        send_mock.assert_called_once()
        assert send_mock.call_args[0][1].startswith(b'GET /')