def test_http_request(): with PluginTester(): print "Testing a simple GET request..." request = HTTP_Request("http://www.example.com/index.html") assert request.method == "GET" assert request.url == "http://www.example.com/index.html" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.1" assert isinstance(request.headers, HTTP_Headers) assert request.post_data == None assert request.request_uri == "/index.html" assert request.hostname == "www.example.com" assert request.user_agent == "Mozilla/5.0 (compatible, GoLismero/2.0 The Web Knife; +https://github.com/golismero/golismero)" assert request.accept_language == "en-US" assert request.accept == "*/*" assert request.referer == None assert request.cookie == None assert request.content_type == None assert request.content_length == None print "Testing a simple POST request..." request = HTTP_Request("http://www.example.com/form.php", post_data="hola=manola") assert request.method == "POST" assert request.url == "http://www.example.com/form.php" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.1" assert isinstance(request.headers, HTTP_Headers) assert request.post_data == "hola=manola" assert request.request_uri == "/form.php" assert request.hostname == "www.example.com" assert request.user_agent == "Mozilla/5.0 (compatible, GoLismero/2.0 The Web Knife; +https://github.com/golismero/golismero)" assert request.accept_language == "en-US" assert request.accept == "*/*" assert request.referer == None assert request.cookie == None assert request.content_type == "application/x-www-form-urlencoded" assert request.content_length == len("hola=manola") print "Testing a custom GET request (1)..." t_headers = (("Cookie", "lala=pepe"), ("Referer", "http://www.example.com/")) request = HTTP_Request("http://www.example.com/index.html", headers=t_headers, version="1.0") assert request.method == "GET" assert request.url == "http://www.example.com/index.html" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.0" assert request.headers.to_tuple() == t_headers assert request.post_data == None assert request.request_uri == "/index.html" assert request.hostname == None assert request.user_agent == None assert request.accept_language == None assert request.accept == None assert request.referer == "http://www.example.com/" assert request.cookie == "lala=pepe" assert request.content_type == None assert request.content_length == None print "Testing a custom GET request (2)..." d_headers = { "Cookie": "lala=pepe", "Referer": "http://www.example.com/" } request = HTTP_Request("http://www.example.com/index.html", headers=d_headers, version="1.0") assert request.method == "GET" assert request.url == "http://www.example.com/index.html" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.0" assert request.headers.to_tuple() == tuple(sorted(d_headers.items())) assert request.post_data == None assert request.request_uri == "/index.html" assert request.hostname == None assert request.user_agent == None assert request.accept_language == None assert request.accept == None assert request.referer == "http://www.example.com/" assert request.cookie == "lala=pepe" assert request.content_type == None assert request.content_length == None print "Testing a custom GET request (3)..." o_headers = HTTP_Headers.from_items(t_headers) request = HTTP_Request("http://www.example.com/form.php?hola=manola", headers=o_headers, version="1.0") assert request.method == "GET" assert request.url == "http://www.example.com/form.php?hola=manola" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.0" assert request.headers is o_headers assert request.post_data == None assert request.request_uri == "/form.php?hola=manola" assert request.hostname == None assert request.user_agent == None assert request.accept_language == None assert request.accept == None assert request.referer == "http://www.example.com/" assert request.cookie == "lala=pepe" assert request.content_type == None assert request.content_length == None print "Testing a custom POST request..." request = HTTP_Request("http://www.example.com/form.php", post_data="hola=manola", headers=t_headers, version="1.0") assert request.method == "POST" assert request.url == "http://www.example.com/form.php" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.0" assert request.headers.to_tuple() == t_headers assert request.post_data == "hola=manola" assert request.request_uri == "/form.php" assert request.hostname == None assert request.user_agent == None assert request.accept_language == None assert request.accept == None assert request.referer == "http://www.example.com/" assert request.cookie == "lala=pepe" assert request.content_type == None assert request.content_length == None
def test_http_request(): with PluginTester(): print "Testing a simple GET request..." request = HTTP_Request("http://www.example.com/index.html") assert request.method == "GET" assert request.url == "http://www.example.com/index.html" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.1" assert isinstance(request.headers, HTTP_Headers) assert request.post_data == None assert request.request_uri == "/index.html" assert request.hostname == "www.example.com" assert request.user_agent == "Mozilla/5.0 (compatible, GoLismero/2.0 The Web Knife; +https://github.com/golismero/golismero)" assert request.accept_language == "en-US" assert request.accept == "*/*" assert request.referer == None assert request.cookie == None assert request.content_type == None assert request.content_length == None print "Testing a simple POST request..." request = HTTP_Request("http://www.example.com/form.php", post_data="hola=manola") assert request.method == "POST" assert request.url == "http://www.example.com/form.php" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.1" assert isinstance(request.headers, HTTP_Headers) assert request.post_data == "hola=manola" assert request.request_uri == "/form.php" assert request.hostname == "www.example.com" assert request.user_agent == "Mozilla/5.0 (compatible, GoLismero/2.0 The Web Knife; +https://github.com/golismero/golismero)" assert request.accept_language == "en-US" assert request.accept == "*/*" assert request.referer == None assert request.cookie == None assert request.content_type == "application/x-www-form-urlencoded" assert request.content_length == len("hola=manola") print "Testing a custom GET request (1)..." t_headers = (("Cookie", "lala=pepe"), ("Referer", "http://www.example.com/"), ("User-Agent", "Test User Agent")) request = HTTP_Request("http://www.example.com/index.html", headers=t_headers, version="1.0") assert request.method == "GET" assert request.url == "http://www.example.com/index.html" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.0" assert request.headers.to_tuple() == t_headers assert request.post_data == None assert request.request_uri == "/index.html" assert request.hostname == None assert request.user_agent == "Test User Agent" assert request.accept_language == None assert request.accept == None assert request.referer == "http://www.example.com/" assert request.cookie == "lala=pepe" assert request.content_type == None assert request.content_length == None print "Testing a custom GET request (2)..." d_headers = {"Cookie": "lala=pepe", "Referer": "http://www.example.com/", "User-Agent": "Test User Agent"} request = HTTP_Request("http://www.example.com/index.html", headers=d_headers, version="1.0") assert request.method == "GET" assert request.url == "http://www.example.com/index.html" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.0" assert request.headers.to_tuple() == tuple(sorted(d_headers.items())) assert request.post_data == None assert request.request_uri == "/index.html" assert request.hostname == None assert request.user_agent == "Test User Agent" assert request.accept_language == None assert request.accept == None assert request.referer == "http://www.example.com/" assert request.cookie == "lala=pepe" assert request.content_type == None assert request.content_length == None print "Testing a custom GET request (3)..." o_headers = HTTP_Headers.from_items(t_headers) request = HTTP_Request("http://www.example.com/form.php?hola=manola", headers=o_headers, version="1.0") assert request.method == "GET" assert request.url == "http://www.example.com/form.php?hola=manola" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.0" assert request.headers is o_headers assert request.post_data == None assert request.request_uri == "/form.php?hola=manola" assert request.hostname == None assert request.user_agent == "Test User Agent" assert request.accept_language == None assert request.accept == None assert request.referer == "http://www.example.com/" assert request.cookie == "lala=pepe" assert request.content_type == None assert request.content_length == None print "Testing a custom POST request..." request = HTTP_Request("http://www.example.com/form.php", post_data="hola=manola", headers=t_headers, version="1.0") assert request.method == "POST" assert request.url == "http://www.example.com/form.php" assert isinstance(request.parsed_url, ParsedURL) assert request.protocol == "HTTP" assert request.version == "1.0" assert request.headers.to_tuple() == t_headers assert request.post_data == "hola=manola" assert request.request_uri == "/form.php" assert request.hostname == None assert request.user_agent == "Test User Agent" assert request.accept_language == None assert request.accept == None assert request.referer == "http://www.example.com/" assert request.cookie == "lala=pepe" assert request.content_type == None assert request.content_length == None
def test_http_headers(): with PluginTester(): # Test the methods. print "Testing HTTP_Header() methods..." raw_headers = ("Host: www.example.com\r\n" "Connection: keep-alive\r\n" "Content-Length: 0\r\n" "Content-Encoding: plain\r\n" "Transport-Encoding: plain\r\n" "Pragma: no-cache\r\n" "\r\n") headers = HTTP_Headers(raw_headers) assert str(headers) == raw_headers assert headers["cOnNeCtIoN"] == "keep-alive" assert headers.get("FAKE", "fake") == "fake" assert headers.get("NotHere") == None try: print headers["lalalala"] assert False except KeyError: pass try: print headers.get(object()) assert False except TypeError: pass try: print headers.get(object(), "lalala") assert False except TypeError: pass try: print headers[object()] assert False except TypeError: pass assert headers[0] == "Host: www.example.com\r\n" assert headers[3] == "Content-Encoding: plain\r\n" assert headers[-1] == "Pragma: no-cache\r\n" try: print headers[6] assert False except IndexError: pass try: print headers[-7] assert False except IndexError: pass assert headers[ 1:3] == "Connection: keep-alive\r\nContent-Length: 0\r\n" assert headers[ -3: -1] == "Content-Encoding: plain\r\nTransport-Encoding: plain\r\n" assert headers[ 3:] == "Content-Encoding: plain\r\nTransport-Encoding: plain\r\nPragma: no-cache\r\n" assert headers[ -2:] == "Transport-Encoding: plain\r\nPragma: no-cache\r\n" assert headers[-100:] == raw_headers[:-2] assert not headers[:-7] assert not headers[6:] assert headers[:100] == raw_headers[:-2] assert list(headers) == [ "Host: www.example.com\r\n", "Connection: keep-alive\r\n", "Content-Length: 0\r\n", "Content-Encoding: plain\r\n", "Transport-Encoding: plain\r\n", "Pragma: no-cache\r\n", ] assert list(headers.iterkeys()) == [ "Host", "Connection", "Content-Length", "Content-Encoding", "Transport-Encoding", "Pragma", ] assert list(headers.itervalues()) == [ "www.example.com", "keep-alive", "0", "plain", "plain", "no-cache", ] assert list(headers.iteritems()) == [ ("Host", "www.example.com"), ("Connection", "keep-alive"), ("Content-Length", "0"), ("Content-Encoding", "plain"), ("Transport-Encoding", "plain"), ("Pragma", "no-cache"), ] original = sorted(headers.to_tuple()) orig_dict = dict(original) headers = HTTP_Headers.from_items(original) assert headers.to_tuple() == tuple(original) parsed = headers.to_dict() headers = HTTP_Headers.from_items(orig_dict.items()) assert sorted(headers.to_tuple()) == original assert headers.to_dict() == parsed headers = HTTP_Headers.from_items(HTTP_Request.DEFAULT_HEADERS) assert headers.to_tuple() == HTTP_Request.DEFAULT_HEADERS parsed = headers.to_dict() headers = HTTP_Headers.from_items(sorted(parsed.items())) assert headers.to_dict() == parsed assert headers.to_tuple() == tuple(sorted(parsed.items())) # Run parser test cases. for title, raw_headers, original, parsed in cases_http_headers: print "Testing parser with %s..." % title headers = HTTP_Headers(raw_headers) assert str(headers) == raw_headers assert headers.to_tuple() == original assert headers.to_dict() == parsed headers = HTTP_Headers.from_items(original) assert headers.to_tuple() == original assert headers.to_dict() == parsed headers = HTTP_Headers.from_items(parsed.items()) assert headers.to_dict() == parsed
def test_http_headers(): with PluginTester(): # Test the methods. print "Testing HTTP_Header() methods..." raw_headers = ( "Host: www.example.com\r\n" "Connection: keep-alive\r\n" "Content-Length: 0\r\n" "Content-Encoding: plain\r\n" "Transport-Encoding: plain\r\n" "Pragma: no-cache\r\n" "\r\n" ) headers = HTTP_Headers(raw_headers) assert str(headers) == raw_headers assert headers["cOnNeCtIoN"] == "keep-alive" assert headers.get("FAKE", "fake") == "fake" assert headers.get("NotHere") == None try: print headers["lalalala"] assert False except KeyError: pass try: print headers.get(object()) assert False except TypeError: pass try: print headers.get(object(), "lalala") assert False except TypeError: pass try: print headers[object()] assert False except TypeError: pass assert headers[0] == "Host: www.example.com\r\n" assert headers[3] == "Content-Encoding: plain\r\n" assert headers[-1] == "Pragma: no-cache\r\n" try: print headers[6] assert False except IndexError: pass try: print headers[-7] assert False except IndexError: pass assert headers[1:3] == "Connection: keep-alive\r\nContent-Length: 0\r\n" assert headers[-3:-1] == "Content-Encoding: plain\r\nTransport-Encoding: plain\r\n" assert headers[3:] == "Content-Encoding: plain\r\nTransport-Encoding: plain\r\nPragma: no-cache\r\n" assert headers[-2:] == "Transport-Encoding: plain\r\nPragma: no-cache\r\n" assert headers[-100:] == raw_headers[:-2] assert not headers[:-7] assert not headers[6:] assert headers[:100] == raw_headers[:-2] assert list(headers) == [ "Host: www.example.com\r\n", "Connection: keep-alive\r\n", "Content-Length: 0\r\n", "Content-Encoding: plain\r\n", "Transport-Encoding: plain\r\n", "Pragma: no-cache\r\n", ] assert list(headers.iterkeys()) == [ "Host", "Connection", "Content-Length", "Content-Encoding", "Transport-Encoding", "Pragma", ] assert list(headers.itervalues()) == [ "www.example.com", "keep-alive", "0", "plain", "plain", "no-cache", ] assert list(headers.iteritems()) == [ ("Host", "www.example.com"), ("Connection", "keep-alive"), ("Content-Length", "0"), ("Content-Encoding", "plain"), ("Transport-Encoding", "plain"), ("Pragma", "no-cache"), ] original = sorted(headers.to_tuple()) orig_dict = dict(original) headers = HTTP_Headers.from_items(original) assert headers.to_tuple() == tuple(original) parsed = headers.to_dict() headers = HTTP_Headers.from_items(orig_dict.items()) assert sorted(headers.to_tuple()) == original assert headers.to_dict() == parsed headers = HTTP_Headers.from_items(HTTP_Request.DEFAULT_HEADERS) assert headers.to_tuple() == HTTP_Request.DEFAULT_HEADERS parsed = headers.to_dict() headers = HTTP_Headers.from_items(sorted(parsed.items())) assert headers.to_dict() == parsed assert headers.to_tuple() == tuple(sorted(parsed.items())) # Run parser test cases. for title, raw_headers, original, parsed in cases_http_headers: print "Testing parser with %s..." % title headers = HTTP_Headers(raw_headers) assert str(headers) == raw_headers assert headers.to_tuple() == original assert headers.to_dict() == parsed headers = HTTP_Headers.from_items(original) assert headers.to_tuple() == original assert headers.to_dict() == parsed headers = HTTP_Headers.from_items(parsed.items()) assert headers.to_dict() == parsed