Exemplo n.º 1
0
    def test_string_representation_with_no_message(self):
        url = "look at my silly little URL"
        headers = {suds.byte_str("yuck"): suds.byte_str("ptooiii...")}
        request = Request(url)
        request.headers = headers
        expected = u("""\
URL: %s
HEADERS: %s""") % (url, request.headers)
        assert text_type(request) == expected
        if sys.version_info < (3, ):
            assert str(request) == expected.encode("utf-8")
Exemplo n.º 2
0
    def test_string_representation_with_no_message(self):
        url = "look at my silly little URL"
        headers = {suds.byte_str("yuck"): suds.byte_str("ptooiii...")}
        request = Request(url)
        request.headers = headers
        expected = u("""\
URL: %s
HEADERS: %s""") % (url, request.headers)
        assert text_type(request) == expected
        if sys.version_info < (3,):
            assert str(request) == expected.encode("utf-8")
Exemplo n.º 3
0
 def send(self, soapenv):
     """
     Send soap message.
     @param soapenv: A soap envelope to send.
     @type soapenv: L{Document}
     @return: The reply to the sent message.
     @rtype: I{builtin} or I{subclass of} L{Object}
     """
     location = self.location()
     binding = self.method.binding.input
     transport = self.options.transport
     retxml = self.options.retxml
     nosend = self.options.nosend
     prettyxml = self.options.prettyxml
     timer = metrics.Timer()
     log.debug('sending to (%s)\nmessage:\n%s', location, soapenv)
     try:
         self.last_sent(soapenv)
         plugins = PluginContainer(self.options.plugins)
         plugins.message.marshalled(envelope=soapenv.root())
         if prettyxml:
             soapenv = soapenv.str()
         else:
             soapenv = soapenv.plain()
         soapenv = soapenv.encode('utf-8')
         
         # remove empty nodes
         import re
         soapenv = re.sub('(<(\w+)[^<]*?)/>', '', soapenv)
         
         ctx = plugins.message.sending(envelope=soapenv)
         soapenv = ctx.envelope
         if nosend:
             return RequestContext(self, binding, soapenv)
         request = Request(location, soapenv)
         request.headers = self.headers()
         timer.start()
         reply = transport.send(request)
         timer.stop()
         metrics.log.debug('waited %s on server reply', timer)
         ctx = plugins.message.received(reply=reply.message)
         reply.message = ctx.reply
         if retxml:
             result = reply.message
         else:
             result = self.succeeded(binding, reply.message)
     except TransportError as e:
         if e.httpcode in (202, 204):
             result = None
         else:
             log.error(self.last_sent(), exc_info=True)
             result = self.failed(binding, e)
     return result
Exemplo n.º 4
0
 def send(self, soapenv):
     """
     Send soap message.
     @param soapenv: A soap envelope to send.
     @type soapenv: L{Document}
     @return: The reply to the sent message.
     @rtype: I{builtin} or I{subclass of} L{Object}
     """
     result = None
     location = self.location()
     binding = self.method.binding.input
     transport = self.options.transport
     retxml = self.options.retxml
     nosend = self.options.nosend
     prettyxml = self.options.prettyxml
     timer = metrics.Timer()
     log.debug('sending to (%s)\nmessage:\n%s', location, soapenv)
     try:
         self.last_sent(soapenv)
         plugins = PluginContainer(self.options.plugins)
         plugins.message.marshalled(envelope=soapenv.root())
         if prettyxml:
             soapenv = soapenv.str()
         else:
             soapenv = soapenv.plain()
         soapenv = soapenv.encode('utf-8')
         ctx = plugins.message.sending(envelope=soapenv)
         soapenv = ctx.envelope
         if nosend:
             return RequestContext(self, binding, soapenv)
         request = Request(location, soapenv)
         request.headers = self.headers()
         timer.start()
         reply = transport.send(request)
         timer.stop()
         metrics.log.debug('waited %s on server reply', timer)
         ctx = plugins.message.received(reply=reply.message)
         reply.message = ctx.reply
         if retxml:
             result = reply.message
         else:
             result = self.succeeded(binding, reply.message)
     except TransportError as e:
         if e.httpcode in (202, 204):
             result = None
         else:
             log.error(self.last_sent())
             result = self.failed(binding, e)
     return result
Exemplo n.º 5
0
 def send(self, soapenv):
     """
     Send soap message.
     @param soapenv: A soap envelope to send.
     @type soapenv: L{Document}
     @return: The reply to the sent message.
     @rtype: I{builtin} or I{subclass of} L{Object}
     """
     result = None
     location = suds.bytes2str(self.location())
     binding = self.method.binding.input
     transport = self.options.transport
     retxml = self.options.retxml
     prettyxml = self.options.prettyxml
     log.debug('sending to (%s)\nmessage:\n%s', location, soapenv)
     try:
         self.last_sent(soapenv)
         plugins = PluginContainer(self.options.plugins)
         plugins.message.marshalled(envelope=soapenv.root())
         if prettyxml:
             soapenv = soapenv.str()
         else:
             soapenv = soapenv.plain()
         soapenv = soapenv.encode('utf-8')
         plugins.message.sending(envelope=soapenv)
         request = Request(location, soapenv)            
         request.headers = self.headers()    
         reply = transport.send(request)
         ctx = plugins.message.received(reply=reply.message)
         reply.message = ctx.reply            
         if retxml:
             result = reply.message
         else:
             result = self.succeeded(binding, reply.message)
     except TransportError as e:
         if e.httpcode in (202,204):
             result = None
         else:
             log.error(self.last_sent())
             result = self.failed(binding, e)
     return result
Exemplo n.º 6
0
    def test_string_representation_with_message(self, url, headers, message):
        for key, value in headers.items():
            old_key = key
            if isinstance(key, text_type):
                key = key.encode("utf-8")
                del headers[old_key]
            if isinstance(value, text_type):
                value = value.encode("utf-8")
            headers[key] = value
        if isinstance(message, text_type):
            message = message.encode("utf-8")
        request = Request(url, message)
        request.headers = headers
        expected = u("""\
URL: %s
HEADERS: %s
MESSAGE:
%s""") % (url, request.headers, message.decode("raw_unicode_escape"))
        assert text_type(request) == expected
        if sys.version_info < (3,):
            assert str(request) == expected.encode("utf-8")
Exemplo n.º 7
0
    def test_string_representation_with_message(self, url, headers, message):
        for key, value in headers.items():
            old_key = key
            if isinstance(key, text_type):
                key = key.encode("utf-8")
                del headers[old_key]
            if isinstance(value, text_type):
                value = value.encode("utf-8")
            headers[key] = value
        if isinstance(message, text_type):
            message = message.encode("utf-8")
        request = Request(url, message)
        request.headers = headers
        expected = u("""\
URL: %s
HEADERS: %s
MESSAGE:
%s""") % (url, request.headers, message.decode("raw_unicode_escape"))
        assert text_type(request) == expected
        if sys.version_info < (3, ):
            assert str(request) == expected.encode("utf-8")
Exemplo n.º 8
0
 def send(self, soapenv):
     """
     Send soap message.
     @param soapenv: A soap envelope to send.
     @type soapenv: L{Document}
     @return: The reply to the sent message.
     @rtype: I{builtin} or I{subclass of} L{Object}
     """
     location = self.location()
     log.debug('sending to (%s)\nmessage:\n%s', location, soapenv)
     original_soapenv = soapenv
     plugins = PluginContainer(self.options.plugins)
     plugins.message.marshalled(envelope=soapenv.root())
     if self.options.prettyxml:
         soapenv = soapenv.str()
     else:
         soapenv = soapenv.plain()
     soapenv = soapenv.encode('utf-8')
     ctx = plugins.message.sending(envelope=soapenv)
     soapenv = ctx.envelope
     if self.options.nosend:
         return RequestContext(self, soapenv, original_soapenv)
     request = Request(location, soapenv)
     request.headers = self.headers()
     try:
         timer = metrics.Timer()
         timer.start()
         reply = self.options.transport.send(request)
         timer.stop()
         metrics.log.debug('waited %s on server reply', timer)
     except TransportError as e:
         content = e.fp and e.fp.read() or ''
         return self.process_reply(reply=content,
                                   status=e.httpcode,
                                   description=tostr(e),
                                   original_soapenv=original_soapenv)
     return self.process_reply(reply=reply.message,
                               original_soapenv=original_soapenv)
Exemplo n.º 9
0
 def send(self, soapenv):
     """
     Send soap message.
     @param soapenv: A soap envelope to send.
     @type soapenv: L{Document}
     @return: The reply to the sent message.
     @rtype: I{builtin} or I{subclass of} L{Object}
     """
     location = self.location()
     log.debug('sending to (%s)\nmessage:\n%s', location, soapenv)
     original_soapenv = soapenv
     plugins = PluginContainer(self.options.plugins)
     plugins.message.marshalled(envelope=soapenv.root())
     if self.options.prettyxml:
         soapenv = soapenv.str()
     else:
         soapenv = soapenv.plain()
     soapenv = soapenv.encode('utf-8')
     ctx = plugins.message.sending(envelope=soapenv)
     soapenv = ctx.envelope
     if self.options.nosend:
         return RequestContext(self, soapenv, original_soapenv)
     request = Request(location, soapenv)
     request.headers = self.headers()
     try:
         timer = metrics.Timer()
         timer.start()
         reply = self.options.transport.send(request)
         timer.stop()
         metrics.log.debug('waited %s on server reply', timer)
     except TransportError as e:
         content = e.fp and e.fp.read() or ''
         return self.process_reply(reply=content, status=e.httpcode,
             description=tostr(e), original_soapenv=original_soapenv)
     return self.process_reply(reply=reply.message,
         original_soapenv=original_soapenv)
Exemplo n.º 10
0
def with_soap_attachment(suds_method, attachment_data, *args, **kwargs):
    """ Add an attachment to a suds soap request.

    attachment_data is assumed to contain a list:
      ( <attachment content>, <content id>, <mime-type> )

    The attachment content is only required required list element.
    """
    from suds.transport import Request
    import uuid
    # Suds doesn't currently support SOAP Attachments, so we have to build our
    # own attachment support, using parts of the suds library

    MIME_DEFAULT = 'text/plain'
    attachment_transfer_encoding = 'base64'
    soap_method = suds_method.method
    HUD_ARM_LOCAL_URL  = "file:///home/qwu/wmtestsoap/resource/ctcc_mm_send_service_2_2.wsdl"
    HUD_ARM_SERVICE_URL = "http://219.148.22.73:38080/sag/services/SendMessageService" 

    if len(attachment_data) == 3:
        data, attachment_id, attachment_mimetype = attachment_data
    elif len(attachment_data) == 2:
        data, attachment_id = attachment_data
        attachment_mimetype = MIME_DEFAULT
    elif len(attachment_data) == 1:
        data = attachment_data
        attachment_mimetype = MIME_DEFAULT
        attachment_id = uuid.uuid4()
    
    #attachment_mimetype = MIME_DEFAULT
    # Generate SOAP XML appropriate for this request
    soap_client = suds_method.clientclass(kwargs)
    binding = soap_method.binding.input
    soap_xml = binding.get_message(soap_method, args, kwargs)
    #print soap_xml
    #print attachment_data
    # Prepare MIME headers & boundaries
    boundary_id = 'uuid:%s' % uuid.uuid4()
    boundary_id2 = 'uuid:%s' % uuid.uuid4()
    root_part_id = 'uuid:%s' % uuid.uuid4()
    message_id = 'uuid:%s' % uuid.uuid4()
    request_headers = {
      'Content-Type': '; '.join([
          'Multipart/Related',
          #'Multipart/mixed',
          'type="text/xml"',
          'start="<%s>"' % root_part_id,
          'boundary="%s"' % boundary_id,
        ]),
    }

    request_headers2 = 'Content-Type: Multipart/alternative; boundary="%s"' % boundary_id2
         
    soap_headers = '\n'.join([
      'Content-Type: text/xml; charset=UTF-8',
      'Content-Transfer-Encoding: 8bit',
      'Content-ID: <%s>' % root_part_id,
      '',
    ])
        
    message_headers = '\n'.join([
      'Content-Type: text/plain; charset=UTF-8',
      'Content-Transfer-Encoding: 8bit',
      'Content-ID: <%s>' % message_id,
      '',
    ])
    attachment_headers = '\n'.join([
      'Content-Type: %s; name="1.jpg"' % attachment_mimetype,
      'Content-Disposition: attachment; filename="1.jpg"',
      'Content-Transfer-Encoding: %s' % attachment_transfer_encoding,
      'Content-ID: <%s>' % attachment_id,
      '',
    ])

    request_text = '\n'.join([
      '',
      '--%s' % boundary_id,
      soap_headers,
      str(soap_xml),
      '',
      #'--%s' % boundary_id,
      #message_headers,
      #'hello world!',
      #'',
      '--%s' % boundary_id,
      attachment_headers,
      data,
      '--%s--' % boundary_id
    ])

    # Build the full request
    ddrequest_text = '\n'.join([
      '',
      '--%s' % boundary_id,
      request_headers2,
      '',
      '--%s' % boundary_id2,
      soap_headers,
      str(soap_xml),
      '',
      '--%s' % boundary_id2,
      message_headers,
      'hello world!',
      '',
      '--%s--' % boundary_id2,
      '',
      '--%s' % boundary_id,
      attachment_headers,
      data,
      '--%s--' % boundary_id
    ])
    #print request_text
    # Stuff everything into a request object
    headers = suds_method.client.options.headers.copy()
    headers.update(request_headers)
    request = Request(HUD_ARM_SERVICE_URL, request_text)
    #request = Request(HUD_ARM_LOCAL_URL, request_text)
    request.headers = headers
    #print type(request)
    #file = open("f:/testsoap/1.txt","w")
    #file.write(request_text)
    #file.close()
    # Send the request
    #print request
    response = suds_method.client.options.transport.send(request)
    return response
Exemplo n.º 11
0
def with_soap_attachment(suds_method, attachment_data, *args, **kwargs):
    """ Add an attachment to a suds soap request.

    attachment_data is assumed to contain a list:
      ( <attachment content>, <content id>, <mime-type> )

    The attachment content is only required required list element.
    """
    from suds.transport import Request

    # Suds doesn't currently support SOAP Attachments, so we have to build our
    # own attachment support, using parts of the suds library

    MIME_DEFAULT = 'text/plain'
    attachment_transfer_encoding = '8bit'
    soap_method = suds_method.method

    if len(attachment_data) == 3:
        data, attachment_id, attachment_mimetype = attachment_data
    elif len(attachment_data) == 2:
        data, attachment_id = attachment_data
        attachment_mimetype = MIME_DEFAULT
    elif len(attachment_data) == 1:
        data = attachment_data
        attachment_mimetype = MIME_DEFAULT
        attachment_id = uuid.uuid4()

    # Generate SOAP XML appropriate for this request
    soap_client = suds_method.clientclass(kwargs)
    binding = soap_method.binding.input
    soap_xml = binding.get_message(soap_method, args, kwargs)

    # Prepare MIME headers & boundaries
    boundary_id = 'uuid:%s' % uuid.uuid4()
    root_part_id ='uuid:%s' % uuid.uuid4()
    request_headers = {
      'Content-Type': '; '.join([
          'multipart/related',
          'type="text/xml"',
          'start="<%s>"' % root_part_id,
          'boundary="%s"' % boundary_id,
        ]),
    }
    soap_headers = '\n'.join([
      'Content-Type: text/xml; charset=UTF-8',
      'Content-Transfer-Encoding: 8bit',
      'Content-Id: <%s>' % root_part_id,
      '',
    ])
    attachment_headers = '\n'.join([
      'Content-Type: %s' % attachment_mimetype,
      'Content-Transfer-Encoding: %s' % attachment_transfer_encoding,
      'Content-Id: <%s>' % attachment_id,
      '',
    ])

    # Build the full request
    request_text = '\n'.join([
      '',
      '--%s' % boundary_id,
      soap_headers,
      str(soap_xml),
      '--%s' % boundary_id,
      attachment_headers,
      data,
      '--%s--' % boundary_id
    ])

    # Stuff everything into a request object
    headers = suds_method.client.options.headers.copy()
    headers.update(request_headers)
    request = Request(suds_method.client.options.location, request_text)
    request.headers = headers
    # Send the request
    response = suds_method.client.options.transport.send(request)
    return response