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 _replace_headers_template(self, test_name, headers): replaced_headers = {} try: for name in headers: replaced_name = self.replace_template(name) replaced_headers[replaced_name] = self.replace_template( headers[name]) except TypeError as exc: raise exception.GabbiFormatError( 'malformed headers in test %s: %s' % (test_name, exc)) return replaced_headers