def test_request_blank_cookies(): r = http.Request(('GET / HTTP/1.1\r\n' 'Cookie: \r\n')) assert r.cookies[''] == '' r = http.Request(('GET / HTTP/1.1\r\n' 'Cookie: a=b; ; c=d\r\n')) assert r.cookies[''] == '' r = http.Request(('GET / HTTP/1.1\r\n' 'Cookie: a=b; foo; c=d\r\n')) assert r.cookies['foo'] == ''
def test_request_blank_url_params(): r = http.Request() r.add_line('GET /this/??-asdf/ HTTP/1.1') assert r.full_request == ('GET /this/??-asdf/ HTTP/1.1\r\n\r\n') r = http.Request() r.add_line('GET /this/??-asdf/?a=b&c&d=ef HTTP/1.1') assert r.full_request == ('GET /this/??-asdf/?a=b&c&d=ef HTTP/1.1\r\n\r\n') assert r.url_params['?-asdf/?a'] == 'b' assert r.url_params['c'] == None assert r.url_params['d'] == 'ef'
def test_request_set_url(): r = http.Request('GET / HTTP/1.1\r\n') r.url = 'www.AAAA.BBBB' assert r.host == 'www.AAAA.BBBB' assert r.port == 80 assert not r.is_ssl r.url = 'https://www.AAAA.BBBB' assert r.host == 'www.AAAA.BBBB' assert r.port == 443 assert r.is_ssl r.url = 'https://www.AAAA.BBBB:1234' assert r.host == 'www.AAAA.BBBB' assert r.port == 1234 assert r.is_ssl r.url = 'http://www.AAAA.BBBB:443' assert r.host == 'www.AAAA.BBBB' assert r.port == 443 assert not r.is_ssl r.url = 'www.AAAA.BBBB:443' assert r.host == 'www.AAAA.BBBB' assert r.port == 443 assert r.is_ssl
def test_request_modify_header2(): r = http.Request(( 'POST /some/path HTTP/1.1\r\n' 'Host: test.host.thing\r\n' 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:43.0) Gecko/20100101 Firefox/43.0\r\n' 'Content-Length: 282\r\n' 'Connection: keep-alive\r\n' '\r\n' 'a|b|c|d')) r2 = r.copy() r2.headers['User-Agent'] = 'Moziller/6.9' assert r2.full_message == ('POST /some/path HTTP/1.1\r\n' 'Host: test.host.thing\r\n' 'User-Agent: Moziller/6.9\r\n' 'Content-Length: 7\r\n' 'Connection: keep-alive\r\n' '\r\n' 'a|b|c|d') r2.post_params['foo'] = 'barr' assert r2.full_message == ( 'POST /some/path HTTP/1.1\r\n' 'Host: test.host.thing\r\n' 'User-Agent: Moziller/6.9\r\n' 'Content-Length: 8\r\n' 'Connection: keep-alive\r\n' 'Content-Type: application/x-www-form-urlencoded\r\n' '\r\n' 'foo=barr')
def test_request_blank_headers(): r = http.Request(('GET / HTTP/1.1\r\n' 'Header: \r\n' 'Header2:\r\n')) assert r.headers['header'] == '' assert r.headers['header2'] == ''
def test_request_to_json(): r = http.Request() r.start_line = 'GET / HTTP/1.1' r.headers['content-length'] = 500 r.tags = {'foo', 'bar'} r.body = 'AAAA' r.reqid = '1' rsp = http.Response() rsp.start_line = 'HTTP/1.1 200 OK' rsp.rspid = '2' r.response = rsp expected_reqdata = { 'full_message': unicode(base64.b64encode(r.full_request)), 'response_id': str(rsp.rspid), 'port': 80, 'is_ssl': False, 'tags': ['foo', 'bar'], 'reqid': str(r.reqid), 'host': '', } assert json.loads(r.to_json()) == expected_reqdata
def test_request_connect_request(): r = http.Request(('GET /foo/path HTTP/1.1\r\n' 'Host: www.example.faketld\r\n\r\n')) assert r.connect_request == None r.is_ssl = True assert r.connect_request.full_message == ( 'CONNECT www.example.faketld:443 HTTP/1.1\r\n' 'Host: www.example.faketld\r\n\r\n')
def test_proxy_auth(): r = http.Request(('GET /foo/path HTTP/1.1\r\n' 'Host: www.example.faketld\r\n\r\n')) r.proxy_creds = ('username', 'pass:word') assert r.full_message == ( 'GET /foo/path HTTP/1.1\r\n' 'Host: www.example.faketld\r\n' 'Proxy-Authorization: Basic dXNlcm5hbWU6cGFzczp3b3Jk\r\n\r\n')
def test_request_copy(): r = http.Request(('GET / HTTP/1.1\r\n' 'Content-Length: 4\r\n\r\n' 'AAAA')) r2 = copy.copy(r) assert r2.full_request == ('GET / HTTP/1.1\r\n' 'Content-Length: 4\r\n\r\n' 'AAAA')
def test_request_update_content_length(): r = http.Request(('GET / HTTP/1.1\r\n' 'Content-Length: 4\r\n\r\n' 'AAAAAAAAAA'), update_content_length=True) assert r.full_request == (('GET / HTTP/1.1\r\n' 'Content-Length: 10\r\n\r\n' 'AAAAAAAAAA'))
def test_request_url_blankpath(): r = http.Request() r.start_line = 'GET / HTTP/1.1' r.url = 'https://www.google.com' r.headers['Host'] = r.host r.url_params.from_dict({'foo': 'bar'}) assert r.full_path == '/?foo=bar' assert r.url == 'https://www.google.com?foo=bar'
def test_request_update_data(): r = http.Request() r.start_line = 'GET / HTTP/1.1' r.headers['content-length'] = 500 r.body = 'AAAA' assert r.full_request == ('GET / HTTP/1.1\r\n' 'content-length: 4\r\n' '\r\n' 'AAAA')
def test_request_update_statusline(): r = http.Request() r.start_line = 'GET / HTTP/1.1' assert r.verb == 'GET' assert r.path == '/' assert r.version == 'HTTP/1.1' assert not r.complete assert r.full_request == 'GET / HTTP/1.1\r\n\r\n'
def test_request_connect_request_creds(): r = http.Request(('GET /foo/path HTTP/1.1\r\n' 'Host: www.example.faketld\r\n\r\n')) r.is_ssl = True r.proxy_creds = ('username', 'pass:word') assert r.connect_request.full_message == ( 'CONNECT www.example.faketld:443 HTTP/1.1\r\n' 'Host: www.example.faketld\r\n' 'Proxy-Authorization: Basic dXNlcm5hbWU6cGFzczp3b3Jk\r\n\r\n')
def test_request_set_url_params(): r = http.Request('GET / HTTP/1.1\r\n') r.url = 'www.AAAA.BBBB?a=b&c=d#foo' assert r.url_params.all_pairs() == [('a', 'b'), ('c', 'd')] assert r.fragment == 'foo' assert r.url == 'http://www.AAAA.BBBB?a=b&c=d#foo' r.port = 400 assert r.url == 'http://www.AAAA.BBBB:400?a=b&c=d#foo' r.is_ssl = True assert r.url == 'https://www.AAAA.BBBB:400?a=b&c=d#foo'
def test_request_newline_delim(): r = http.Request(('GET / HTTP/1.1\n' 'Content-Length: 4\n' 'Test-Header: foo\r\n' 'Other-header: bar\n\r\n' 'AAAA')) assert r.full_request == ('GET / HTTP/1.1\r\n' 'Content-Length: 4\r\n' 'Test-Header: foo\r\n' 'Other-header: bar\r\n\r\n' 'AAAA')
def test_request_modified_headers(): r = http.Request() r.start_line = 'GET / HTTP/1.1' r.headers['content-length'] = '100' r.headers['cookie'] = 'abc=123' r.cookies['abc'] = '456' r.body = 'AAAA' assert r.full_request == ('GET / HTTP/1.1\r\n' 'content-length: 4\r\n' 'cookie: abc=456\r\n\r\n' 'AAAA') assert r.headers['content-length'] == '4' assert r.headers['cookie'] == 'abc=456'
def test_post_params_update(): r = http.Request(('GET / HTTP/1.1\r\n' 'Content-Type: application/x-www-form-urlencoded\r\n' 'Content-Length: 7\r\n\r\n' 'a=b&c=d')) r.post_params['c'] = 'e' assert r.full_request == ('GET / HTTP/1.1\r\n' 'Content-Type: application/x-www-form-urlencoded\r\n' 'Content-Length: 7\r\n\r\n' 'a=b&c=e') r.post_params['a'] = 'f' assert r.full_request == ('GET / HTTP/1.1\r\n' 'Content-Type: application/x-www-form-urlencoded\r\n' 'Content-Length: 7\r\n\r\n' 'a=f&c=e')
def test_request_update_headers(): r = http.Request() r.start_line = 'GET / HTTP/1.1' r.headers['Content-Length'] = '0' r.headers['Test-Header'] = 'Test Value' r.headers['Other-Header'] = 'Other Value' r.headers['Host'] = 'www.test.com' r.headers.append('Test-Header', 'Test Value2') assert r.full_request == ('GET / HTTP/1.1\r\n' 'Content-Length: 0\r\n' 'Test-Header: Test Value\r\n' 'Other-Header: Other Value\r\n' 'Host: www.test.com\r\n' 'Test-Header: Test Value2\r\n' '\r\n') assert r.host == 'www.test.com'
def test_request_absolute_url(): r = http.Request(('GET /foo/path HTTP/1.1\r\n' 'Host: www.example.faketld\r\n\r\n')) assert r.full_message == ('GET /foo/path HTTP/1.1\r\n' 'Host: www.example.faketld\r\n' '\r\n') r.path_type = http.PATH_ABSOLUTE assert r.full_message == ( 'GET http://www.example.faketld/foo/path HTTP/1.1\r\n' 'Host: www.example.faketld\r\n' '\r\n') r.is_ssl = True assert r.full_message == ( 'GET https://www.example.faketld/foo/path HTTP/1.1\r\n' 'Host: www.example.faketld\r\n' '\r\n')
def test_headers_end(): header_lines = [ 'GET / HTTP/1.1', 'Content-Type: text/xml; charset="utf-8"', 'Accept-Encoding: gzip,deflate', 'User-Agent: TestAgent', 'Host: www.test.com', 'Content-Length: 100', 'Connection: Keep-Alive', 'Cache-Control: no-cache', '', ] r = http.Request() for l in header_lines: r.add_line(l) assert not r.complete assert r.headers_complete
def test_request_update_cookies(): r = http.Request() r.start_line = 'GET / HTTP/1.1' # Check new cookies r.cookies['foo'] = 'bar' r.cookies['baz'] = 'fuzz' assert r.full_request == ('GET / HTTP/1.1\r\n' 'Cookie: foo=bar; baz=fuzz\r\n' '\r\n') # Check updated cookies (should be updated in place) r.cookies['foo'] = 'buzz' assert r.full_request == ('GET / HTTP/1.1\r\n' 'Cookie: foo=buzz; baz=fuzz\r\n' '\r\n') # Check repeated cookies r.cookies.append('foo', 'bar') assert r.full_request == ('GET / HTTP/1.1\r\n' 'Cookie: foo=buzz; baz=fuzz; foo=bar\r\n' '\r\n')
def test_request_to_json(): r = http.Request() r.status_line = 'GET / HTTP/1.1' r.headers['content-length'] = 500 r.tags = ['foo', 'bar'] r.raw_data = 'AAAA' r.reqid = '1' rsp = http.Response() rsp.status_line = 'HTTP/1.1 200 OK' rsp.rspid = '2' r.response = rsp expected_reqdata = {u'full_request': unicode(base64.b64encode(r.full_request)), u'response_id': str(rsp.rspid), u'port': 80, u'is_ssl': False, u'tags': ['foo', 'bar'], u'reqid': str(r.reqid), } assert json.loads(r.to_json()) == expected_reqdata
def test_request_blank(): r = http.Request('\r\n\n\n') assert r.full_request == ''