Ejemplo n.º 1
0
def test_failed_to_get_route_config():
    client_request = entities.ClientRequest(
        method='GET',
        path='path',
        query_string='name=name',
        headers={},
        raw_body=b'',
    )

    template_service = mock.MagicMock(spec=services.TemplateService)
    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = None
    file_helper = mock.MagicMock(spec=helpers.FileHelperBase)
    json_helper = mock.MagicMock(spec=helpers.JSONHelperBase)

    uc = GetMockResponseUseCase(
        template_service=template_service,
        mat_config_repository=mat_config_repository,
        file_helper=file_helper,
        json_helper=json_helper,
    )

    with pytest.raises(exceptions.NotFoundError, match='找不到對應的 ConfigRoute'):
        assert uc.execute(client_request)

    mat_config_repository.query_route_config.assert_called_with(
        path=client_request.path,
        method=client_request.method,
        query_string=client_request.query_string,
    )
Ejemplo n.º 2
0
def test_check_mock_response_exists():
    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = entities.RouteConfig(
        listen_path='listen_path',
        method='GET',
        status_code=200,
        query=None,
        response=entities.RouteResponseConfig(),
    )

    uc = CheckIfMockResponseExistsUseCase(
        mat_config_repository=mat_config_repository, )

    assert uc.execute(
        entities.ClientRequest(method='GET',
                               path='path',
                               query_string='query_string',
                               headers={},
                               raw_body=b'')) is True

    mat_config_repository.query_route_config.assert_called_with(
        path='path',
        method='GET',
        query_string='query_string',
    )
Ejemplo n.º 3
0
        async def proxy(path, request: fastapi.Request):
            client_request = entities.ClientRequest(
                method=request.method,
                path=path,
                query_string=str(request.query_params),
                headers=dict(request.headers),
                raw_body=await request.body()
            )

            # 檢查是否需要 mock response
            existed = self._check_if_mock_response_exists_use_case.execute(client_request)

            # 如果需要 mock
            if existed:
                mock_response = self._get_mock_response_use_case.execute(client_request)
                return transform_response_to_fastapi_response(mock_response)

            # 檢查是否有 Proxy Server
            existed = self._check_if_proxy_server_exists_use_case.execute()
            if not existed:
                raise fastapi.HTTPException(status_code=404)

            # 如果不需要 mock,直接轉給 proxy server
            else:
                proxy_server_response = self._get_proxy_server_response_use_case.execute(client_request)
                return transform_response_to_fastapi_response(proxy_server_response)
Ejemplo n.º 4
0
def test_get_mock_response_using_response_file_path():
    data = b'bytes'

    client_request = entities.ClientRequest(
        method='GET',
        path='path',
        query_string='name=name',
        headers={},
        raw_body=b'',
    )

    route_config = entities.RouteConfig(
        listen_path=client_request.path,
        method=client_request.method,
        status_code=200,
        query=urllib.parse.parse_qs(client_request.query_string),
        response=entities.RouteResponseConfig(file_path='file_path'),
    )

    template_service = mock.MagicMock(spec=services.TemplateService)

    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = route_config

    file_helper = mock.MagicMock(spec=helpers.FileHelperBase)
    file_helper.join_file_paths.return_value = 'joined_file_path'
    file_helper.read_bytes.return_value = data
    file_helper.guess_file_type.return_value = 'application/json'

    json_helper = mock.MagicMock(spec=helpers.JSONHelperBase)

    uc = GetMockResponseUseCase(
        template_service=template_service,
        mat_config_repository=mat_config_repository,
        file_helper=file_helper,
        json_helper=json_helper,
    )
    assert uc.execute(client_request) == entities.ServerResponse(
        raw_body=data,
        status_code=route_config.status_code,
        headers={
            'Content-Type': 'application/json',
        },
    )

    mat_config_repository.query_route_config.assert_called_with(
        path=client_request.path,
        method=client_request.method,
        query_string=client_request.query_string,
    )

    file_helper.join_file_paths.assert_called_with('mat-data', 'file_path')
    file_helper.read_bytes.assert_called_with('joined_file_path')
    file_helper.guess_file_type.assert_called_with('joined_file_path')
Ejemplo n.º 5
0
def test_check_mock_response_not_exists():
    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = None

    uc = CheckIfMockResponseExistsUseCase(
        mat_config_repository=mat_config_repository, )

    assert uc.execute(
        entities.ClientRequest(method='GET',
                               path='path',
                               query_string='query_string',
                               headers={},
                               raw_body=b'')) is False
Ejemplo n.º 6
0
def test_get_mock_response_using_response_data_with_json_type():
    client_request = entities.ClientRequest(
        method='GET',
        path='path',
        query_string='name=name',
        headers={},
        raw_body=b'',
    )

    route_config = entities.RouteConfig(
        listen_path=client_request.path,
        method=client_request.method,
        status_code=200,
        query=urllib.parse.parse_qs(client_request.query_string),
        response=entities.RouteResponseConfig(data={
            'msg': 'data',
        }),
    )

    template_service = mock.MagicMock(spec=services.TemplateService)

    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = route_config

    file_helper = mock.MagicMock(spec=helpers.FileHelperBase)
    json_helper = mock.MagicMock(spec=helpers.JSONHelperBase)
    json_helper.serialize.return_value = json.dumps(route_config.response.data)

    uc = GetMockResponseUseCase(
        template_service=template_service,
        mat_config_repository=mat_config_repository,
        file_helper=file_helper,
        json_helper=json_helper,
    )
    assert uc.execute(client_request) == entities.ServerResponse(
        raw_body=json_helper.serialize.return_value.encode(),
        status_code=route_config.status_code,
        headers={
            'Content-Type': 'application/json',
        },
    )

    mat_config_repository.query_route_config.assert_called_with(
        path=client_request.path,
        method=client_request.method,
        query_string=client_request.query_string,
    )
def test_get_proxy_server_response():
    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.get_proxy_host.return_value = 'https://paji.marco79423.net'

    request_helper = mock.MagicMock(spec=helpers.HTTPRequestHelperBase)
    request_helper.send.return_value = entities.HTTPResponse(
        raw_data=b'raw_data',
        status_code=200,
        headers={
            'Name': 'name',
            'connection': 'Close',
        },
    )

    uc = GetProxyServerResponseUseCase(
        mat_config_repository=mat_config_repository,
        request_helper=request_helper,
    )

    server_response = uc.execute(
        request=entities.ClientRequest(method='GET',
                                       path='path',
                                       query_string='name=name',
                                       headers={
                                           'Host': 'host',
                                           'Name': 'name',
                                       }))

    assert server_response == entities.ServerResponse(
        raw_body=b'raw_data',
        status_code=200,
        headers={
            'Name': 'name',
        },
    )

    request_helper.send.assert_called_with(
        entities.HTTPRequest(
            url='https://paji.marco79423.net/path?name=name',
            method='GET',
            headers={
                'name': 'name',
            },
            raw_body=b'',
        ))
Ejemplo n.º 8
0
def test_get_mock_response_with_conflict_response_config():
    client_request = entities.ClientRequest(
        method='GET',
        path='path',
        query_string='name=name',
        headers={},
        raw_body=b'',
    )

    route_config = entities.RouteConfig(
        listen_path=client_request.path,
        method=client_request.method,
        status_code=200,
        query=urllib.parse.parse_qs(client_request.query_string),
        response=entities.RouteResponseConfig(
            file_path='file_path',
            data='raw_data',
        ),
    )

    template_service = mock.MagicMock(spec=services.TemplateService)
    mat_config_repository = mock.MagicMock(
        spec=repositories.MatConfigRepositoryBase)
    mat_config_repository.query_route_config.return_value = route_config
    file_helper = mock.MagicMock(spec=helpers.FileHelperBase)
    json_helper = mock.MagicMock(spec=helpers.JSONHelperBase)

    uc = GetMockResponseUseCase(
        template_service=template_service,
        mat_config_repository=mat_config_repository,
        file_helper=file_helper,
        json_helper=json_helper,
    )

    with pytest.raises(exceptions.ValidationError, match='回傳資源衝突'):
        assert uc.execute(client_request)

    mat_config_repository.query_route_config.assert_called_with(
        path=client_request.path,
        method=client_request.method,
        query_string=client_request.query_string,
    )