<s> from __future__ import unicode_literals <EOL> from flask import Blueprint , redirect , views , abort as flask_abort <EOL> from flask . signals import got_request_exception , signals_available <EOL> import flask <EOL> from werkzeug . exceptions import HTTPException , Unauthorized , BadRequest , NotFound <EOL> from werkzeug . http import quote_etag , unquote_etag <EOL> import flask_restplus as restplus <EOL> from flask_restplus . _compat import OrderedDict <EOL> from json import dumps , loads , JSONEncoder <EOL> from nose . tools import assert_equals , assert_true , assert_false <EOL> import six <EOL> from . . import TestCase , Mock <EOL> class HelloWorld ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return { } <EOL> class APITestCase ( TestCase ) : <EOL> def test_unauthorized_no_challenge_by_default ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> response = Mock ( ) <EOL> response . headers = { } <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> response = api . unauthorized ( response ) <EOL> assert_false ( '<STR_LIT>' in response . headers ) <EOL> def test_unauthorized ( self ) : <EOL> api = restplus . Api ( self . app , serve_challenge_on_401 = True ) <EOL> response = Mock ( ) <EOL> response . headers = { } <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> response = api . unauthorized ( response ) <EOL> self . assertEqual ( response . headers [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_unauthorized_custom_realm ( self ) : <EOL> self . app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> api = restplus . Api ( self . app , serve_challenge_on_401 = True ) <EOL> response = Mock ( ) <EOL> response . headers = { } <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> response = api . unauthorized ( response ) <EOL> self . assertEqual ( response . headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_handle_error_401_no_challenge_by_default ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> resp = api . handle_error ( Unauthorized ( ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> assert_false ( '<STR_LIT>' in resp . headers ) <EOL> def test_handle_error_401_sends_challege_default_realm ( self ) : <EOL> api = restplus . Api ( self . app , serve_challenge_on_401 = True ) <EOL> exception = HTTPException ( ) <EOL> exception . code = <NUM_LIT> <EOL> exception . data = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> resp = api . handle_error ( exception ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . headers [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_handle_error_401_sends_challege_configured_realm ( self ) : <EOL> self . app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> api = restplus . Api ( self . app , serve_challenge_on_401 = True ) <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> resp = api . handle_error ( Unauthorized ( ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . headers [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_handle_error_does_not_swallow_exceptions ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> exception = BadRequest ( '<STR_LIT:x>' ) <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> resp = api . handle_error ( exception ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . get_data ( ) , b'<STR_LIT>' ) <EOL> def test_marshal ( self ) : <EOL> fields = OrderedDict ( [ ( '<STR_LIT:foo>' , restplus . fields . Raw ) ] ) <EOL> marshal_dict = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> output = restplus . marshal ( marshal_dict , fields ) <EOL> self . assertEqual ( output , { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> def test_marshal_with_envelope ( self ) : <EOL> fields = OrderedDict ( [ ( '<STR_LIT:foo>' , restplus . fields . Raw ) ] ) <EOL> marshal_dict = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> output = restplus . marshal ( marshal_dict , fields , envelope = '<STR_LIT>' ) <EOL> self . assertEqual ( output , { '<STR_LIT>' : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } } ) <EOL> def test_marshal_decorator ( self ) : <EOL> fields = OrderedDict ( [ ( '<STR_LIT:foo>' , restplus . fields . Raw ) ] ) <EOL> @ restplus . marshal_with ( fields ) <EOL> def try_me ( ) : <EOL> return OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( try_me ( ) , { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> def test_marshal_decorator_with_envelope ( self ) : <EOL> fields = OrderedDict ( [ ( '<STR_LIT:foo>' , restplus . fields . Raw ) ] ) <EOL> @ restplus . marshal_with ( fields , envelope = '<STR_LIT>' ) <EOL> def try_me ( ) : <EOL> return OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( try_me ( ) , { '<STR_LIT>' : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } } ) <EOL> def test_marshal_decorator_tuple ( self ) : <EOL> fields = OrderedDict ( [ ( '<STR_LIT:foo>' , restplus . fields . Raw ) ] ) <EOL> @ restplus . marshal_with ( fields ) <EOL> def try_me ( ) : <EOL> return OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) , <NUM_LIT:200> , { '<STR_LIT>' : <NUM_LIT> } <EOL> self . assertEqual ( try_me ( ) , ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <NUM_LIT:200> , { '<STR_LIT>' : <NUM_LIT> } ) ) <EOL> def test_marshal_decorator_tuple_with_envelope ( self ) : <EOL> fields = OrderedDict ( [ ( '<STR_LIT:foo>' , restplus . fields . Raw ) ] ) <EOL> @ restplus . marshal_with ( fields , envelope = '<STR_LIT>' ) <EOL> def try_me ( ) : <EOL> return OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) , <NUM_LIT:200> , { '<STR_LIT>' : <NUM_LIT> } <EOL> self . assertEqual ( try_me ( ) , ( { '<STR_LIT>' : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } } , <NUM_LIT:200> , { '<STR_LIT>' : <NUM_LIT> } ) ) <EOL> def test_marshal_field_decorator ( self ) : <EOL> field = restplus . fields . Raw <EOL> @ restplus . marshal_with_field ( field ) <EOL> def try_me ( ) : <EOL> return '<STR_LIT:foo>' <EOL> self . assertEqual ( try_me ( ) , '<STR_LIT:foo>' ) <EOL> def test_marshal_field_decorator_tuple ( self ) : <EOL> field = restplus . fields . Raw <EOL> @ restplus . marshal_with_field ( field ) <EOL> def try_me ( ) : <EOL> return '<STR_LIT:foo>' , <NUM_LIT:200> , { '<STR_LIT>' : <NUM_LIT> } <EOL> self . assertEqual ( ( '<STR_LIT:foo>' , <NUM_LIT:200> , { '<STR_LIT>' : <NUM_LIT> } ) , try_me ( ) ) <EOL> def test_marshal_field ( self ) : <EOL> fields = OrderedDict ( { '<STR_LIT:foo>' : restplus . fields . Raw ( ) } ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> self . assertEqual ( output , { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> def test_marshal_tuple ( self ) : <EOL> fields = OrderedDict ( { '<STR_LIT:foo>' : restplus . fields . Raw } ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> output = restplus . marshal ( ( marshal_fields , ) , fields ) <EOL> self . assertEqual ( output , [ { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ] ) <EOL> def test_marshal_tuple_with_envelope ( self ) : <EOL> fields = OrderedDict ( { '<STR_LIT:foo>' : restplus . fields . Raw } ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> output = restplus . marshal ( ( marshal_fields , ) , fields , envelope = '<STR_LIT>' ) <EOL> self . assertEqual ( output , { '<STR_LIT>' : [ { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ] } ) <EOL> def test_marshal_nested ( self ) : <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT>' , restplus . fields . Nested ( { <EOL> '<STR_LIT>' : restplus . fields . String , <EOL> } ) ) <EOL> ] ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) ] ) <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> expected = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , OrderedDict ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) ] ) <EOL> self . assertEqual ( output , expected ) <EOL> def test_marshal_nested_with_non_null ( self ) : <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT>' , restplus . fields . Nested ( <EOL> OrderedDict ( [ <EOL> ( '<STR_LIT>' , restplus . fields . String ) , <EOL> ( '<STR_LIT>' , restplus . fields . String ) <EOL> ] ) , allow_null = False ) ) <EOL> ] ) <EOL> marshal_fields = [ OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , None ) ] ) ] <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> expected = [ OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , OrderedDict ( [ ( '<STR_LIT>' , None ) , ( '<STR_LIT>' , None ) ] ) ) ] ) ] <EOL> self . assertEqual ( output , expected ) <EOL> def test_marshal_nested_with_null ( self ) : <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT>' , restplus . fields . Nested ( <EOL> OrderedDict ( [ <EOL> ( '<STR_LIT>' , restplus . fields . String ) , <EOL> ( '<STR_LIT>' , restplus . fields . String ) <EOL> ] ) , allow_null = True ) ) <EOL> ] ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , None ) ] ) <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> expected = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , None ) ] ) <EOL> self . assertEqual ( output , expected ) <EOL> def test_allow_null_presents_data ( self ) : <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT>' , restplus . fields . Nested ( <EOL> OrderedDict ( [ <EOL> ( '<STR_LIT>' , restplus . fields . String ) , <EOL> ( '<STR_LIT>' , restplus . fields . String ) <EOL> ] ) , allow_null = True ) ) <EOL> ] ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) ] ) <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> expected = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , OrderedDict ( [ ( '<STR_LIT>' , None ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) ] ) <EOL> self . assertEqual ( output , expected ) <EOL> def test_marshal_nested_property ( self ) : <EOL> class TestObject ( object ) : <EOL> @ property <EOL> def fee ( self ) : <EOL> return { '<STR_LIT>' : '<STR_LIT>' } <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT>' , restplus . fields . Nested ( <EOL> OrderedDict ( [ <EOL> ( '<STR_LIT>' , restplus . fields . String ) , <EOL> ( '<STR_LIT>' , restplus . fields . String ) <EOL> ] ) , allow_null = True ) ) <EOL> ] ) <EOL> obj = TestObject ( ) <EOL> obj . foo = '<STR_LIT:bar>' <EOL> obj . bat = '<STR_LIT>' <EOL> output = restplus . marshal ( [ obj ] , fields ) <EOL> expected = [ OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , OrderedDict ( [ ( '<STR_LIT>' , None ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) ] ) ] <EOL> self . assertEqual ( output , expected ) <EOL> def test_marshal_list ( self ) : <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT>' , restplus . fields . List ( restplus . fields . String ) ) <EOL> ] ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) ] ) <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> expected = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) ] ) <EOL> self . assertEqual ( output , expected ) <EOL> def test_marshal_list_of_nesteds ( self ) : <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT>' , restplus . fields . List ( restplus . fields . Nested ( { <EOL> '<STR_LIT>' : restplus . fields . String <EOL> } ) ) ) <EOL> ] ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) ] ) <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> expected = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , [ OrderedDict ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ] ) ] ) <EOL> self . assertEqual ( output , expected ) <EOL> def test_marshal_list_of_lists ( self ) : <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT>' , restplus . fields . List ( restplus . fields . List ( <EOL> restplus . fields . String ) ) ) <EOL> ] ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , [ [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ] ) ] ) <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> expected = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , [ [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ] ) ] ) <EOL> self . assertEqual ( output , expected ) <EOL> def test_marshal_nested_dict ( self ) : <EOL> fields = OrderedDict ( [ <EOL> ( '<STR_LIT:foo>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT:bar>' , OrderedDict ( [ <EOL> ( '<STR_LIT:a>' , restplus . fields . Raw ) , <EOL> ( '<STR_LIT:b>' , restplus . fields . Raw ) , <EOL> ] ) ) , <EOL> ] ) <EOL> marshal_fields = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) , ( '<STR_LIT:bar>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) , ( '<STR_LIT:c>' , <NUM_LIT:3> ) ] ) <EOL> output = restplus . marshal ( marshal_fields , fields ) <EOL> expected = OrderedDict ( [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) , ( '<STR_LIT:bar>' , OrderedDict ( [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) ] ) ) ] ) <EOL> self . assertEqual ( output , expected ) <EOL> def test_api_representation ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> @ api . representation ( '<STR_LIT:foo>' ) <EOL> def foo ( ) : <EOL> pass <EOL> self . assertEqual ( api . representations [ '<STR_LIT:foo>' ] , foo ) <EOL> def test_api_base ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> self . assertEqual ( api . urls , { } ) <EOL> self . assertEqual ( api . prefix , '<STR_LIT>' ) <EOL> self . assertEqual ( api . default_mediatype , '<STR_LIT:application/json>' ) <EOL> def test_api_delayed_initialization ( self ) : <EOL> api = restplus . Api ( ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT:/>' , endpoint = "<STR_LIT:hello>" ) <EOL> api . init_app ( self . app ) <EOL> with self . app . test_client ( ) as client : <EOL> self . assertEqual ( client . get ( '<STR_LIT:/>' ) . status_code , <NUM_LIT:200> ) <EOL> def test_api_prefix ( self ) : <EOL> api = restplus . Api ( self . app , prefix = '<STR_LIT>' ) <EOL> self . assertEqual ( api . prefix , '<STR_LIT>' ) <EOL> def test_handle_server_error ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resp = api . handle_error ( Exception ( ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . data . decode ( ) , dumps ( { <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } ) + "<STR_LIT:\n>" ) <EOL> def test_handle_error_with_code ( self ) : <EOL> api = restplus . Api ( self . app , serve_challenge_on_401 = True ) <EOL> exception = Exception ( ) <EOL> exception . code = "<STR_LIT>" <EOL> exception . data = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resp = api . handle_error ( exception ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . data . decode ( ) , dumps ( { "<STR_LIT:foo>" : "<STR_LIT:bar>" } ) + "<STR_LIT:\n>" ) <EOL> def test_handle_auth ( self ) : <EOL> api = restplus . Api ( self . app , serve_challenge_on_401 = True ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resp = api . handle_error ( Unauthorized ( ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> expected_data = dumps ( { '<STR_LIT:message>' : Unauthorized . description } ) + "<STR_LIT:\n>" <EOL> self . assertEqual ( resp . data . decode ( ) , expected_data ) <EOL> self . assertTrue ( '<STR_LIT>' in resp . headers ) <EOL> def test_handle_api_error ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> class Test ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> flask . abort ( <NUM_LIT> ) <EOL> api . add_resource ( Test ( ) , '<STR_LIT>' , endpoint = '<STR_LIT>' ) <EOL> resp = self . get ( "<STR_LIT>" ) <EOL> assert_equals ( resp . status_code , <NUM_LIT> ) <EOL> assert_equals ( '<STR_LIT:application/json>' , resp . headers [ '<STR_LIT:Content-Type>' ] ) <EOL> data = loads ( resp . data . decode ( ) ) <EOL> assert_true ( '<STR_LIT:message>' in data ) <EOL> def test_handle_non_api_error ( self ) : <EOL> restplus . Api ( self . app ) <EOL> resp = self . get ( "<STR_LIT>" ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( '<STR_LIT>' , resp . headers [ '<STR_LIT:Content-Type>' ] ) <EOL> def test_non_api_error_404_catchall ( self ) : <EOL> api = restplus . Api ( self . app , catch_all_404s = True ) <EOL> resp = self . get ( "<STR_LIT>" ) <EOL> self . assertEqual ( api . default_mediatype , resp . headers [ '<STR_LIT:Content-Type>' ] ) <EOL> def test_handle_error_signal ( self ) : <EOL> if not signals_available : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> api = restplus . Api ( self . app ) <EOL> exception = BadRequest ( ) <EOL> recorded = [ ] <EOL> def record ( sender , exception ) : <EOL> recorded . append ( exception ) <EOL> got_request_exception . connect ( record , self . app ) <EOL> try : <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> api . handle_error ( exception ) <EOL> self . assertEqual ( len ( recorded ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( exception is recorded [ <NUM_LIT:0> ] ) <EOL> finally : <EOL> got_request_exception . disconnect ( record , self . app ) <EOL> def test_handle_error ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resp = api . handle_error ( BadRequest ( ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . data . decode ( ) , dumps ( { <EOL> '<STR_LIT:message>' : BadRequest . description , <EOL> } ) + "<STR_LIT:\n>" ) <EOL> def test_handle_smart_errors ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> view = restplus . Resource <EOL> api . add_resource ( view , '<STR_LIT>' , endpoint = '<STR_LIT>' ) <EOL> api . add_resource ( view , '<STR_LIT>' , endpoint = '<STR_LIT>' ) <EOL> api . add_resource ( view , '<STR_LIT>' , endpoint = '<STR_LIT>' ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resp = api . handle_error ( NotFound ( ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . data . decode ( ) , dumps ( { <EOL> "<STR_LIT:message>" : NotFound . description , <EOL> } ) + "<STR_LIT:\n>" ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resp = api . handle_error ( NotFound ( ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( '<STR_LIT>' in resp . data . decode ( ) ) <EOL> self . app . config [ '<STR_LIT>' ] = False <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resp = api . handle_error ( NotFound ( ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . data . decode ( ) , dumps ( { <EOL> "<STR_LIT:message>" : NotFound . description <EOL> } ) + "<STR_LIT:\n>" ) <EOL> def test_error_router_falls_back_to_original ( self ) : <EOL> """<STR_LIT>""" <EOL> api = restplus . Api ( self . app ) <EOL> self . app . handle_exception = Mock ( ) <EOL> api . handle_error = Mock ( side_effect = Exception ( ) ) <EOL> api . _has_fr_route = Mock ( return_value = True ) <EOL> exception = Mock ( spec = HTTPException ) <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> api . error_router ( exception , self . app . handle_exception ) <EOL> self . assertTrue ( self . app . handle_exception . called_with ( exception ) ) <EOL> def test_media_types ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( "<STR_LIT>" , headers = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' <EOL> } ) : <EOL> self . assertEqual ( api . mediatypes ( ) , [ '<STR_LIT:application/json>' ] ) <EOL> def test_media_types_method ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( "<STR_LIT>" , headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) : <EOL> self . assertEqual ( api . mediatypes_method ( ) ( Mock ( ) ) , <EOL> [ '<STR_LIT>' , '<STR_LIT:application/json>' ] ) <EOL> def test_media_types_q ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( "<STR_LIT>" , headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) : <EOL> self . assertEqual ( api . mediatypes ( ) , <EOL> [ '<STR_LIT:application/json>' , '<STR_LIT>' ] ) <EOL> def test_decorator ( self ) : <EOL> def return_zero ( func ) : <EOL> return <NUM_LIT:0> <EOL> app = Mock ( flask . Flask ) <EOL> app . view_functions = { } <EOL> app . extensions = { } <EOL> app . config = { } <EOL> view = Mock ( ) <EOL> api = restplus . Api ( app ) <EOL> api . decorators . append ( return_zero ) <EOL> api . output = Mock ( ) <EOL> api . add_resource ( view , '<STR_LIT>' , endpoint = '<STR_LIT:bar>' ) <EOL> app . add_url_rule . assert_called_with ( '<STR_LIT>' , view_func = <NUM_LIT:0> ) <EOL> def test_add_resource_endpoint ( self ) : <EOL> view = Mock ( ** { '<STR_LIT>' : str ( '<STR_LIT>' ) } ) <EOL> api = restplus . Api ( self . app ) <EOL> api . add_resource ( view , '<STR_LIT>' , endpoint = '<STR_LIT:bar>' ) <EOL> view . as_view . assert_called_with ( '<STR_LIT:bar>' , api ) <EOL> def test_add_two_conflicting_resources_on_same_endpoint ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> class Foo1 ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return '<STR_LIT>' <EOL> class Foo2 ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return '<STR_LIT>' <EOL> api . add_resource ( Foo1 , '<STR_LIT>' , endpoint = '<STR_LIT:bar>' ) <EOL> self . assertRaises ( ValueError , api . add_resource , Foo2 , '<STR_LIT>' , endpoint = '<STR_LIT:bar>' ) <EOL> def test_add_the_same_resource_on_same_endpoint ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> class Foo1 ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return '<STR_LIT>' <EOL> api . add_resource ( Foo1 , '<STR_LIT>' , endpoint = '<STR_LIT:bar>' ) <EOL> api . add_resource ( Foo1 , '<STR_LIT>' , endpoint = '<STR_LIT>' ) <EOL> with self . app . test_client ( ) as client : <EOL> foo1 = client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( foo1 . data , b'<STR_LIT>' ) <EOL> foo2 = client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( foo2 . data , b'<STR_LIT>' ) <EOL> def test_add_resource ( self ) : <EOL> app = Mock ( flask . Flask ) <EOL> app . view_functions = { } <EOL> app . extensions = { } <EOL> app . config = { } <EOL> api = restplus . Api ( app ) <EOL> api . output = Mock ( ) <EOL> api . add_resource ( views . MethodView , '<STR_LIT>' ) <EOL> app . add_url_rule . assert_called_with ( '<STR_LIT>' , <EOL> view_func = api . output ( ) ) <EOL> def test_add_resource_kwargs ( self ) : <EOL> app = Mock ( flask . Flask ) <EOL> app . view_functions = { } <EOL> app . extensions = { } <EOL> app . config = { } <EOL> api = restplus . Api ( app ) <EOL> api . output = Mock ( ) <EOL> api . add_resource ( views . MethodView , '<STR_LIT>' , defaults = { "<STR_LIT:bar>" : "<STR_LIT>" } ) <EOL> app . add_url_rule . assert_called_with ( '<STR_LIT>' , <EOL> view_func = api . output ( ) , <EOL> defaults = { "<STR_LIT:bar>" : "<STR_LIT>" } ) <EOL> def test_add_resource_forward_resource_class_parameters ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> class Foo ( restplus . Resource ) : <EOL> def __init__ ( self , api , * args , ** kwargs ) : <EOL> self . one = args [ <NUM_LIT:0> ] <EOL> self . two = kwargs [ '<STR_LIT>' ] <EOL> super ( Foo , self ) . __init__ ( api , * args , ** kwargs ) <EOL> def get ( self ) : <EOL> return "<STR_LIT>" . format ( self . one , self . two ) <EOL> api . add_resource ( Foo , '<STR_LIT>' , <EOL> resource_class_args = ( '<STR_LIT>' , ) , <EOL> resource_class_kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> foo = self . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( foo . data , b'<STR_LIT>' ) <EOL> def test_output_unpack ( self ) : <EOL> def make_empty_response ( ) : <EOL> return { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> wrapper = api . output ( make_empty_response ) <EOL> resp = wrapper ( ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data . decode ( ) , '<STR_LIT>' ) <EOL> def test_output_func ( self ) : <EOL> def make_empty_resposne ( ) : <EOL> return flask . make_response ( '<STR_LIT>' ) <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> wrapper = api . output ( make_empty_resposne ) <EOL> resp = wrapper ( ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data . decode ( ) , '<STR_LIT>' ) <EOL> def test_resource ( self ) : <EOL> resource = restplus . Resource ( ) <EOL> resource . get = Mock ( ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resource . dispatch_request ( ) <EOL> def test_resource_resp ( self ) : <EOL> resource = restplus . Resource ( ) <EOL> resource . get = Mock ( ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> resource . get . return_value = flask . make_response ( '<STR_LIT>' ) <EOL> resource . dispatch_request ( ) <EOL> def test_resource_text_plain ( self ) : <EOL> def text ( data , code , headers = None ) : <EOL> return flask . make_response ( six . text_type ( data ) ) <EOL> class Foo ( restplus . Resource ) : <EOL> representations = { <EOL> '<STR_LIT>' : text , <EOL> } <EOL> def get ( self ) : <EOL> return '<STR_LIT:hello>' <EOL> with self . app . test_request_context ( "<STR_LIT>" , headers = { '<STR_LIT>' : '<STR_LIT>' } ) : <EOL> resource = Foo ( None ) <EOL> resp = resource . dispatch_request ( ) <EOL> self . assertEqual ( resp . data . decode ( ) , '<STR_LIT:hello>' ) <EOL> def test_resource_error ( self ) : <EOL> resource = restplus . Resource ( ) <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> self . assertRaises ( AssertionError , lambda : resource . dispatch_request ( ) ) <EOL> def test_resource_head ( self ) : <EOL> resource = restplus . Resource ( ) <EOL> with self . app . test_request_context ( "<STR_LIT>" , method = "<STR_LIT>" ) : <EOL> self . assertRaises ( AssertionError , lambda : resource . dispatch_request ( ) ) <EOL> def test_abort_data ( self ) : <EOL> try : <EOL> restplus . abort ( <NUM_LIT> , foo = '<STR_LIT:bar>' ) <EOL> assert False <EOL> except Exception as e : <EOL> self . assertEqual ( e . data , { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> def test_abort_no_data ( self ) : <EOL> try : <EOL> restplus . abort ( <NUM_LIT> ) <EOL> assert False <EOL> except Exception as e : <EOL> self . assertEqual ( False , hasattr ( e , "<STR_LIT:data>" ) ) <EOL> def test_abort_custom_message ( self ) : <EOL> try : <EOL> restplus . abort ( <NUM_LIT> , message = "<STR_LIT>" ) <EOL> assert False <EOL> except Exception as e : <EOL> assert_equals ( e . data [ '<STR_LIT:message>' ] , "<STR_LIT>" ) <EOL> def test_abort_type ( self ) : <EOL> self . assertRaises ( HTTPException , lambda : restplus . abort ( <NUM_LIT> ) ) <EOL> def test_endpoints ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' , endpoint = "<STR_LIT:hello>" ) <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertFalse ( api . _has_fr_route ( ) ) <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertTrue ( api . _has_fr_route ( ) ) <EOL> def test_url_for ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' ) <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertEqual ( api . url_for ( HelloWorld , id = <NUM_LIT> ) , '<STR_LIT>' ) <EOL> def test_url_for_with_blueprint ( self ) : <EOL> """<STR_LIT>""" <EOL> api_bp = Blueprint ( '<STR_LIT>' , __name__ ) <EOL> api = restplus . Api ( api_bp ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' ) <EOL> self . app . register_blueprint ( api_bp ) <EOL> with self . app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertEqual ( api . url_for ( HelloWorld , bar = '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_fr_405 ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' , endpoint = "<STR_LIT:hello>" ) <EOL> resp = self . post ( '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . content_type , api . default_mediatype ) <EOL> allow = '<STR_LIT:U+002CU+0020>' . join ( set ( resp . headers . get_all ( '<STR_LIT>' ) ) ) <EOL> allow = set ( method . strip ( ) for method in allow . split ( '<STR_LIT:U+002C>' ) ) <EOL> self . assertEqual ( allow , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' ] + HelloWorld . methods ) ) <EOL> def test_exception_header_forwarded ( self ) : <EOL> """<STR_LIT>""" <EOL> self . app . config [ '<STR_LIT>' ] = True <EOL> api = restplus . Api ( self . app ) <EOL> class NotModified ( HTTPException ) : <EOL> code = <NUM_LIT> <EOL> def __init__ ( self , etag , * args , ** kwargs ) : <EOL> super ( NotModified , self ) . __init__ ( * args , ** kwargs ) <EOL> self . etag = quote_etag ( etag ) <EOL> def get_headers ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return [ ( '<STR_LIT>' , self . etag ) ] <EOL> class Foo1 ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> flask_abort ( <NUM_LIT> , etag = '<STR_LIT>' ) <EOL> api . add_resource ( Foo1 , '<STR_LIT>' ) <EOL> flask_abort . mapping . update ( { <NUM_LIT> : NotModified } ) <EOL> with self . app . test_client ( ) as client : <EOL> foo = client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( foo . get_etag ( ) , <EOL> unquote_etag ( quote_etag ( '<STR_LIT>' ) ) ) <EOL> def test_exception_header_forwarding_doesnt_duplicate_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> api = restplus . Api ( self . app ) <EOL> with self . app . test_request_context ( '<STR_LIT:/>' ) : <EOL> r = api . handle_error ( BadRequest ( ) ) <EOL> self . assertEqual ( len ( r . headers . getlist ( '<STR_LIT>' ) ) , <NUM_LIT:1> ) <EOL> def test_will_prettyprint_json_in_debug_mode ( self ) : <EOL> self . app . config [ '<STR_LIT>' ] = True <EOL> api = restplus . Api ( self . app ) <EOL> class Foo1 ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> api . add_resource ( Foo1 , '<STR_LIT>' , endpoint = '<STR_LIT:bar>' ) <EOL> with self . app . test_client ( ) as client : <EOL> foo = client . get ( '<STR_LIT>' ) <EOL> lines = foo . data . splitlines ( ) <EOL> lines = [ line . decode ( ) for line in lines ] <EOL> self . assertEqual ( "<STR_LIT:{>" , lines [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( lines [ <NUM_LIT:1> ] . startswith ( '<STR_LIT:U+0020>' ) ) <EOL> self . assertTrue ( lines [ <NUM_LIT:2> ] . startswith ( '<STR_LIT:U+0020>' ) ) <EOL> self . assertEqual ( "<STR_LIT:}>" , lines [ <NUM_LIT:3> ] ) <EOL> self . assertTrue ( foo . data . endswith ( b'<STR_LIT:\n>' ) ) <EOL> def test_read_json_settings_from_config ( self ) : <EOL> class TestConfig ( object ) : <EOL> RESTPLUS_JSON = { '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : ( '<STR_LIT:U+002CU+0020>' , '<STR_LIT>' ) } <EOL> self . app . config . from_object ( TestConfig ) <EOL> api = restplus . Api ( self . app ) <EOL> class Foo ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> api . add_resource ( Foo , '<STR_LIT>' ) <EOL> with self . app . test_client ( ) as client : <EOL> data = client . get ( '<STR_LIT>' ) . data <EOL> expected = b'<STR_LIT>' <EOL> self . assertEqual ( data , expected ) <EOL> def test_use_custom_jsonencoder ( self ) : <EOL> class CabageEncoder ( JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> return '<STR_LIT>' <EOL> class TestConfig ( object ) : <EOL> RESTPLUS_JSON = { '<STR_LIT>' : CabageEncoder } <EOL> self . app . config . from_object ( TestConfig ) <EOL> api = restplus . Api ( self . app ) <EOL> class Cabbage ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return { '<STR_LIT>' : object ( ) } <EOL> api . add_resource ( Cabbage , '<STR_LIT>' ) <EOL> with self . app . test_client ( ) as client : <EOL> data = client . get ( '<STR_LIT>' ) . data <EOL> expected = b'<STR_LIT>' <EOL> self . assertEqual ( data , expected ) <EOL> def test_json_with_no_settings ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> class Foo ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> api . add_resource ( Foo , '<STR_LIT>' ) <EOL> with self . app . test_client ( ) as client : <EOL> data = client . get ( '<STR_LIT>' ) . data <EOL> expected = b'<STR_LIT>' <EOL> self . assertEqual ( data , expected ) <EOL> def test_redirect ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> class FooResource ( restplus . Resource ) : <EOL> def get ( self ) : <EOL> return redirect ( '<STR_LIT:/>' ) <EOL> api . add_resource ( FooResource , '<STR_LIT>' ) <EOL> resp = self . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_json_float_marshalled ( self ) : <EOL> api = restplus . Api ( self . app ) <EOL> class FooResource ( restplus . Resource ) : <EOL> fields = { '<STR_LIT:foo>' : restplus . fields . Float } <EOL> def get ( self ) : <EOL> return restplus . marshal ( { "<STR_LIT:foo>" : <NUM_LIT> } , self . fields ) <EOL> api . add_resource ( FooResource , '<STR_LIT>' ) <EOL> resp = self . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data . decode ( '<STR_LIT:utf-8>' ) , '<STR_LIT>' ) <EOL> def test_calling_owns_endpoint_before_api_init ( self ) : <EOL> api = restplus . Api ( ) <EOL> try : <EOL> api . owns_endpoint ( '<STR_LIT>' ) <EOL> except AttributeError as ae : <EOL> self . fail ( ae . message ) </s>
