コード例 #1
0
def test_content_type(monkeypatch):
    """
    Test HTTP Content-type header handling.
    """
    for method in ["PUT", "POST", "DELETE"]:
        text_plain_header = {CONTENT_TYPE: 'text/plain'}
        for header_arg in [text_plain_header, None]:
            call = {
                "uri": "http://localhost:8080/source/api/v1/foo",
                "method": method,
                "data": "data",
                "headers": header_arg
            }

            def mock_response(verb, uri, **kwargs):
                headers = kwargs['headers']
                if header_arg:
                    assert text_plain_header.items() <= headers.items()
                else:
                    assert {CONTENT_TYPE: APPLICATION_JSON}.items() \
                        <= headers.items()

            with monkeypatch.context() as m:
                m.setattr("opengrok_tools.utils.restful.do_api_call",
                          mock_response)
                call_rest_api(ApiCall(call))
コード例 #2
0
def test_restful_fail(monkeypatch):
    """
    Test that failures in call_rest_api() result in HTTPError exception.
    This is done only for the PUT HTTP verb.
    """
    class MockResponse:
        def p(self):
            raise HTTPError("foo")

        def __init__(self):
            self.status_code = 400
            self.raise_for_status = self.p

    def mock_response(uri, headers, data, params, proxies, timeout):
        return MockResponse()

    with monkeypatch.context() as m:
        m.setattr("requests.put", mock_response)
        with pytest.raises(HTTPError):
            call_rest_api(
                ApiCall({
                    "uri": 'http://foo',
                    "method": 'PUT',
                    "data": 'data'
                }))
コード例 #3
0
ファイル: test_restful.py プロジェクト: vibhudv/opengrok
def test_content_type(monkeypatch):
    """
    Test HTTP Content-type header handling.
    """
    for verb in ["PUT", "POST", "DELETE"]:
        text_plain_header = {CONTENT_TYPE: 'text/plain'}
        for header_arg in [text_plain_header, None]:
            command = {
                "command": [
                    "http://localhost:8080/source/api/v1/foo", verb, "data",
                    header_arg
                ]
            }

            def mock_response(uri, verb, headers, data):
                if header_arg:
                    assert text_plain_header.items() <= headers.items()
                else:
                    assert {CONTENT_TYPE: APPLICATION_JSON}.items() \
                        <= headers.items()

            with monkeypatch.context() as m:
                m.setattr("opengrok_tools.utils.restful.do_api_call",
                          mock_response)
                call_rest_api(command)
コード例 #4
0
def test_headers_timeout(monkeypatch):
    """
    Test that HTTP headers from command specification are united with
    HTTP headers passed to call_res_api(). Also test timeout.
    :param monkeypatch: monkey fixture
    """
    headers = {'Tatsuo': 'Yasuko'}
    expected_timeout = 42
    expected_api_timeout = 24
    call = {
        "uri": "http://localhost:8080/source/api/v1/bar",
        "method": "GET",
        "data": "data",
        "headers": headers
    }
    extra_headers = {'Mei': 'Totoro'}

    def mock_do_api_call(verb, uri, **kwargs):
        all_headers = headers
        all_headers.update(extra_headers)
        assert headers == all_headers
        assert kwargs['timeout'] == expected_timeout
        assert kwargs['api_timeout'] == expected_api_timeout

    with monkeypatch.context() as m:
        m.setattr("opengrok_tools.utils.restful.do_api_call", mock_do_api_call)
        call_rest_api(ApiCall(call),
                      http_headers=extra_headers,
                      timeout=expected_timeout,
                      api_timeout=expected_api_timeout)
コード例 #5
0
def test_headers(monkeypatch):
    """
    Test HTTP header handling.
    """
    for verb in ["PUT", "POST", "DELETE"]:
        TEXT_PLAIN = {'Content-type': 'text/plain'}
        for headers in [TEXT_PLAIN, None]:
            command = {
                "command": [
                    "http://localhost:8080/source/api/v1/foo", verb, "data",
                    headers
                ]
            }

            def mock_response(command, uri, verb, headers_arg, data):
                if headers:
                    assert TEXT_PLAIN.items() <= headers_arg.items()
                else:
                    assert {CONTENT_TYPE: APPLICATION_JSON}.items() \
                        <= headers_arg.items()

            with monkeypatch.context() as m:
                m.setattr("opengrok_tools.utils.restful.do_api_call",
                          mock_response)
                call_rest_api(command, None, None)
コード例 #6
0
ファイル: test_restful.py プロジェクト: zzmjohn/opengrok
def test_unknown_verb():
    command = {"command": ["http://localhost:8080/source/api/v1/foo",
                           "FOOBAR", "data"]}
    pattern = "%FOO%"
    value = "BAR"
    with pytest.raises(Exception):
        call_rest_api(command, {pattern: value})
コード例 #7
0
def test_invalid_command_bad_uri():
    with pytest.raises(Exception):
        call_rest_api(
            ApiCall({
                "uri": "foo",
                "method": "PUT",
                "data": "data",
                "headers": "headers"
            }))
コード例 #8
0
def test_unknown_method():
    call = {
        "uri": "http://localhost:8080/source/api/v1/foo",
        "method": "FOOBAR",
        "data": "data"
    }
    pattern = "%FOO%"
    value = "BAR"
    with pytest.raises(Exception):
        call_rest_api(ApiCall(call), {pattern: value})
コード例 #9
0
ファイル: test_restful.py プロジェクト: lzufalcon/opengrok
def test_restful_verbs(monkeypatch):
    okay_status = 200

    class MockResponse:
        def __init__(self):
            self.status_code = okay_status

    def mock_response(command, uri, verb, headers, json_data):
        # Spying on mocked function is maybe too much so verify
        # the arguments here.
        assert uri == "http://localhost:8080/source/api/v1/BAR"
        assert json_data == '"fooBARbar"'

        return MockResponse()

    for verb in ["PUT", "POST", "DELETE"]:
        command = {
            "command":
            ["http://localhost:8080/source/api/v1/%FOO%", verb, "foo%FOO%bar"]
        }
        pattern = "%FOO%"
        value = "BAR"
        with monkeypatch.context() as m:
            m.setattr("opengrok_tools.utils.restful.do_api_call",
                      mock_response)
            assert call_rest_api(command, pattern, value). \
                status_code == okay_status
コード例 #10
0
ファイル: test_restful.py プロジェクト: vibhudv/opengrok
def test_restful_fail(monkeypatch):
    class MockResponse:
        def p(self):
            raise HTTPError("foo")

        def __init__(self):
            self.status_code = 400
            self.raise_for_status = self.p

    def mock_response(uri, headers, data, params, proxies):
        return MockResponse()

    with monkeypatch.context() as m:
        m.setattr("requests.put", mock_response)
        with pytest.raises(HTTPError):
            call_rest_api({'command': ['http://foo', 'PUT', 'data']})
コード例 #11
0
ファイル: test_restful.py プロジェクト: zzmjohn/opengrok
def test_replacement(monkeypatch):
    """
    Test replacement performed both in the URL and data.
    """
    okay_status = 200

    class MockResponse:
        def p(self):
            pass

        def __init__(self):
            self.status_code = okay_status
            self.raise_for_status = self.p

    def mock_do_api_call(verb, uri, headers, data, timeout):
        # Spying on mocked function is maybe too much so verify
        # the arguments here.
        assert uri == "http://localhost:8080/source/api/v1/BAR"
        assert data == '"fooBARbar"'

        return MockResponse()

    for verb in ["PUT", "POST", "DELETE"]:
        command = {"command": ["http://localhost:8080/source/api/v1/%FOO%",
                               verb, "foo%FOO%bar"]}
        pattern = "%FOO%"
        value = "BAR"
        with monkeypatch.context() as m:
            m.setattr("opengrok_tools.utils.restful.do_api_call",
                      mock_do_api_call)
            assert call_rest_api(command, {pattern: value}). \
                status_code == okay_status
コード例 #12
0
ファイル: test_restful.py プロジェクト: yyyiii/opengrok
def test_restful_fail(monkeypatch):
    class MockResponse:
        def p(self):
            raise HTTPError("foo")

        def __init__(self):
            self.status_code = 400
            self.raise_for_status = self.p

    def mock_response(uri, verb, headers, data):
        return MockResponse()

    with monkeypatch.context() as m:
        m.setattr("opengrok_tools.utils.restful.do_api_call", mock_response)
        with pytest.raises(HTTPError):
            call_rest_api({'command': ['http://foo', 'PUT', 'data']}, None,
                          None)
コード例 #13
0
ファイル: test_restful.py プロジェクト: zzmjohn/opengrok
def test_invalid_command_negative():
    with pytest.raises(Exception):
        call_rest_api(None)

    with pytest.raises(Exception):
        call_rest_api({"foo": "bar"})

    with pytest.raises(Exception):
        call_rest_api(["foo", "bar"])

    with pytest.raises(Exception):
        call_rest_api({COMMAND_PROPERTY: ["foo", "PUT", "data", "headers"]})
コード例 #14
0
ファイル: test_restful.py プロジェクト: vibhudv/opengrok
def test_headers(monkeypatch):
    """
    Test that HTTP headers from command specification are united with
    HTTP headers passed to call_res_api()
    :param monkeypatch: monkey fixture
    """
    headers = {'Tatsuo': 'Yasuko'}
    command = {
        "command":
        ["http://localhost:8080/source/api/v1/bar", 'GET', "data", headers]
    }
    extra_headers = {'Mei': 'Totoro'}

    def mock_response(uri, verb, headers, data):
        all_headers = headers
        all_headers.update(extra_headers)
        assert headers == all_headers

    with monkeypatch.context() as m:
        m.setattr("opengrok_tools.utils.restful.do_api_call", mock_response)
        call_rest_api(command, http_headers=extra_headers)
コード例 #15
0
def test_api_call_timeout_override(monkeypatch):
    """
    Test that ApiCall object timeouts override timeouts passed as call_rest_api() arguments.
    """
    expected_timeout = 42
    expected_api_timeout = 24

    call = {
        "uri": "http://localhost:8080/source/api/v1/bar",
        "method": "POST",
        "data": "data",
        "api_timeout": expected_timeout,
        "async_api_timeout": expected_api_timeout
    }

    def mock_do_api_call(verb, uri, **kwargs):
        assert kwargs['timeout'] == expected_timeout
        assert kwargs['api_timeout'] == expected_api_timeout

    with monkeypatch.context() as m:
        m.setattr("opengrok_tools.utils.restful.do_api_call", mock_do_api_call)
        call_rest_api(ApiCall(call),
                      timeout=expected_timeout + 1,
                      api_timeout=expected_api_timeout + 1)
コード例 #16
0
def test_invalid_command_none():
    with pytest.raises(Exception):
        call_rest_api(None)
コード例 #17
0
def test_invalid_command_uknown_key():
    with pytest.raises(Exception):
        call_rest_api(ApiCall({"foo": "bar"}))
コード例 #18
0
def test_invalid_command_list():
    with pytest.raises(Exception):
        call_rest_api(ApiCall(["foo", "bar"]))