Exemple #1
0
    def handle(self, method, request):
        """Handle a request

        :param method: string - HTTP Verb
        :param request: aiohttp.client_reqrep.ClientRequest

        :returns: aiohttp.client_reqrep.ClientResponse

        Note: Returns an HTTP 405 if the HTTP Verb is not
              supported
        """

        # If the method has a registered handler, then
        # return it. Otherwise, create a 405 response
        if method in self._method_handlers:
            handler = self._method_handlers[method]
            # Callbacks must be callables
            if hasattr(handler, '__call__'):
                return self._method_handlers[method](_request)
            else:
                return handler

        else:
            response = ClientResponse(method, self.uri, host='aiohttp_mock')
            response.status = 405
            response.reason = 'Method Not Supported'
            response._should_close = False
            response._headers = cidict({
                'x-agent': 'aiohttp-mock',
                'content-length': 0
            })
            return response
    def make_response(uri, method, status_code=200, body=None, add_headers=None):
        """Utility function for building a ClientResponse object

        :param uri: string - URI of the request being handled
        :param method: string - HTTP verb being responded to
        :param status_code: int - HTTP Response Status Code
        :param body: bytes - HTTP Response Body Data
        :param add_headers: dict - HTTP Response Headers

        :returns: ClientResponse instance
        """
        content_length = 0
        if body:
            content_length = len(body)

        response = ClientResponse(method, uri, host='aiohttp_mock')
        response.status = status_code
        response.reason = ConnectionManager.get_reason_for_status(status_code)
        response._should_code = False
        response._headers = cidict({
            'x-agent': 'aiohttp-mock',
            'content-length': content_length
        })
        return response