<s> import unittest <EOL> from mock import Mock <EOL> from biicode . common . utils . bii_logging import logger <EOL> from biicode . server . rest . bottle_plugins . bson_bottle_plugin import BSONBottlePlugin , BSONBottlePluginException <EOL> import bottle <EOL> from bson import BSON <EOL> from bottle import request , HTTPResponse , PluginError <EOL> from biicode . server . conf import BII_MAX_MEMORY_PER_REQUEST <EOL> class BsonBottleTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . plugin = BSONBottlePlugin ( ) <EOL> self . ip = "<STR_LIT>" <EOL> self . callback = Mock ( wraps = self . _callback ) <EOL> self . callbacknobson = Mock ( wraps = self . _callbackno ) <EOL> bottle . request . environ [ '<STR_LIT>' ] = self . ip <EOL> bottle . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . context = Mock ( ) <EOL> self . context . callback = self . _callback <EOL> self . contextnobson = Mock ( wraps = self . _callbackno ) <EOL> self . contextnobson . callback = self . _callbackno <EOL> self . __set_bson_content ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def tearDown ( self ) : <EOL> pass <EOL> def testAddOtherPluginWithSameKeywork ( self ) : <EOL> app = Mock ( ) <EOL> bad_plugin = Mock ( ) <EOL> bad_plugin . keyword = "<STR_LIT>" <EOL> app . plugins = set ( [ bad_plugin ] ) <EOL> self . assertRaises ( PluginError , self . plugin . setup , app ) <EOL> def testCallbackCalled ( self ) : <EOL> self . plugin . apply ( self . callback , self . context ) ( ) <EOL> self . assertTrue ( self . callback . called ) <EOL> def testNotBsonRequest ( self ) : <EOL> bottle . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertRaises ( BSONBottlePluginException , self . plugin . apply ( self . callback , self . context ) ) <EOL> def testNotBsonRequestWithNoBonDataParameter ( self ) : <EOL> bottle . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . plugin . apply ( self . callbacknobson , self . contextnobson ) ( ) <EOL> self . callbacknobson . assert_called_once_with ( ) <EOL> def testNoContent ( self ) : <EOL> bottle . request . environ [ "<STR_LIT>" ] = <NUM_LIT:0> <EOL> self . assertRaises ( BSONBottlePluginException , <EOL> self . plugin . apply ( self . callback , self . context ) ) <EOL> def testMaxContent ( self ) : <EOL> bottle . request . environ [ "<STR_LIT>" ] = ( BII_MAX_MEMORY_PER_REQUEST ) + <NUM_LIT:1> <EOL> self . assertRaises ( BSONBottlePluginException , <EOL> self . plugin . apply ( self . callback , self . context ) ) <EOL> def testAbortWithBSON ( self ) : <EOL> tmp = self . plugin . abort_with_bson ( <NUM_LIT> , { "<STR_LIT>" : <NUM_LIT:2> } ) <EOL> self . assertIsInstance ( tmp , HTTPResponse ) <EOL> self . assertEquals ( "<STR_LIT>" , tmp . content_type ) <EOL> self . assertEquals ( <NUM_LIT> , tmp . status_code ) <EOL> self . assertEquals ( str ( BSON . encode ( { "<STR_LIT>" : <NUM_LIT:2> } ) ) , tmp . body ) <EOL> def _callback ( self , bson_data = "<STR_LIT>" ) : <EOL> logger . debug ( "<STR_LIT>" + str ( bson_data ) ) <EOL> pass <EOL> def _callbackno ( self ) : <EOL> pass <EOL> def __set_bson_content ( self , data ) : <EOL> bottle . request . environ [ '<STR_LIT>' ] = str ( BSON . encode ( data ) ) <EOL> bottle . request . environ [ "<STR_LIT>" ] = len ( bottle . request . environ [ '<STR_LIT>' ] ) <EOL> bottle . request . body = Mock <EOL> bottle . request . body . read = Mock ( return_value = bottle . request . environ [ '<STR_LIT>' ] ) </s>
