def get_data(self, data_format, transport, add_string=NON_ASCII_STRING, needs_payload=True, payload='', service_class=DummyAdminService): handler = channel.RequestHandler(get_dummy_server()) expected = { 'key': 'a' + uuid4().hex + add_string, 'value': uuid4().hex + NON_ASCII_STRING, 'result': uuid4().hex, 'details': uuid4().hex, 'cid': new_cid(), 'zato':zato_namespace } if needs_payload: if not payload: if data_format == SIMPLE_IO.FORMAT.JSON: payload_value = {expected['key']: expected['value']} else: # NOTE: str.format can't handle Unicode arguments http://bugs.python.org/issue7300 payload_value = """<%(key)s xmlns="%(zato)s">%(value)s<zato_env> <cid>%(cid)s</cid> <result>%(result)s</result> <details>%(details)s</details> </zato_env> </%(key)s>""" % (expected) payload = DummyPayload(payload_value) else: payload = None response = DummyResponse(payload, expected['result'], expected['details']) service = service_class(response, expected['cid']) handler.set_payload(response, data_format, transport, service) return expected, service
def test_create_channel_params(self): url_match = Bunch() url_match.named = Bunch() url_match.named.url_key1 = 'path-{}'.format(uuid4().hex) url_match.named.url_key2 = 'path-{}'.format(uuid4().hex) qs_key1, qs_value1 = 'url_key1', 'qs-aaa-{}'.format(uuid4().hex) qs_key2, qs_value2 = 'url_key2', 'qs-bbbb-{}'.format(uuid4().hex) # Same key, different values qs_key3_1, qs_value3_1 = 'url_key3', 'qs-key3_1-{}'.format(uuid4().hex) qs_key3_2, qs_value3_2 = 'url_key3', 'qs-key3_2-{}'.format(uuid4().hex) post_key1, post_value1 = 'post_key1', uuid4().hex post_key2, post_value2 = 'post_key2', uuid4().hex raw_request = '{}={}&{}={}'.format(post_key1, post_value1, post_key2, post_value2) wsgi_environ = {} wsgi_environ['QUERY_STRING'] = '{}={}&{}={}&{}={}&{}={}'.format( qs_key1, qs_value1, qs_key2, qs_value2, qs_key3_1, qs_value3_1, qs_key3_2, qs_value3_2) for data_format in DATA_FORMAT: for url_params_pri in URL_PARAMS_PRIORITY: channel_item = Bunch() channel_item.data_format = data_format channel_item.url_params_pri = url_params_pri rh = channel.RequestHandler() channel_params = rh.create_channel_params( url_match, channel_item, wsgi_environ, raw_request) get = wsgi_environ['zato.http.GET'] eq_(get[qs_key1], qs_value1) eq_(get[qs_key2], qs_value2) eq_(get[qs_key3_1], [qs_value3_1, qs_value3_2]) eq_(get[qs_key3_2], [qs_value3_1, qs_value3_2]) if data_format == DATA_FORMAT.POST: eq_(sorted(wsgi_environ['zato.http.POST'].items()), [(post_key1, post_value1), (post_key2, post_value2)]) if url_params_pri == URL_PARAMS_PRIORITY.PATH_OVER_QS: eq_(channel_params['url_key1'], url_match.named.url_key1) eq_(channel_params['url_key2'], url_match.named.url_key2) else: eq_(channel_params['url_key1'], qs_value1) eq_(channel_params['url_key2'], qs_value2)
def test_handle(self): expected_cid = uuid4().hex expected_url_match = uuid4().hex expected_wsgi_environ = uuid4().hex expected_raw_request = uuid4().hex expected_simple_io_config = uuid4().hex expected_channel = CHANNEL.HTTP_SOAP expected_channel_item = Bunch() expected_channel_item.service_impl_name = Bunch() expected_channel_item.data_format = uuid4().hex expected_channel_item.transport = uuid4().hex expected_channel_item.params_pri = uuid4().hex expected_worker_store = Bunch() expected_worker_store.broker_client = uuid4().hex expected_channel_params = uuid4().hex def _create_channel_params(url_match, channel_item, wsgi_environ, raw_request, post_data=None): eq_(url_match, expected_url_match) eq_(channel_item, expected_channel_item) eq_(wsgi_environ, expected_wsgi_environ) eq_(raw_request, expected_raw_request) return expected_channel_params for merge_url_params_req in (True, False): expected_channel_item.merge_url_params_req = merge_url_params_req rh = channel.RequestHandler() class _Service: def update_handle(_self, _set_response_data, service, raw_request, channel, data_format, transport, server, broker_client, worker_store, cid, simple_io_config, wsgi_environ, url_match, channel_item, channel_params, merge_channel_params, params_priority): eq_(_set_response_data, rh._set_response_data) eq_(_self, service) eq_(raw_request, expected_raw_request) eq_(channel, expected_channel) eq_(data_format, expected_channel_item.data_format) eq_(transport, expected_channel_item.transport) eq_(server, rh.server) eq_(broker_client, expected_worker_store.broker_client) eq_(sorted(worker_store.items()), sorted(expected_worker_store.items())) eq_(cid, expected_cid) eq_(simple_io_config, expected_simple_io_config) eq_(wsgi_environ, expected_wsgi_environ) eq_(url_match, expected_url_match) eq_(sorted(channel_item.items()), sorted(expected_channel_item.items())) if merge_url_params_req: eq_(channel_params, expected_channel_params) else: eq_(channel_params, None) eq_(merge_channel_params, merge_url_params_req) eq_(params_priority, expected_channel_item.params_pri) class _server: class service_store: @staticmethod def new_instance(service_impl_name): _server.service_impl_name = service_impl_name return _Service() rh.server = _server rh.create_channel_params = _create_channel_params rh.handle(expected_cid, expected_url_match, expected_channel_item, expected_wsgi_environ, expected_raw_request, expected_worker_store, expected_simple_io_config, None)
def test_set_content_type(self): class FakeServer(object): soap11_content_type = 'soap11_content_type-' + rand_string() soap12_content_type = 'soap12_content_type-' + rand_string() plain_xml_content_type = 'plain_xml_content_type-' + rand_string() json_content_type = 'json_content_type-' + rand_string() fs_server_config = Bunch(misc=Bunch(use_soap_envelope=True)) class FakeResponse(object): def __init__(self, content_type_changed=False): self.content_type_changed = content_type_changed self.content_type = ZATO_NONE class FakeChannelItem(object): def __init__(self, soap_version='1.1'): self.soap_version = soap_version rh = channel.RequestHandler(FakeServer()) # # Here are the scenarios we support # # 1) User sets their own content-type # 2) SOAP 1.1 # 3) SOAP 1.2 # 4) Plain XML # 5) JSON # 6) No content-type specified - we need to use default value # # # 1) # response = FakeResponse(True) user_content_type = rand_string() response.content_type = user_content_type rh.set_content_type(response, rand_string(), rand_string(), None, FakeChannelItem()) eq_(response.content_type, user_content_type) # # 2) # response = FakeResponse() rh.set_content_type(response, SIMPLE_IO.FORMAT.XML, URL_TYPE.SOAP, None, FakeChannelItem()) eq_(response.content_type, FakeServer.soap11_content_type) # # 3) # response = FakeResponse() rh.set_content_type(response, SIMPLE_IO.FORMAT.XML, URL_TYPE.SOAP, None, FakeChannelItem('1.2')) eq_(response.content_type, FakeServer.soap12_content_type) # # 4) # response = FakeResponse() rh.set_content_type(response, SIMPLE_IO.FORMAT.XML, URL_TYPE.PLAIN_HTTP, None, FakeChannelItem()) eq_(response.content_type, FakeServer.plain_xml_content_type) # # 5) # response = FakeResponse() rh.set_content_type(response, SIMPLE_IO.FORMAT.JSON, URL_TYPE.PLAIN_HTTP, None, FakeChannelItem()) eq_(response.content_type, FakeServer.json_content_type) # # 6) # response = FakeResponse(False) user_content_type = rand_string() response.content_type = user_content_type rh.set_content_type(response, rand_string(), rand_string(), None, FakeChannelItem()) eq_(response.content_type, user_content_type)