コード例 #1
0
def test_text_full(mock_data_store):
    callback_manager.load("tests/serve/mock/callbacks")
    mock_data_store.clear()

    request = RequestBuilder.from_dict(
        dict(
            method="post",
            host="another.api.com",
            pathname="/echo",
            query={},
            protocol="http",
            body="Hello",
            bodyAsJson={},
            headers={},
        ))

    response = ResponseBuilder.from_dict(
        dict(statusCode=200,
             body="",
             bodyAsJson={"field": "value"},
             headers={}))

    new_response = callback_manager(request, response, MockData())

    assert "Hello" == new_response.body
    assert "value" == new_response.headers["X-Echo-Header"]
コード例 #2
0
def test_example_from_readme():
    request = RequestBuilder.from_dict({
        "host": "api.github.com",
        "protocol": "https",
        "method": "get",
        "pathname": "/v1/users",
        "query": {
            "a": "b",
            "q": ["1", "2"]
        },
    })

    response = ResponseBuilder.from_dict({
        "statusCode": 200,
        "headers": {
            "content-type": "text/plain"
        },
        "body": "(response body string)",
    })

    exchange = HttpExchange(request=request, response=response)

    output_file = StringIO()
    writer = HttpExchangeWriter(output_file)
    writer.write(exchange)

    input_file = output_file
    input_file.seek(0)

    for exchange in HttpExchangeReader.from_jsonl(input_file):
        assert exchange.request.method == HttpMethod.GET
        assert exchange.request.protocol == Protocol.HTTPS
        assert exchange.response.statusCode == 200
コード例 #3
0
 def __call__(self, request, response, storage):
     callback = self._callbacks.get(
         (request.host, request.method.value, request.pathname))
     if callback is not None:
         out = callback(asdict(request), asdict(response), storage)
         return ResponseBuilder.from_dict(out)
     else:
         return response
コード例 #4
0
def test_response_from_dict_without_body():
    response = ResponseBuilder.from_dict({
        "statusCode": 200,
        "headers": {
            "content-type": "text/plain"
        }
    })
    assert response.statusCode == 200
コード例 #5
0
def test_json():
    callback_manager.load("tests/serve/mock/callbacks")
    storage = MockData()

    request = RequestBuilder.from_dict(
        dict(
            method="post",
            host="api.com",
            pathname="/counter",
            query={},
            body="",
            protocol="http",
            bodyAsJson={},
            headers={},
        ))

    response = ResponseBuilder.from_dict(
        dict(statusCode=200,
             body="",
             bodyAsJson={"field": "value"},
             headers={}))

    new_response = callback_manager(request, response, storage)

    assert 1 == new_response.bodyAsJson["count"]
    assert "value" == new_response.bodyAsJson["field"]
    assert "count" in new_response.body

    request_set = RequestBuilder.from_dict(
        dict(
            method="post",
            host="api.com",
            pathname="/counter",
            query={},
            body="",
            protocol="http",
            bodyAsJson={"set": 10},
            headers={},
        ))

    new_response = callback_manager(request_set, response, storage)

    assert 10 == new_response.bodyAsJson["count"]
    assert "value" == new_response.bodyAsJson["field"]
    assert "count" in new_response.body

    new_response = callback_manager(request, response, storage)
    assert 11 == new_response.bodyAsJson["count"]
    assert "value" == new_response.bodyAsJson["field"]
    assert "count" in new_response.body
コード例 #6
0
def test_httpbin():
    httpretty.register_uri(httpretty.GET,
                           "https://httpbin.org/ip",
                           body='{"origin": "127.0.0.1"}')

    rq = request.Request("https://httpbin.org/ip")
    rs = request.urlopen(rq)
    req = RequestBuilder.from_urllib_request(rq)
    res = ResponseBuilder.from_http_client_response(rs)
    assert req.protocol == Protocol.HTTPS
    assert req.method == HttpMethod.GET
    assert req.path == "/ip"
    assert res.statusCode == 200
    assert res.bodyAsJson == {"origin": "127.0.0.1"}
    assert isinstance(res.body, str)
コード例 #7
0
def test_no_callback():
    callback_manager.load("tests/serve/mock/callbacks")

    request = RequestBuilder.from_dict(
        dict(
            method="get",
            host="api.com",
            pathname="/nothing",
            query={},
            body="",
            protocol="http",
            bodyAsJson={},
            headers={},
        ))

    response = ResponseBuilder.from_dict(
        dict(statusCode=200, body="", bodyAsJson={}, headers={}))

    new_response = callback_manager(request, response, MockData())

    assert response == new_response
コード例 #8
0
def test_text(mock_data_store):
    callback_manager.load("tests/serve/mock/callbacks")
    mock_data_store.clear()

    request = RequestBuilder.from_dict(
        dict(
            method="get",
            host="api.com",
            pathname="/text_counter",
            query={"set": 10},
            body="",
            protocol="http",
            bodyAsJson={},
            headers={},
        ))

    response = ResponseBuilder.from_dict(
        dict(statusCode=200, body="Called", bodyAsJson={}, headers={}))

    new_response = callback_manager(request, response, MockData())

    assert 10 == new_response.headers["x-hmt-counter"]
    assert "Called 10 times" == new_response.body
コード例 #9
0
ファイル: test_views.py プロジェクト: wilsonify/hmt
from unittest.mock import Mock

import pytest
from http_types import HttpMethod, Protocol
from http_types.utils import ResponseBuilder
from tornado.httpclient import HTTPRequest

from hmt.serve.mock.log import Log
from hmt.serve.mock.scope import Scope
from hmt.serve.mock.specs import load_specs
from hmt.serve.utils.routing import HeaderRouting

response = ResponseBuilder.from_dict(
    dict(
        statusCode=200,
        body='{"message": "hello"}',
        bodyAsJson=json.loads('{"message": "hello"}'),
        headers={},
    ))
process_mock = Mock(return_value=response)


@pytest.fixture()
def request_processor(request_processor):
    def _rp(specs):
        rp = request_processor(specs)
        rp.process = process_mock
        return rp

    return _rp