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")
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")
def test_request_as_string(message): request = Request("my url", message) request.headers["aaa"] = 1 expected = u"""\ URL: my url HEADERS: %s MESSAGE: %s""" % (request.headers, message) assert unicode(request) == expected if sys.version_info < (3, 0): assert str(request) == expected.encode("utf-8")
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
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
def test_construct(self, message): # Always use the same URL as different ways to specify a Request's URL # are tested separately. url = "some://url" request = Request(url, message) assert request.url is url assert request.message is message assert request.headers == {}
def test_request_constructor(url, message): request = Request(url, message) assert request.url == url assert request.message == message assert request.headers == {}
def test_request_without_message(): request = Request("for a bitch it's haaaard...") assert request.url == "for a bitch it's haaaard..." assert request.message is None assert request.headers == {}
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
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")
def test_open(self): """ Verify the open method calls the API and returns the response content. """ request = Request(API_URL) body = str(uuid.uuid4()) httpretty.register_uri(httpretty.GET, API_URL, body=body, content_type=CONTENT_TYPE) transport = RequestsTransport() response = transport.open(request).getvalue() self.assertEqual(response, body)
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")
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)
def download(self, url): """ Download the docuemnt. @param url: A document url. @type url: str. @return: A file pointer to the docuemnt. @rtype: file-like """ store = DocumentStore() fp = store.open(url) if fp is None: fp = self.options.transport.open(Request(url)) sax = Parser() return sax.parse(file=fp)
def test_URL_null_bytes(self, url): """ Transport Request accepts its URL as either a byte or a unicode string. Internally URL information is kept as the native Python str type. """ request = Request(url) assert isinstance(request.url, str) if url.__class__ is str: assert request.url is url elif url.__class__ is u: assert request.url == url.encode("ascii") # Python 2. else: assert request.url == url.decode("ascii") # Python 3.
def download(self, url): """ Download the docuemnt. @param url: A document url. @type url: str. @return: A file pointer to the docuemnt. @rtype: file-like """ store = DocumentStore() fp = store.open(url) if fp is None: fp = self.options.transport.open(Request(url)) content = fp.read() fp.close() ctx = self.plugins.document.loaded(url=url, document=content) content = ctx.document sax = Parser() return sax.parse(string=content)
def test_send(self): """ Verify the send method POSTs data to the API and returns a Reply object. """ request = Request(API_URL) body = str(uuid.uuid4()) httpretty.register_uri(httpretty.POST, API_URL, body=body, content_type=CONTENT_TYPE, forcing_headers={'date': 'never'}) transport = RequestsTransport() response = transport.send(request) self.assertEqual(response.code, 200) self.assertEqual(response.headers, { 'date': 'never', 'content-type': CONTENT_TYPE }) self.assertEqual(response.message, body)
def open(self, url): """ Open an XML document at the specified I{url}. First, the document attempted to be retrieved from the I{document cache}. If not found, it is downloaded and parsed using the SAX parser. The result is added to the document store for the next open(). @param url: A document url. @type url: str. @return: The specified XML document. @rtype: I{Document} """ d = self.options.cache.get(url) if d is None: fp = self.options.transport.open(Request(url)) sax = Parser() d = sax.parse(file=fp) #self.options.cache.put(url, d) return d
def parse(self, file=None, url=None, string=None): """ parse a document """ handler = Handler() timer = metrics.Timer() timer.start() if file is not None: parse(file, handler) timer.stop() metrics.log.debug('sax (%s) duration: %s', file, timer) return handler.nodes[0] if url is not None: fp = self.transport.open(Request(url)) parse(fp, handler) timer.stop() metrics.log.debug('sax (%s) duration: %s', url, timer) return handler.nodes[0] if string is not None: parseString(string, handler) timer.stop() metrics.log.debug('%s\nsax duration: %s', string, timer) return handler.nodes[0]
def download(self, url): """ Download the document. @param url: A document URL. @type url: str. @return: A file pointer to the document. @rtype: file-like """ content = None store = self.options.documentStore if store is not None: content = store.open(url) if content is None: fp = self.options.transport.open(Request(url)) try: content = fp.read() finally: fp.close() ctx = self.plugins.document.loaded(url=url, document=content) content = ctx.document sax = Parser() return sax.parse(string=content)
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
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
def test_construct_with_no_message(self): request = Request("some://url") assert request.headers == {} assert request.message is None