def _run_test(self): """Make an HTTP request and compare the response with expectations.""" test = self.test_data base_url = self.replace_template(test['url']) # Save the URL after replacers but before query_parameters self.url = base_url full_url = self._parse_url(base_url) method = test['method'].upper() headers = test['request_headers'] for name in headers: try: headers[name] = self.replace_template(headers[name]) except TypeError as exc: raise exception.GabbiFormatError( 'malformed headers in test %s: %s' % (test['name'], exc)) if test['data'] != '': body = self._test_data_to_string( test['data'], utils.extract_content_type(headers, default='')[0]) else: body = '' # ensure body is bytes, encoding as UTF-8 because that's # what we do here if isinstance(body, six.text_type): body = body.encode('UTF-8') if test['poll']: count = int( float(self.replace_template(test['poll'].get('count', 1)))) delay = float(self.replace_template(test['poll'].get('delay', 1))) failure = None while count: try: self._run_request(full_url, method, headers, body, redirect=test['redirects']) self._assert_response() failure = None break except (AssertionError, utils.ConnectionRefused) as exc: failure = exc count -= 1 time.sleep(delay) if failure: raise failure else: self._run_request(full_url, method, headers, body, redirect=test['redirects']) self._assert_response()
def _print_body(self, headers, content): """Output body if not binary.""" if self._show_body and utils.not_binary( utils.extract_content_type(headers)[0]): self._verbose_output('') self._verbose_output( utils.decode_response_content(headers, content))
def test_extract_content_type_default_charset(self): """Empty dicts returns default type and chartset.""" content_type, charset = utils.extract_content_type({ 'content-type': 'text/colorful'}) self.assertEqual('text/colorful', content_type) self.assertEqual('utf-8', charset)
def _run_test(self): """Make an HTTP request and compare the response with expectations.""" test = self.test_data base_url = self.replace_template(test['url']) # Save the URL after replacers but before query_parameters self.url = base_url full_url = self._parse_url(base_url) # Replace variables in headers with variable values. This includes both # in the header key and the header value. test['request_headers'] = self._replace_headers_template( test['name'], test['request_headers']) test['response_headers'] = self._replace_headers_template( test['name'], test['response_headers']) method = test['method'].upper() headers = test['request_headers'] if test['data'] != '': body = self._test_data_to_string( test['data'], utils.extract_content_type(headers, default='')[0]) else: body = '' # ensure body is bytes, encoding as UTF-8 because that's # what we do here if isinstance(body, six.text_type): body = body.encode('UTF-8') if test['poll']: count = int(float(self.replace_template( test['poll'].get('count', 1)))) delay = float(self.replace_template(test['poll'].get('delay', 1))) failure = None while count: try: self._run_request(full_url, method, headers, body, redirect=test['redirects']) self._assert_response() failure = None break except (AssertionError, utils.ConnectionRefused) as exc: failure = exc count -= 1 time.sleep(delay) if failure: raise failure else: self._run_request(full_url, method, headers, body, redirect=test['redirects']) self._assert_response()
def __call__(self, test): """Implement our own to make the HTTP call.""" if test.test_data[self._key] and test.test_data[self._key].get( 'paths'): self.preprocess(test) if not isinstance(test.test_data[self._key], type(self.test_key_value)): raise GabbiFormatError( "%s in '%s' has incorrect type, must be %s" % (self._key, test.test_data['name'], type(self.test_key_value))) # This is our additions. paths = test.test_data[self._key]['paths'] source = test.test_data[self._key]['source'] source = test.replace_template(source) parsed_source = urlparse.urlsplit(source) parsed_url = urlparse.urlsplit(test.url) headers = test.test_data['request_headers'] # Replace the test target url's scheme and netloc with the source. full_url = urlparse.urlunsplit( (parsed_source.scheme, parsed_source.netloc, parsed_url.path, parsed_url.query, '')) if test.test_data['data'] != '': body = test._test_data_to_string( test.test_data['data'], utils.extract_content_type(headers, default='')[0]) else: body = '' response, content = test.http.request( full_url, method=test.test_data['method'].upper(), headers=headers, body=body, redirect=test.test_data['redirects']) data = self.json.loads(content) for path in paths: path = test.replace_template(path) source_match = self.json.extract_json_path_value(data, path) target_match = self.json.extract_json_path_value( test.response_data, path) if isinstance(source_match, float) and isinstance( target_match, float): test.assertAlmostEqual(source_match, target_match, msg="Expecting %d, got %d" % (source_match, target_match)) else: test.assertEqual( source_match, target_match, "Expecting %s, got %s" % (source_match, target_match))
def _run_test(self): """Make an HTTP request and compare the response with expectations.""" test = self.test_data base_url = self.replace_template(test['url']) # Save the URL after replacers but before query_parameters self.url = base_url full_url = self._parse_url(base_url) method = test['method'].upper() headers = test['request_headers'] for name in headers: try: headers[name] = self.replace_template(headers[name]) except TypeError as exc: raise exception.GabbiFormatError( 'malformed headers in test %s: %s' % (test['name'], exc)) if test['data'] != '': body = self._test_data_to_string( test['data'], utils.extract_content_type(headers, default='')[0]) else: body = '' if test['poll']: count = test['poll'].get('count', 1) delay = test['poll'].get('delay', 1) failure = None while count: try: self._run_request(full_url, method, headers, body, redirect=test['redirects']) self._assert_response() failure = None break except (AssertionError, utils.ConnectionRefused) as exc: failure = exc count -= 1 time.sleep(delay) if failure: raise failure else: self._run_request(full_url, method, headers, body, redirect=test['redirects']) self._assert_response()
def _print_body(self, headers, content): """Output body if not binary.""" content_type = utils.extract_content_type(headers)[0] if self._show_body and utils.not_binary(content_type): content = utils.decode_response_content(headers, content) # TODO(cdent): Using the JSONHandler here instead of # just the json module to make it clear that eventually # we could pretty print any printable output by using a # handler's loads() and dumps(). Not doing that now # because it would be pointless (no other interesting # handlers) and this approach may be entirely wrong. if jsonhandler.JSONHandler.accepts(content_type): data = jsonhandler.JSONHandler.loads(content) content = jsonhandler.JSONHandler.dumps(data, pretty=True) self._verbose_output('') self._verbose_output(content)
def _print_body(self, headers, content): """Output body if not binary.""" content_type = utils.extract_content_type(headers)[0] if self._show_body and utils.not_binary(content_type): content = utils.decode_response_content(headers, content) # TODO(cdent): Using the JSONHandler here instead of # just the json module to make it clear that eventually # we could pretty print any printable output by using a # handler's loads() and dumps(). Not doing that now # because it would be pointless (no other interesting # handlers) and this approach may be entirely wrong. if content and jsonhandler.JSONHandler.accepts(content_type): data = jsonhandler.JSONHandler.loads(content) content = jsonhandler.JSONHandler.dumps(data, pretty=True) self._verbose_output('') self._verbose_output(content)
def test_extract_content_type_bad_params(self): content_type, charset = utils.extract_content_type( {'content-type': 'text/colorful; version=1.24; charset=latin-10;'}) self.assertEqual('text/colorful', content_type) self.assertEqual('utf-8', charset)
def test_extract_content_type_with_charset(self): content_type, charset = utils.extract_content_type( {'content-type': 'text/colorful; charset=latin-10'}) self.assertEqual('text/colorful', content_type) self.assertEqual('latin-10', charset)
def test_extract_content_type_default_both(self): """Empty dicts returns default type and chartset.""" content_type, charset = utils.extract_content_type({}) self.assertEqual('application/binary', content_type) self.assertEqual('utf-8', charset)