Ejemplo n.º 1
0
class RefgetRequestHandler(tornado.web.RequestHandler):
    """Generic request handler for all refget-related requests

    All refget request handlers must be initialized with the runtime properties,
    a generic request object modified from the Tornado-specific request object,
    and an empty refget response object that will be modified by the method.

    Attributes:
        properties (Properties): runtime properties imported from server
        g_request (Request): generic refget request created from Tornado request
        g_response (Response): generic refget response modified by route methods
    """
    def initialize(self, properties):
        """Initialize the request handler by loading server Properties"""

        self.properties = properties
        self.g_request = None
        self.g_response = None

    def prepare(self):
        """Instantiate the generic request and response passed to refget func

        the generic request is instantiated by converting the Tornado-specific
        request object, so that it will be compatible with refget functions
        """

        self.g_request = self.get_generic_request()
        self.g_response = Response()

    def get_generic_request(self):
        """Instantiate a generic refget request from Tornado request object

        Returns:
            (Request): generic refget request, compatible with refget functions
        """

        g_request = Request()
        for key in self.path_kwargs.keys():
            g_request.add_path_param(key, self.path_kwargs[key])
        for key in self.request.query_arguments:
            g_request.add_query_param(key, self.get_query_argument(key))
        for key, value in self.request.headers.get_all():
            g_request.add_header(key, value)
        return g_request

    def finalize_response(self):
        """Converts the completed generic response to Tornado-specific response

        The generic response is updated by the refget functions. This method
        writes the generic response as a Tornado-specific response, so that the
        server will return valid responses in the Tornado runtime context.
        """

        self.set_status(self.g_response.get_status_code())
        headers = self.g_response.get_headers()
        for key in headers.keys():
            self.set_header(key, headers[key])
        self.write(self.g_response.get_body())
Ejemplo n.º 2
0
    def prepare(self):
        """Instantiate the generic request and response passed to refget func

        the generic request is instantiated by converting the Tornado-specific
        request object, so that it will be compatible with refget functions
        """

        self.g_request = self.get_generic_request()
        self.g_response = Response()
Ejemplo n.º 3
0
def test_data(key, value):
    response = Response()
    response.put_data(key, value)
    assert response.get_datum(key) == value
    assert response.get_data()[key] == value

    new_dict = {"dataA": "valueA", "dataB": "valueB"}
    response.update_data(new_dict)
    assert response.get_datum(key) == value
    assert response.get_data()[key] == value
Ejemplo n.º 4
0
def test_header(key, value):
    response = Response()
    response.put_header(key, value)
    assert response.get_header(key) == value
    assert response.get_headers()[key] == value

    new_dict = {"headerA": "valueA", "headerB": "valueB"}
    response.update_headers(new_dict)
    assert response.get_header(key) == value
    assert response.get_headers()[key] == value
Ejemplo n.º 5
0
def test_media_type_middleware(request_dict, exp_status_code):
    properties = Properties({})
    request = setup_request(request_dict)
    response = Response()

    @MediaTypeMidware(properties, request, response)
    def dummy_function(properties, request, response):
        return None

    dummy_function(properties, request, response)

    assert response.get_status_code() == exp_status_code
Ejemplo n.º 6
0
def get_response():
    """Instantiate a generic refget response

    Returns:
        (Response): generic refget response, compatible with refget functions
    """

    return Response()
Ejemplo n.º 7
0
def test_redirect(url):
    response = Response()
    response.set_redirect_found(url)
    assert response.get_status_code() == SC.REDIRECT_FOUND
    assert response.get_header("Location") == url
Ejemplo n.º 8
0
def test_status_code(status_code):
    response = Response()
    response.set_status_code(status_code)
    assert response.get_status_code() == status_code
Ejemplo n.º 9
0
def test_body(body):
    response = Response()
    response.set_body(body)
    assert response.get_body() == body
Ejemplo n.º 10
0
def setup_properties_request_response(props_dict, request_dict):
    return [
        setup_properties(props_dict),
        setup_request(request_dict),
        Response()
    ]