def test_givenRouteWhichTakesADtoAsParameter_whenInvoking_thenDeserializeRequestBodyIntoTheDtoClass(
            self):
        self.request = Request('POST', PATH + "/dto", {}, "", DTO_BODY)

        response = self.resource_invocator.invoke(self.request)

        self.assertEqual(ResourceClass.the_response, response)
    def test_givenRouteWhichTakesADictionaryAsParameter_whenInvoking_thenPassRequestBodyAsParameter(
            self):
        self.request = Request('POST', PATH + "/dictionary", {}, "", BODY)

        response = self.resource_invocator.invoke(self.request)

        self.assertEqual(ResourceClass.the_response, response)
    def test_givenOverloadedRouteRegistrations_whenInvoking_thenChooseMethodBasedOnMethodSignature(
            self):
        self.request = Request('GET', PATH + "/overloaded", {}, "query=foo",
                               "")

        response = self.resource_invocator.invoke(self.request)

        self.assertEqual(ResourceClass.OVERLOADED_RETURN, response.body)
    def test_givenRouteWithPathParameters_whenInvoking_thenPassAsArguments(
            self):
        self.request = Request('GET', PATH + "/path-param/" + A_PATH_PARAM, {},
                               "", "")

        response = self.resource_invocator.invoke(self.request)

        self.assertEqual(A_PATH_PARAM, response.body)
    def test_givenRouteWhichTakesADtoWithoutAnExplicitConstructor_whenInvoking_thenDeserializeRequestBodyIntoDto(
            self):
        self.request = Request('POST', PATH + "/dto-no-param", {}, "",
                               DTO_BODY)

        response = self.resource_invocator.invoke(self.request)

        self.assertEqual(ResourceClass.the_response, response)
    def test_givenRouteWithQueryParameter_whenInvoking_thenParseQueryParametersBeforeInvoking(
            self):
        name = "foobar"
        self.request = Request('GET', PATH + "/query-param", {},
                               "name=" + name, "")

        response = self.resource_invocator.invoke(self.request)

        self.assertEqual(name, response.body)
    def test_givenEscapedPathParameter_whenInvoking_thenUnescapeBeforeInvoking(
            self):
        escaped_name = "rocket%20man"
        unescaped_name = "rocket man"
        self.request = Request('GET', PATH + "/path-param/" + escaped_name, {},
                               "", "")

        response = self.resource_invocator.invoke(self.request)

        self.assertEqual(unescaped_name, response.body)
    def test_givenRouteWithNumericPathParameter_whenInvoking_thenParseStringToNumberBeforePassing(
            self):
        a_numeric_path_param = 5
        self.request = Request(
            'GET', PATH + "/numeric-param/" + str(a_numeric_path_param), {},
            "", "")

        response = self.resource_invocator.invoke(self.request)

        self.assertEqual(a_numeric_path_param, response.body)
 def setUp(self):
     self.serviceLocator = ServiceLocator()
     self.serviceLocator.bind(ResourceClass, ResourceClass)
     registry = Registry()
     self.routingTable = ReflectiveRoutingTable(
         registry, [Registration(ResourceClass, arguments={"value": PATH})])
     self.deserializer = Deserializer(registry)
     self.resource_invocator = TestIntegratedResourceInvoker(
         self.serviceLocator, self.routingTable, self.deserializer)
     self.request = Request('GET', PATH, {}, "", "")
     ResourceClass.has_been_called = False
예제 #10
0
    def build_request(self, env: dict) -> Request:
        """Builds the Jivago request object from the WSGI environment dictionary."""
        raw_headers = Stream(env.items()).filter(
            lambda key, value: key.startswith("HTTP_")).map(
                lambda key, value: (re.sub("^HTTP_", "", key).title().replace(
                    "_", "-"), value)).toDict()
        request_size = int(
            env.get('CONTENT_LENGTH')) if 'CONTENT_LENGTH' in env else 0

        if 'CONTENT_TYPE' in env:
            raw_headers['Content-Type'] = env['CONTENT_TYPE']

        return Request(env['REQUEST_METHOD'], env['PATH_INFO'],
                       Headers(raw_headers), env['QUERY_STRING'],
                       env['wsgi.input'].read(request_size))
예제 #11
0
import unittest
from unittest import mock

from jivago.wsgi.filter.filter import Filter
from jivago.wsgi.filter.filter_chain import FilterChain
from jivago.wsgi.invocation.incorrect_resource_parameters_exception import IncorrectResourceParametersException
from jivago.wsgi.invocation.resource_invoker import ResourceInvoker
from jivago.wsgi.invocation.route_handler_factory import RouteHandlerFactory
from jivago.wsgi.request.request import Request
from jivago.wsgi.request.response import Response

A_REQUEST = Request('GET', "/path", {}, "", "")
A_RESPONSE = Response.empty()


class FilterChainTest(unittest.TestCase):
    def setUp(self):
        self.resourceInvokerFactoryMock: RouteHandlerFactory = mock.create_autospec(
            RouteHandlerFactory)
        self.resourceInvokerMock: ResourceInvoker = mock.create_autospec(
            ResourceInvoker)
        self.filterMock: Filter = mock.create_autospec(Filter)
        self.secondFilterMock: Filter = mock.create_autospec(Filter)
        self.filterChain = FilterChain(
            [self.filterMock, self.secondFilterMock],
            self.resourceInvokerFactoryMock)
        self.resourceInvokerFactoryMock.create_route_handlers.return_value = [
            self.resourceInvokerMock
        ]

    def test_givenEmptyFilterChain_whenApplyingFilterChain_thenInvokeResourceInvocator(
    def test_givenIncorrectParameters_whenInvoking_thenThrowIncorrectResourceParametersException(
            self):
        self.request = Request('POST', PATH + "/dto", {}, "", "")

        with self.assertRaises(IncorrectResourceParametersException):
            self.resource_invocator.invoke(self.request)
예제 #13
0
 def build(self):
     return Request(self._method, self._path, self._headers, self._queryString, self._body)
예제 #14
0
    def test_whenCreatingFilterChain_thenUseFiltersFromAllApplicableRules(self):
        filter_chain = self.filter_chain_factory.create_filter_chain(Request("GET", "/foo/bar/baz", {}, "", ""))

        self.assertEqual(2, len(filter_chain.filters))
        self.assertTrue(DEFAULT_FILTER in filter_chain.filters)
        self.assertTrue(SPECIFIC_FILTER in filter_chain.filters)