<s> """<STR_LIT>""" <EOL> import cStringIO <EOL> import gzip <EOL> import json <EOL> import unittest <EOL> from google . appengine . tools . devappserver2 . endpoints import api_request <EOL> from google . appengine . tools . devappserver2 . endpoints import test_utils <EOL> class RequestTest ( unittest . TestCase ) : <EOL> def test_parse_no_body ( self ) : <EOL> request = test_utils . build_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , request . path ) <EOL> self . assertEqual ( '<STR_LIT>' , request . query ) <EOL> self . assertEqual ( { '<STR_LIT:bar>' : [ '<STR_LIT>' ] } , request . parameters ) <EOL> self . assertEqual ( '<STR_LIT>' , request . body ) <EOL> self . assertEqual ( { } , request . body_json ) <EOL> self . assertEqual ( [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] , <EOL> request . headers . items ( ) ) <EOL> self . assertEqual ( None , request . request_id ) <EOL> def test_parse_with_body ( self ) : <EOL> request = test_utils . build_request ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , request . path ) <EOL> self . assertEqual ( '<STR_LIT>' , request . query ) <EOL> self . assertEqual ( { '<STR_LIT:bar>' : [ '<STR_LIT>' ] } , request . parameters ) <EOL> self . assertEqual ( '<STR_LIT>' , request . body ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : '<STR_LIT:body>' } , request . body_json ) <EOL> self . assertEqual ( [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] , <EOL> request . headers . items ( ) ) <EOL> self . assertEqual ( None , request . request_id ) <EOL> def test_parse_gzipped_body ( self ) : <EOL> def gzip_encode ( content ) : <EOL> out = cStringIO . StringIO ( ) <EOL> with gzip . GzipFile ( fileobj = out , mode = '<STR_LIT:w>' ) as f : <EOL> f . write ( content ) <EOL> return out . getvalue ( ) <EOL> uncompressed = '<STR_LIT>' <EOL> compressed = gzip_encode ( uncompressed ) <EOL> request = test_utils . build_request ( '<STR_LIT>' , compressed , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , request . path ) <EOL> self . assertEqual ( '<STR_LIT>' , request . query ) <EOL> self . assertEqual ( { '<STR_LIT:bar>' : [ '<STR_LIT>' ] } , request . parameters ) <EOL> self . assertEqual ( uncompressed , request . body ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : '<STR_LIT:body>' } , request . body_json ) <EOL> self . assertItemsEqual ( [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> request . headers . items ( ) ) <EOL> self . assertEqual ( None , request . request_id ) <EOL> def test_parse_empty_values ( self ) : <EOL> request = test_utils . build_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , request . path ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , request . query ) <EOL> self . assertEqual ( { '<STR_LIT:bar>' : [ '<STR_LIT>' ] } , request . parameters ) <EOL> self . assertEqual ( '<STR_LIT>' , request . body ) <EOL> self . assertEqual ( { } , request . body_json ) <EOL> self . assertEqual ( [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] , <EOL> request . headers . items ( ) ) <EOL> self . assertEqual ( None , request . request_id ) <EOL> def test_parse_multiple_values ( self ) : <EOL> request = test_utils . build_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , request . path ) <EOL> self . assertEqual ( '<STR_LIT>' , request . query ) <EOL> self . assertEqual ( { '<STR_LIT:bar>' : [ '<STR_LIT>' , '<STR_LIT:foo>' ] , '<STR_LIT:foo>' : [ '<STR_LIT:bar>' ] } , <EOL> request . parameters ) <EOL> self . assertEqual ( '<STR_LIT>' , request . body ) <EOL> self . assertEqual ( { } , request . body_json ) <EOL> self . assertEqual ( [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] , <EOL> request . headers . items ( ) ) <EOL> self . assertEqual ( None , request . request_id ) <EOL> def test_is_rpc ( self ) : <EOL> request = test_utils . build_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , request . path ) <EOL> self . assertEqual ( None , request . query ) <EOL> self . assertTrue ( request . is_rpc ( ) ) <EOL> def test_is_not_rpc ( self ) : <EOL> request = test_utils . build_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , request . path ) <EOL> self . assertEqual ( None , request . query ) <EOL> self . assertFalse ( request . is_rpc ( ) ) <EOL> def teset_is_not_rpc_prefix ( self ) : <EOL> request = test_utils . build_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , request . path ) <EOL> self . assertEqual ( None , request . query ) <EOL> self . assertFalse ( request . is_rpc ( ) ) <EOL> def test_batch ( self ) : <EOL> request = test_utils . build_request ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertTrue ( request . is_batch ( ) ) <EOL> self . assertFalse ( isinstance ( request . body_json , list ) ) <EOL> def test_batch_too_large ( self ) : <EOL> """<STR_LIT>""" <EOL> request = test_utils . build_request ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertTrue ( request . is_batch ( ) ) <EOL> self . assertEqual ( json . loads ( '<STR_LIT>' ) , <EOL> request . body_json ) <EOL> def test_source_ip ( self ) : <EOL> body = '<STR_LIT:{}>' <EOL> path = '<STR_LIT>' <EOL> env = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : path , '<STR_LIT>' : cStringIO . StringIO ( body ) } <EOL> request = api_request . ApiRequest ( env ) <EOL> self . assertEqual ( request . source_ip , None ) <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> request = api_request . ApiRequest ( env ) <EOL> self . assertEqual ( request . source_ip , '<STR_LIT>' ) <EOL> def test_copy ( self ) : <EOL> request = test_utils . build_request ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> copied = request . copy ( ) <EOL> self . assertEqual ( request . headers . items ( ) , copied . headers . items ( ) ) <EOL> self . assertEqual ( request . body , copied . body ) <EOL> self . assertEqual ( request . body_json , copied . body_json ) <EOL> self . assertEqual ( request . path , copied . path ) <EOL> copied . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> copied . body = '<STR_LIT>' <EOL> copied . body_json = { '<STR_LIT>' : '<STR_LIT:body>' } <EOL> copied . path = '<STR_LIT>' <EOL> self . assertNotEqual ( request . headers . items ( ) , copied . headers . items ( ) ) <EOL> self . assertNotEqual ( request . body , copied . body ) <EOL> self . assertNotEqual ( request . body_json , copied . body_json ) <EOL> self . assertNotEqual ( request . path , copied . path ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
