def test_parse_headers(self): request_text = (b'GET /who/ken/trust.html HTTP/1.1\r\n' b'Host: cm.bell-labs.com\r\n' b'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n' b'Accept: text/html;q=0.9,text/plain\r\n' b'\r\n') result_list = Parser.parse_headers(request_text) self.assertEqual(3, len(result_list)) self.assertEqual(('host', 'cm.bell-labs.com'), result_list[0]) request_text = "" result_list = Parser.parse_headers(request_text) self.assertEqual(0, len(result_list)) request_text = '\r\n' result_list = Parser.parse_headers(request_text) self.assertEqual(0, len(result_list))
def run(self, context=None, timeout=None, curl_handler=None): if context is None: context = self.__context self.pre_update(context) self.render() if timeout is None: timeout = DEFAULT_TIMEOUT if curl_handler: try: # Check the curl handle isn't closed, and reuse it if possible curl_handler.getinfo(curl_handler.HTTP_CODE) # Below clears the cookies & curl options for clean run # But retains the DNS cache and connection pool curl_handler.reset() curl_handler.setopt(curl_handler.COOKIELIST, "ALL") except pycurl.error: curl_handler = pycurl.Curl() else: curl_handler = pycurl.Curl() body_byte, header_byte = self.__default_curl_config( curl_handler, timeout) if self.config.timeout: curl_handler.setopt(pycurl.CONNECTTIMEOUT, self.config.timeout) if self.__ssl_insecure: curl_handler.setopt(pycurl.SSL_VERIFYPEER, 0) curl_handler.setopt(pycurl.SSL_VERIFYHOST, 0) if self.body: logger.debug("Request body %s" % self.body) curl_handler.setopt(curl_handler.READFUNCTION, BytesIO(bytes(self.body, 'utf-8')).read) if self.auth_username and self.auth_password: curl_handler.setopt(pycurl.USERPWD, self.auth_username + ':' + self.auth_password) self.__configure_curl_method(curl_handler) head = self.headers self.__configure_curl_headers(curl_handler, head) if self.__delay: time.sleep(self.__delay) try: logger.info("Hitting %s" % self.url) curl_handler.perform() except pycurl.error as e: logger.error("Unknown Exception", exc_info=True) self.__passed = False curl_handler.close() trace = traceback.format_exc() self.__failure_list.append( Failure(message="Curl Exception: {0}".format(e), details=trace, failure_type=FAILURE_CURL_EXCEPTION)) return self.body = body_byte.getvalue() body_byte.close() response_code = curl_handler.getinfo(pycurl.RESPONSE_CODE) self.__response_code = int(response_code) if self.config.print_bodies: print(self.body) try: response_headers = Parser.parse_headers(header_byte.getvalue()) self.__response_headers = response_headers logger.debug("RESPONSE HEADERS: %s" % self.__response_headers) header_byte.close() except Exception as e: # Need to catch the expected exception trace = traceback.format_exc() self.__failure_list.append( Failure(message="Header parsing exception: {0}".format(e), details=trace, failure_type=FAILURE_TEST_EXCEPTION)) self.__passed = False curl_handler.close() return if self.__response_code in self.expected_http_status_code_list: self.__passed = True self.__failure_list.extend(self.__perform_validation()) self.post_update(context) else: self.__passed = False failure_message = "Invalid HTTP response code: response code {0} not in expected codes {1}".format( self.__response_code, self.expected_http_status_code_list) self.__failure_list.append( Failure(message=failure_message, details=None, failure_type=FAILURE_INVALID_RESPONSE)) curl_handler.close()