<s> import base64 <EOL> import datetime <EOL> import decimal <EOL> import wsme . tests . protocol <EOL> try : <EOL> import simplejson as json <EOL> except : <EOL> import json <EOL> from wsme . rest . json import fromjson , tojson , parse <EOL> from wsme . utils import parse_isodatetime , parse_isotime , parse_isodate <EOL> from wsme . types import isarray , isdict , isusertype , register_type <EOL> from wsme . types import UserType , ArrayType , DictType <EOL> from wsme . rest import expose , validate <EOL> from wsme . exc import ClientSideError , InvalidInput <EOL> import six <EOL> from six import b , u <EOL> if six . PY3 : <EOL> from urllib . parse import urlencode <EOL> else : <EOL> from urllib import urlencode <EOL> def prepare_value ( value , datatype ) : <EOL> if isinstance ( datatype , list ) : <EOL> return [ prepare_value ( item , datatype [ <NUM_LIT:0> ] ) for item in value ] <EOL> if isinstance ( datatype , dict ) : <EOL> key_type , value_type = list ( datatype . items ( ) ) [ <NUM_LIT:0> ] <EOL> return dict ( ( <EOL> ( prepare_value ( item [ <NUM_LIT:0> ] , key_type ) , <EOL> prepare_value ( item [ <NUM_LIT:1> ] , value_type ) ) <EOL> for item in value . items ( ) <EOL> ) ) <EOL> if datatype in ( datetime . date , datetime . time , datetime . datetime ) : <EOL> return value . isoformat ( ) <EOL> if datatype == decimal . Decimal : <EOL> return str ( value ) <EOL> if datatype == wsme . types . binary : <EOL> return base64 . encodestring ( value ) . decode ( '<STR_LIT:ascii>' ) <EOL> if datatype == wsme . types . bytes : <EOL> return value . decode ( '<STR_LIT:ascii>' ) <EOL> return value <EOL> def prepare_result ( value , datatype ) : <EOL> print ( value , datatype ) <EOL> if value is None : <EOL> return None <EOL> if datatype == wsme . types . binary : <EOL> return base64 . decodestring ( value . encode ( '<STR_LIT:ascii>' ) ) <EOL> if isusertype ( datatype ) : <EOL> datatype = datatype . basetype <EOL> if isinstance ( datatype , list ) : <EOL> return [ prepare_result ( item , datatype [ <NUM_LIT:0> ] ) for item in value ] <EOL> if isarray ( datatype ) : <EOL> return [ prepare_result ( item , datatype . item_type ) for item in value ] <EOL> if isinstance ( datatype , dict ) : <EOL> return dict ( ( <EOL> ( prepare_result ( item [ <NUM_LIT:0> ] , list ( datatype . keys ( ) ) [ <NUM_LIT:0> ] ) , <EOL> prepare_result ( item [ <NUM_LIT:1> ] , list ( datatype . values ( ) ) [ <NUM_LIT:0> ] ) ) <EOL> for item in value . items ( ) <EOL> ) ) <EOL> if isdict ( datatype ) : <EOL> return dict ( ( <EOL> ( prepare_result ( item [ <NUM_LIT:0> ] , datatype . key_type ) , <EOL> prepare_result ( item [ <NUM_LIT:1> ] , datatype . value_type ) ) <EOL> for item in value . items ( ) <EOL> ) ) <EOL> if datatype == datetime . date : <EOL> return parse_isodate ( value ) <EOL> if datatype == datetime . time : <EOL> return parse_isotime ( value ) <EOL> if datatype == datetime . datetime : <EOL> return parse_isodatetime ( value ) <EOL> if hasattr ( datatype , '<STR_LIT>' ) : <EOL> for attr in datatype . _wsme_attributes : <EOL> if attr . key not in value : <EOL> continue <EOL> value [ attr . key ] = prepare_result ( value [ attr . key ] , attr . datatype ) <EOL> return value <EOL> if datatype == wsme . types . bytes : <EOL> return value . encode ( '<STR_LIT:ascii>' ) <EOL> if type ( value ) != datatype : <EOL> print ( type ( value ) , datatype ) <EOL> return datatype ( value ) <EOL> return value <EOL> class CustomInt ( UserType ) : <EOL> basetype = int <EOL> name = "<STR_LIT>" <EOL> class Obj ( wsme . types . Base ) : <EOL> id = int <EOL> name = wsme . types . text <EOL> class NestedObj ( wsme . types . Base ) : <EOL> o = Obj <EOL> class CRUDResult ( object ) : <EOL> data = Obj <EOL> message = wsme . types . text <EOL> def __init__ ( self , data = wsme . types . Unset , message = wsme . types . Unset ) : <EOL> self . data = data <EOL> self . message = message <EOL> class MiniCrud ( object ) : <EOL> @ expose ( CRUDResult , method = '<STR_LIT>' ) <EOL> @ validate ( Obj ) <EOL> def create ( self , data ) : <EOL> print ( repr ( data ) ) <EOL> return CRUDResult ( data , u ( '<STR_LIT>' ) ) <EOL> @ expose ( CRUDResult , method = '<STR_LIT:GET>' , ignore_extra_args = True ) <EOL> @ validate ( Obj ) <EOL> def read ( self , ref ) : <EOL> print ( repr ( ref ) ) <EOL> if ref . id == <NUM_LIT:1> : <EOL> ref . name = u ( '<STR_LIT:test>' ) <EOL> return CRUDResult ( ref , u ( '<STR_LIT>' ) ) <EOL> @ expose ( CRUDResult , method = '<STR_LIT:POST>' ) <EOL> @ validate ( Obj ) <EOL> def update ( self , data ) : <EOL> print ( repr ( data ) ) <EOL> return CRUDResult ( data , u ( '<STR_LIT>' ) ) <EOL> @ expose ( CRUDResult , wsme . types . text , body = Obj ) <EOL> def update_with_body ( self , msg , data ) : <EOL> print ( repr ( data ) ) <EOL> return CRUDResult ( data , msg ) <EOL> @ expose ( CRUDResult , method = '<STR_LIT>' ) <EOL> @ validate ( Obj ) <EOL> def delete ( self , ref ) : <EOL> print ( repr ( ref ) ) <EOL> if ref . id == <NUM_LIT:1> : <EOL> ref . name = u ( '<STR_LIT:test>' ) <EOL> return CRUDResult ( ref , u ( '<STR_LIT>' ) ) <EOL> wsme . tests . protocol . WSTestRoot . crud = MiniCrud ( ) <EOL> class TestRestJson ( wsme . tests . protocol . RestOnlyProtocolTestCase ) : <EOL> protocol = '<STR_LIT>' <EOL> def call ( self , fpath , _rt = None , _accept = None , _no_result_decode = False , <EOL> body = None , ** kw ) : <EOL> if body : <EOL> if isinstance ( body , tuple ) : <EOL> body , datatype = body <EOL> else : <EOL> datatype = type ( body ) <EOL> body = prepare_value ( body , datatype ) <EOL> content = json . dumps ( body ) <EOL> else : <EOL> for key in kw : <EOL> if isinstance ( kw [ key ] , tuple ) : <EOL> value , datatype = kw [ key ] <EOL> else : <EOL> value = kw [ key ] <EOL> datatype = type ( value ) <EOL> kw [ key ] = prepare_value ( value , datatype ) <EOL> content = json . dumps ( kw ) <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } <EOL> if _accept is not None : <EOL> headers [ "<STR_LIT>" ] = _accept <EOL> res = self . app . post ( <EOL> '<STR_LIT:/>' + fpath , <EOL> content , <EOL> headers = headers , <EOL> expect_errors = True ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> if _no_result_decode : <EOL> return res <EOL> r = json . loads ( res . text ) <EOL> if res . status_int == <NUM_LIT:200> : <EOL> if _rt and r : <EOL> r = prepare_result ( r , _rt ) <EOL> return r <EOL> else : <EOL> raise wsme . tests . protocol . CallException ( <EOL> r [ '<STR_LIT>' ] , <EOL> r [ '<STR_LIT>' ] , <EOL> r . get ( '<STR_LIT>' ) <EOL> ) <EOL> return json . loads ( res . text ) <EOL> def test_fromjson ( self ) : <EOL> assert fromjson ( str , None ) is None <EOL> def test_keyargs ( self ) : <EOL> r = self . app . get ( '<STR_LIT>' ) <EOL> print ( r ) <EOL> assert json . loads ( r . text ) == <NUM_LIT:2> <EOL> nestedarray = '<STR_LIT>' <EOL> r = self . app . get ( '<STR_LIT>' + nestedarray ) <EOL> print ( r ) <EOL> assert json . loads ( r . text ) == [ <EOL> { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } } , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } } ] <EOL> def test_form_urlencoded_args ( self ) : <EOL> params = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } <EOL> body = urlencode ( params ) <EOL> r = self . app . post ( <EOL> '<STR_LIT>' , <EOL> body , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> ) <EOL> print ( r ) <EOL> assert json . loads ( r . text ) == [ <EOL> { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } } , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } } ] <EOL> def test_body_and_params ( self ) : <EOL> r = self . app . post ( '<STR_LIT>' , '<STR_LIT>' , <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } , <EOL> expect_errors = True ) <EOL> print ( r ) <EOL> assert r . status_int == <NUM_LIT> <EOL> assert json . loads ( r . text ) [ '<STR_LIT>' ] == "<STR_LIT>" <EOL> def test_inline_body ( self ) : <EOL> params = urlencode ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> r = self . app . get ( '<STR_LIT>' + params ) <EOL> print ( r ) <EOL> assert json . loads ( r . text ) == <NUM_LIT:4> <EOL> def test_empty_body ( self ) : <EOL> params = urlencode ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> r = self . app . get ( '<STR_LIT>' + params ) <EOL> print ( r ) <EOL> assert json . loads ( r . text ) == <NUM_LIT:2> <EOL> def test_invalid_content_type_body ( self ) : <EOL> r = self . app . post ( '<STR_LIT>' , '<STR_LIT>' , <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT>" } , <EOL> expect_errors = True ) <EOL> print ( r ) <EOL> assert r . status_int == <NUM_LIT> <EOL> assert json . loads ( r . text ) [ '<STR_LIT>' ] == "<STR_LIT>" <EOL> def test_invalid_json_body ( self ) : <EOL> r = self . app . post ( '<STR_LIT>' , '<STR_LIT>' , <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } , <EOL> expect_errors = True ) <EOL> print ( r ) <EOL> assert r . status_int == <NUM_LIT> <EOL> assert json . loads ( r . text ) [ '<STR_LIT>' ] == "<STR_LIT>" <EOL> def test_unknown_arg ( self ) : <EOL> r = self . app . post ( '<STR_LIT>' , '<STR_LIT>' , <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } , <EOL> expect_errors = True ) <EOL> print ( r ) <EOL> assert r . status_int == <NUM_LIT> <EOL> assert json . loads ( r . text ) [ '<STR_LIT>' ] . startswith ( <EOL> "<STR_LIT>" <EOL> ) <EOL> r = self . app . get ( '<STR_LIT>' , expect_errors = True ) <EOL> print ( r ) <EOL> assert r . status_int == <NUM_LIT> <EOL> assert json . loads ( r . text ) [ '<STR_LIT>' ] . startswith ( <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_set_custom_object ( self ) : <EOL> r = self . app . post ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } <EOL> ) <EOL> self . assertEqual ( r . status_int , <NUM_LIT:200> ) <EOL> self . assertEqual ( r . json , { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT:test>' } ) <EOL> def test_set_extended_int ( self ) : <EOL> r = self . app . post ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } <EOL> ) <EOL> self . assertEqual ( r . status_int , <NUM_LIT:200> ) <EOL> self . assertEqual ( r . json , <NUM_LIT:3> ) <EOL> def test_unset_attrs ( self ) : <EOL> class AType ( object ) : <EOL> attr = int <EOL> wsme . types . register_type ( AType ) <EOL> j = tojson ( AType , AType ( ) ) <EOL> assert j == { } <EOL> def test_array_tojson ( self ) : <EOL> assert tojson ( [ int ] , None ) is None <EOL> assert tojson ( [ int ] , [ ] ) == [ ] <EOL> assert tojson ( [ str ] , [ '<STR_LIT:1>' , '<STR_LIT:4>' ] ) == [ '<STR_LIT:1>' , '<STR_LIT:4>' ] <EOL> def test_dict_tojson ( self ) : <EOL> assert tojson ( { int : str } , None ) is None <EOL> assert tojson ( { int : str } , { <NUM_LIT:5> : '<STR_LIT:5>' } ) == { <NUM_LIT:5> : '<STR_LIT:5>' } <EOL> def test_None_tojson ( self ) : <EOL> for dt in ( datetime . date , datetime . time , datetime . datetime , <EOL> decimal . Decimal ) : <EOL> assert tojson ( dt , None ) is None <EOL> def test_None_fromjson ( self ) : <EOL> for dt in ( str , int , datetime . date , datetime . time , datetime . datetime , <EOL> decimal . Decimal , [ int ] , { int : int } ) : <EOL> assert fromjson ( dt , None ) is None <EOL> def test_parse_valid_date ( self ) : <EOL> j = parse ( '<STR_LIT>' , { '<STR_LIT:a>' : datetime . date } , False ) <EOL> assert isinstance ( j [ '<STR_LIT:a>' ] , datetime . date ) <EOL> def test_invalid_root_dict_fromjson ( self ) : <EOL> try : <EOL> parse ( '<STR_LIT>' , { '<STR_LIT:a>' : ArrayType ( str ) } , False ) <EOL> assert False <EOL> except Exception as e : <EOL> assert isinstance ( e , ClientSideError ) <EOL> assert e . msg == "<STR_LIT>" <EOL> def test_invalid_list_fromjson ( self ) : <EOL> jlist = "<STR_LIT>" <EOL> try : <EOL> parse ( '<STR_LIT>' % jlist , { '<STR_LIT:a>' : ArrayType ( str ) } , False ) <EOL> assert False <EOL> except Exception as e : <EOL> assert isinstance ( e , InvalidInput ) <EOL> assert e . fieldname == '<STR_LIT:a>' <EOL> assert e . value == jlist <EOL> assert e . msg == "<STR_LIT>" % jlist <EOL> def test_invalid_dict_fromjson ( self ) : <EOL> jdict = "<STR_LIT>" <EOL> try : <EOL> parse ( '<STR_LIT>' % jdict , { '<STR_LIT:a>' : DictType ( str , str ) } , False ) <EOL> assert False <EOL> except Exception as e : <EOL> assert isinstance ( e , InvalidInput ) <EOL> assert e . fieldname == '<STR_LIT:a>' <EOL> assert e . value == jdict <EOL> assert e . msg == "<STR_LIT>" % jdict <EOL> def test_invalid_date_fromjson ( self ) : <EOL> jdate = "<STR_LIT>" <EOL> try : <EOL> parse ( '<STR_LIT>' % jdate , { '<STR_LIT:a>' : datetime . date } , False ) <EOL> assert False <EOL> except Exception as e : <EOL> assert isinstance ( e , InvalidInput ) <EOL> assert e . fieldname == '<STR_LIT:a>' <EOL> assert e . value == jdate <EOL> assert e . msg == "<STR_LIT>" % jdate <EOL> def test_parse_valid_date_bodyarg ( self ) : <EOL> j = parse ( '<STR_LIT>' , { '<STR_LIT:a>' : datetime . date } , True ) <EOL> assert isinstance ( j [ '<STR_LIT:a>' ] , datetime . date ) <EOL> def test_invalid_date_fromjson_bodyarg ( self ) : <EOL> jdate = "<STR_LIT>" <EOL> try : <EOL> parse ( '<STR_LIT>' % jdate , { '<STR_LIT:a>' : datetime . date } , True ) <EOL> assert False <EOL> except Exception as e : <EOL> assert isinstance ( e , InvalidInput ) <EOL> assert e . fieldname == '<STR_LIT:a>' <EOL> assert e . value == jdate <EOL> assert e . msg == "<STR_LIT>" % jdate <EOL> def test_valid_str_to_builtin_fromjson ( self ) : <EOL> types = six . integer_types + ( bool , float ) <EOL> value = '<STR_LIT:2>' <EOL> for t in types : <EOL> for ba in True , False : <EOL> jd = '<STR_LIT:%s>' if ba else '<STR_LIT>' <EOL> i = parse ( jd % value , { '<STR_LIT:a>' : t } , ba ) <EOL> self . assertEqual ( <EOL> i , { '<STR_LIT:a>' : t ( value ) } , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> t , repr ( i ) , repr ( t ( value ) ) <EOL> ) <EOL> ) <EOL> self . assertIsInstance ( i [ '<STR_LIT:a>' ] , t ) <EOL> def test_valid_int_fromjson ( self ) : <EOL> value = <NUM_LIT:2> <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> i = parse ( jd % value , { '<STR_LIT:a>' : int } , ba ) <EOL> self . assertEqual ( i , { '<STR_LIT:a>' : <NUM_LIT:2> } ) <EOL> self . assertIsInstance ( i [ '<STR_LIT:a>' ] , int ) <EOL> def test_valid_num_to_float_fromjson ( self ) : <EOL> values = <NUM_LIT:2> , <NUM_LIT> <EOL> for v in values : <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> i = parse ( jd % v , { '<STR_LIT:a>' : float } , ba ) <EOL> self . assertEqual ( i , { '<STR_LIT:a>' : float ( v ) } ) <EOL> self . assertIsInstance ( i [ '<STR_LIT:a>' ] , float ) <EOL> def test_invalid_str_to_buitin_fromjson ( self ) : <EOL> types = six . integer_types + ( float , bool ) <EOL> value = '<STR_LIT>' <EOL> for t in types : <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> try : <EOL> parse ( jd % value , { '<STR_LIT:a>' : t } , ba ) <EOL> assert False , ( <EOL> "<STR_LIT>" % <EOL> ( value , t ) <EOL> ) <EOL> except ClientSideError as e : <EOL> self . assertIsInstance ( e , InvalidInput ) <EOL> self . assertEqual ( e . fieldname , '<STR_LIT:a>' ) <EOL> self . assertEqual ( e . value , value ) <EOL> def test_ambiguous_to_bool ( self ) : <EOL> amb_values = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:2>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for value in amb_values : <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> try : <EOL> parse ( jd % value , { '<STR_LIT:a>' : bool } , ba ) <EOL> assert False , ( <EOL> "<STR_LIT>" % <EOL> ( value , bool ) <EOL> ) <EOL> except ClientSideError as e : <EOL> self . assertIsInstance ( e , InvalidInput ) <EOL> self . assertEqual ( e . fieldname , '<STR_LIT:a>' ) <EOL> self . assertEqual ( e . value , value ) <EOL> def test_true_strings_to_bool ( self ) : <EOL> true_values = ( '<STR_LIT:true>' , '<STR_LIT:t>' , '<STR_LIT:yes>' , '<STR_LIT:y>' , '<STR_LIT>' , '<STR_LIT:1>' ) <EOL> for value in true_values : <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> i = parse ( jd % value , { '<STR_LIT:a>' : bool } , ba ) <EOL> self . assertIsInstance ( i [ '<STR_LIT:a>' ] , bool ) <EOL> self . assertTrue ( i [ '<STR_LIT:a>' ] ) <EOL> def test_false_strings_to_bool ( self ) : <EOL> false_values = ( '<STR_LIT:false>' , '<STR_LIT:f>' , '<STR_LIT>' , '<STR_LIT:n>' , '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> for value in false_values : <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> i = parse ( jd % value , { '<STR_LIT:a>' : bool } , ba ) <EOL> self . assertIsInstance ( i [ '<STR_LIT:a>' ] , bool ) <EOL> self . assertFalse ( i [ '<STR_LIT:a>' ] ) <EOL> def test_true_ints_to_bool ( self ) : <EOL> true_values = ( <NUM_LIT:1> , <NUM_LIT:5> , - <NUM_LIT:3> ) <EOL> for value in true_values : <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> i = parse ( jd % value , { '<STR_LIT:a>' : bool } , ba ) <EOL> self . assertIsInstance ( i [ '<STR_LIT:a>' ] , bool ) <EOL> self . assertTrue ( i [ '<STR_LIT:a>' ] ) <EOL> def test_false_ints_to_bool ( self ) : <EOL> value = <NUM_LIT:0> <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> i = parse ( jd % value , { '<STR_LIT:a>' : bool } , ba ) <EOL> self . assertIsInstance ( i [ '<STR_LIT:a>' ] , bool ) <EOL> self . assertFalse ( i [ '<STR_LIT:a>' ] ) <EOL> def test_valid_simple_custom_type_fromjson ( self ) : <EOL> value = <NUM_LIT:2> <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> i = parse ( jd % value , { '<STR_LIT:a>' : CustomInt ( ) } , ba ) <EOL> self . assertEqual ( i , { '<STR_LIT:a>' : <NUM_LIT:2> } ) <EOL> self . assertIsInstance ( i [ '<STR_LIT:a>' ] , int ) <EOL> def test_invalid_simple_custom_type_fromjson ( self ) : <EOL> value = '<STR_LIT>' <EOL> for ba in True , False : <EOL> jd = '<STR_LIT>' if ba else '<STR_LIT>' <EOL> try : <EOL> i = parse ( jd % value , { '<STR_LIT:a>' : CustomInt ( ) } , ba ) <EOL> self . assertEqual ( i , { '<STR_LIT:a>' : <NUM_LIT:2> } ) <EOL> except ClientSideError as e : <EOL> self . assertIsInstance ( e , InvalidInput ) <EOL> self . assertEqual ( e . fieldname , '<STR_LIT:a>' ) <EOL> self . assertEqual ( e . value , value ) <EOL> self . assertEqual ( <EOL> e . msg , <EOL> "<STR_LIT>" % value <EOL> ) <EOL> def test_parse_unexpected_attribute ( self ) : <EOL> o = { <EOL> "<STR_LIT:id>" : "<STR_LIT:1>" , <EOL> "<STR_LIT:name>" : "<STR_LIT:test>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> for ba in True , False : <EOL> jd = o if ba else { "<STR_LIT:o>" : o } <EOL> try : <EOL> parse ( json . dumps ( jd ) , { '<STR_LIT:o>' : Obj } , ba ) <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> except wsme . exc . UnknownAttribute as e : <EOL> self . assertEqual ( e . attributes , set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def test_parse_unexpected_nested_attribute ( self ) : <EOL> no = { <EOL> "<STR_LIT:o>" : { <EOL> "<STR_LIT:id>" : "<STR_LIT:1>" , <EOL> "<STR_LIT:name>" : "<STR_LIT:test>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> } <EOL> for ba in False , True : <EOL> jd = no if ba else { "<STR_LIT>" : no } <EOL> try : <EOL> parse ( json . dumps ( jd ) , { '<STR_LIT>' : NestedObj } , ba ) <EOL> except wsme . exc . UnknownAttribute as e : <EOL> self . assertEqual ( e . attributes , set ( [ '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( e . fieldname , "<STR_LIT>" ) <EOL> def test_nest_result ( self ) : <EOL> self . root . protocols [ <NUM_LIT:0> ] . nest_result = True <EOL> r = self . app . get ( '<STR_LIT>' ) <EOL> print ( r ) <EOL> assert json . loads ( r . text ) == { "<STR_LIT:result>" : <NUM_LIT:2> } <EOL> def test_encode_sample_value ( self ) : <EOL> class MyType ( object ) : <EOL> aint = int <EOL> astr = str <EOL> register_type ( MyType ) <EOL> v = MyType ( ) <EOL> v . aint = <NUM_LIT:4> <EOL> v . astr = '<STR_LIT:s>' <EOL> r = wsme . rest . json . encode_sample_value ( MyType , v , True ) <EOL> print ( r ) <EOL> assert r [ <NUM_LIT:0> ] == ( '<STR_LIT>' ) <EOL> assert r [ <NUM_LIT:1> ] == json . dumps ( { '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : '<STR_LIT:s>' } , ensure_ascii = False , <EOL> indent = <NUM_LIT:4> , sort_keys = True ) <EOL> def test_bytes_tojson ( self ) : <EOL> assert tojson ( wsme . types . bytes , None ) is None <EOL> assert tojson ( wsme . types . bytes , b ( '<STR_LIT:ascii>' ) ) == u ( '<STR_LIT:ascii>' ) <EOL> def test_encode_sample_params ( self ) : <EOL> r = wsme . rest . json . encode_sample_params ( <EOL> [ ( '<STR_LIT:a>' , int , <NUM_LIT:2> ) ] , True <EOL> ) <EOL> assert r [ <NUM_LIT:0> ] == '<STR_LIT>' , r [ <NUM_LIT:0> ] <EOL> assert r [ <NUM_LIT:1> ] == '''<STR_LIT>''' , r [ <NUM_LIT:1> ] <EOL> def test_encode_sample_result ( self ) : <EOL> r = wsme . rest . json . encode_sample_result ( <EOL> int , <NUM_LIT:2> , True <EOL> ) <EOL> assert r [ <NUM_LIT:0> ] == '<STR_LIT>' , r [ <NUM_LIT:0> ] <EOL> assert r [ <NUM_LIT:1> ] == '''<STR_LIT:2>''' <EOL> def test_PUT ( self ) : <EOL> data = { "<STR_LIT:id>" : <NUM_LIT:1> , "<STR_LIT:name>" : u ( "<STR_LIT:test>" ) } <EOL> content = json . dumps ( dict ( data = data ) ) <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } <EOL> res = self . app . put ( <EOL> '<STR_LIT>' , <EOL> content , <EOL> headers = headers , <EOL> expect_errors = False ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> result = json . loads ( res . text ) <EOL> print ( result ) <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] == <NUM_LIT:1> <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:name>' ] == u ( "<STR_LIT:test>" ) <EOL> assert result [ '<STR_LIT:message>' ] == "<STR_LIT>" <EOL> def test_GET ( self ) : <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> } <EOL> res = self . app . get ( <EOL> '<STR_LIT>' , <EOL> headers = headers , <EOL> expect_errors = False ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> result = json . loads ( res . text ) <EOL> print ( result ) <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] == <NUM_LIT:1> <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:name>' ] == u ( "<STR_LIT:test>" ) <EOL> def test_GET_complex_accept ( self ) : <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> res = self . app . get ( <EOL> '<STR_LIT>' , <EOL> headers = headers , <EOL> expect_errors = False ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> result = json . loads ( res . text ) <EOL> print ( result ) <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] == <NUM_LIT:1> <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:name>' ] == u ( "<STR_LIT:test>" ) <EOL> def test_GET_complex_choose_xml ( self ) : <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> res = self . app . get ( <EOL> '<STR_LIT>' , <EOL> headers = headers , <EOL> expect_errors = False ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> assert res . content_type == '<STR_LIT>' <EOL> def test_GET_complex_accept_no_match ( self ) : <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> res = self . app . get ( <EOL> '<STR_LIT>' , <EOL> headers = headers , <EOL> status = <NUM_LIT> ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> assert res . body == b ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_GET_bad_simple_accept ( self ) : <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> res = self . app . get ( <EOL> '<STR_LIT>' , <EOL> headers = headers , <EOL> status = <NUM_LIT> ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> assert res . body == b ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_POST ( self ) : <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } <EOL> res = self . app . post ( <EOL> '<STR_LIT>' , <EOL> json . dumps ( dict ( data = dict ( id = <NUM_LIT:1> , name = u ( '<STR_LIT:test>' ) ) ) ) , <EOL> headers = headers , <EOL> expect_errors = False ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> result = json . loads ( res . text ) <EOL> print ( result ) <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] == <NUM_LIT:1> <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:name>' ] == u ( "<STR_LIT:test>" ) <EOL> assert result [ '<STR_LIT:message>' ] == "<STR_LIT>" <EOL> def test_POST_bad_content_type ( self ) : <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> } <EOL> res = self . app . post ( <EOL> '<STR_LIT>' , <EOL> json . dumps ( dict ( data = dict ( id = <NUM_LIT:1> , name = u ( '<STR_LIT:test>' ) ) ) ) , <EOL> headers = headers , <EOL> status = <NUM_LIT> ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> assert res . body == b ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_DELETE ( self ) : <EOL> res = self . app . delete ( <EOL> '<STR_LIT>' , <EOL> expect_errors = False ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> result = json . loads ( res . text ) <EOL> print ( result ) <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] == <NUM_LIT:1> <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:name>' ] == u ( "<STR_LIT:test>" ) <EOL> assert result [ '<STR_LIT:message>' ] == "<STR_LIT>" <EOL> def test_extra_arguments ( self ) : <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> } <EOL> res = self . app . get ( <EOL> '<STR_LIT>' , <EOL> headers = headers , <EOL> expect_errors = False ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> result = json . loads ( res . text ) <EOL> print ( result ) <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] == <NUM_LIT:1> <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:name>' ] == u ( "<STR_LIT:test>" ) <EOL> assert result [ '<STR_LIT:message>' ] == "<STR_LIT>" <EOL> def test_unexpected_extra_arg ( self ) : <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } <EOL> data = { "<STR_LIT:id>" : <NUM_LIT:1> , "<STR_LIT:name>" : "<STR_LIT:test>" } <EOL> content = json . dumps ( { "<STR_LIT:data>" : data , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> res = self . app . put ( <EOL> '<STR_LIT>' , <EOL> content , <EOL> headers = headers , <EOL> expect_errors = True ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT> ) <EOL> def test_unexpected_extra_attribute ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } <EOL> data = { "<STR_LIT:id>" : <NUM_LIT:1> , "<STR_LIT:name>" : "<STR_LIT:test>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> content = json . dumps ( { "<STR_LIT:data>" : data } ) <EOL> res = self . app . put ( <EOL> '<STR_LIT>' , <EOL> content , <EOL> headers = headers , <EOL> expect_errors = True ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT> ) <EOL> def test_body_arg ( self ) : <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } <EOL> res = self . app . post ( <EOL> '<STR_LIT>' , <EOL> json . dumps ( dict ( id = <NUM_LIT:1> , name = u ( '<STR_LIT:test>' ) ) ) , <EOL> headers = headers , <EOL> expect_errors = False ) <EOL> print ( "<STR_LIT>" , res . body ) <EOL> result = json . loads ( res . text ) <EOL> print ( result ) <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] == <NUM_LIT:1> <EOL> assert result [ '<STR_LIT:data>' ] [ '<STR_LIT:name>' ] == u ( "<STR_LIT:test>" ) <EOL> assert result [ '<STR_LIT:message>' ] == "<STR_LIT:hello>" </s>
