Ticket #306: user-agent-306.patch

File user-agent-306.patch, 6.1 KB (added by thijs, 5 months ago)

Patch against r1464

  • pyamf/tests/remoting/test_client.py

     
    44# See LICENSE for details. 
    55 
    66""" 
    7 Test for Remoting client. 
     7Tests for Remoting client. 
    88 
    99@since: 0.1.0 
    1010""" 
     
    370370        gw.connection = dc 
    371371 
    372372        dc.tc = self 
    373         dc.expected_headers = {'Content-Type': 'application/x-amf'} 
     373        dc.expected_headers = {'Content-Type': remoting.CONTENT_TYPE, 
     374                               'User-Agent': client.DEFAULT_USER_AGENT} 
    374375 
    375376        service = gw.getService('baz', auto_execute=False) 
    376377        wrapper = service.gak() 
    377378 
    378379        response = DummyResponse(200, '\x00\x00\x00\x00\x00\x01\x00\x0b/1/onRe' 
    379380            'sult\x00\x04null\x00\x00\x00\x00\x00\x02\x00\x05hello', { 
    380             'Content-Type': 'application/x-amf', 'Content-Length': 50}) 
     381            'Content-Type': remoting.CONTENT_TYPE, 'Content-Length': 50}) 
    381382        response.tc = self 
    382383 
    383384        dc.expected_url = '/x/y/z' 
     
    392393 
    393394        response = DummyResponse(200, '\x00\x00\x00\x00\x00\x01\x00\x0b/2/onRe' 
    394395            'sult\x00\x04null\x00\x00\x00\x00\x00\x02\x00\x05hello', { 
    395             'Content-Type': 'application/x-amf'}) 
     396            'Content-Type': remoting.CONTENT_TYPE}) 
    396397        response.tc = self 
    397398 
    398399        dc.expected_url = '/x/y/z' 
     
    408409        gw.connection = dc 
    409410 
    410411        dc.tc = self 
    411         dc.expected_headers = {'Content-Type': 'application/x-amf'} 
     412        dc.expected_headers = {'Content-Type': remoting.CONTENT_TYPE, 
     413                               'User-Agent': client.DEFAULT_USER_AGENT} 
    412414 
    413415        baz = gw.getService('baz', auto_execute=False) 
    414416        spam = gw.getService('spam', auto_execute=False) 
     
    418420        response = DummyResponse(200, '\x00\x00\x00\x00\x00\x02\x00\x0b/1/onRe' 
    419421            'sult\x00\x04null\x00\x00\x00\x00\x00\x02\x00\x05hello\x00\x0b/2/o' 
    420422            'nResult\x00\x04null\x00\x00\x00\x00\x00\x02\x00\x05hello', { 
    421                 'Content-Type': 'application/x-amf'}) 
     423                'Content-Type': remoting.CONTENT_TYPE}) 
    422424        response.tc = self 
    423425 
    424426        dc.expected_url = '/x/y/z' 
     
    436438        gw.connection = dc 
    437439 
    438440        response = DummyResponse(200, '\x00\x00\x00\x00\x00\x00', { 
    439             'Content-Type': 'application/x-amf'}) 
     441            'Content-Type': remoting.CONTENT_TYPE}) 
    440442 
    441443        dc.response = response 
    442444 
     
    477479 
    478480        response = DummyResponse(200, '\x00\x00\x00\x01\x00\x12AppendToGatewayUrl' 
    479481            '\x01\x00\x00\x00\x00\x02\x00\x05hello\x00\x00', { 
    480             'Content-Type': 'application/x-amf'}) 
     482            'Content-Type': remoting.CONTENT_TYPE}) 
    481483 
    482484        dc.response = response 
    483485 
     
    491493 
    492494        response = DummyResponse(200, '\x00\x00\x00\x01\x00\x11ReplaceGatewayUrl' 
    493495            '\x01\x00\x00\x00\x00\x02\x00\x10http://spam.eggs\x00\x00', { 
    494             'Content-Type': 'application/x-amf'}) 
     496            'Content-Type': remoting.CONTENT_TYPE}) 
    495497 
    496498        dc.response = response 
    497499 
     
    504506        gw.connection = dc 
    505507        dc.response = DummyResponse(200, '\x00\x00\x00\x01\x00\x11ReplaceGatewayUrl' 
    506508            '\x01\x00\x00\x00\x00\x02\x00\x10http://spam.eggs\x00\x00', { 
    507             'Content-Type': 'application/x-amf'}) 
     509            'Content-Type': remoting.CONTENT_TYPE}) 
    508510 
    509511        gw._getResponse() 
    510512        self.assertTrue(dc.response.closed, True) 
  • pyamf/remoting/client/__init__.py

     
    1616#: @see: L{ClientTypes<pyamf.ClientTypes>} 
    1717DEFAULT_CLIENT_TYPE = pyamf.ClientTypes.Flash6 
    1818 
     19#: Default user agent is C{PyAMF/x.x.x}. 
     20DEFAULT_USER_AGENT = 'PyAMF/%s' % '.'.join(map(lambda x: str(x), pyamf.__version__)) 
     21 
    1922HTTP_OK = 200 
    2023 
    2124def convert_args(args): 
     
    182185    @type headers: L{HeaderCollection<pyamf.remoting.HeaderCollection>} 
    183186    """ 
    184187 
    185     def __init__(self, url, amf_version=pyamf.AMF0, client_type=DEFAULT_CLIENT_TYPE): 
     188    def __init__(self, url, amf_version=pyamf.AMF0, client_type=DEFAULT_CLIENT_TYPE, 
     189                 user_agent=DEFAULT_USER_AGENT): 
    186190        self.logger = logging.instance_logger(self) 
    187191        self.original_url = url 
    188192        self.requests = [] 
    189193        self.request_number = 1 
    190194 
     195        self.user_agent = user_agent 
    191196        self.amf_version = amf_version 
    192197        self.client_type = client_type 
    193198        self.headers = remoting.HeaderCollection() 
     
    232237            raise ValueError, 'Unknown scheme' 
    233238 
    234239        self.logger.info('Creating connection to %s://%s:%s' % (self.url[0], hostname, port)) 
    235  
     240        self.logger.debug('User-Agent: %s' % self.user_agent) 
     241         
    236242    def addHeader(self, name, value, must_understand=False): 
    237243        """ 
    238244        Sets a persistent header to send with each request. 
     
    334340        """ 
    335341        self.logger.debug('Executing single request: %s' % request) 
    336342        body = remoting.encode(self.getAMFRequest([request])) 
    337  
     343        headers = {'Content-Type': remoting.CONTENT_TYPE, 
     344                   'User-Agent': self.user_agent} 
     345         
    338346        self.logger.debug('Sending POST request to %s' % self._root_url) 
    339347        self.connection.request('POST', self._root_url, body.getvalue(), 
    340             {'Content-Type': remoting.CONTENT_TYPE}) 
     348                                headers) 
    341349 
    342350        envelope = self._getResponse() 
    343351        self.removeRequest(request) 
     
    350358        C{self.requests}. 
    351359        """ 
    352360        body = remoting.encode(self.getAMFRequest(self.requests)) 
    353  
     361        headers = {'Content-Type': remoting.CONTENT_TYPE, 
     362                   'User-Agent': self.user_agent} 
     363         
    354364        self.logger.debug('Sending POST request to %s' % self._root_url) 
    355365        self.connection.request('POST', self._root_url, body.getvalue(), 
    356             {'Content-Type': remoting.CONTENT_TYPE}) 
     366                                headers) 
    357367 
    358368        envelope = self._getResponse() 
    359369