def content_type_providers(self): """ Returns a dictionary of content type providers, key is a string of the content type, i.e, `application/json` and the value is a function that returns a response for that type. """ def json_provider(deserializer, content_type): def inner(): content = json.dumps(deserializer(self)) return Response( content, content_type='{}; charset=utf8'.format(content_type)) return inner return { 'application/json': json_provider(deserialize_json, 'application/json'), 'application/hal+json': json_provider(deserialize_hal, 'application/hal+json'), 'application/vnd.siren+json': json_provider(deserialize_siren, 'application/vnd.siren+json'), 'text/html': lambda: Response(deserialize_html(self)), }
def test_process_response_unsets_cookie(middleware, jwt): response = Response() response.jwt_cookie = None response = middleware.process_response(Request(), response) morsel = response.cookies['jwt'] assert morsel.value == '' assert morsel['expires'] == 'Thu, 01-Jan-1970 00:00:00 GMT'
def test_process_request_calls_401_for_invalid_token(): jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJleGFtcGxlLmNvbSJ9.INovSA2CyXeBwzR0Bqq-pFuxfQLVgnFpN4x1JP0Ve84' middleware = JWTMiddleware(key='secret', audience='prod.example.com') middleware.custom_401 = lambda r: Response('custom 401') request = Request(headers={'Authorization': 'Bearer {}'.format(jwt)}) response = middleware.process_request(request) assert response.content == 'custom 401' assert request.jwt == None
def status_view(request): status = { 'entrants': Entrant.select().count(), 'invited': Invitation.select().where( Invitation.state == Invitation.INVITED_STATE).count(), 'accepted': Invitation.select().where( Invitation.state == Invitation.ACCEPTED_STATE).count(), 'rejected': Invitation.select().where( Invitation.state == Invitation.REJECTED_STATE).count(), } return Response(json.dumps(status), content_type='application/json')
def test_process_response_encodes_cookie(middleware, jwt): response = Response() response.jwt_cookie = {'name': 'Kyle'} response = middleware.process_response(Request(), response) assert response.cookies['jwt'].value == jwt
def inner(): content = json.dumps(deserializer(self)) return Response( content, content_type='{}; charset=utf8'.format(content_type))