def test_http_method(self): '''*method* kwarg effect. ''' hc = HttpCompiler() headers = { 'Host': 'httpbin.org', 'Connection': 'close', } qs = '/path/to/some/resource' req = hc.build_raw(qs, method='GET', headers=headers) self.assertTrue(req.split('\r\n')[0].split(' ')[0] == 'GET')
def main(): # case #1: basic query_strings = [ '/api/v1/you', '/api/v1/can?run=false', '/api/v1/can?hide=true', ] headers = {'Host': 'some.cool.org', 'Connection': 'close'} sys.stdout.write('Minimalistic HTTP GET request\n') sys.stdout.write('-' * 80 + '\n') hc = HttpCompiler(method='GET', headers=headers) #hc = HttpCompiler() for qs in query_strings: sys.stdout.write(hc.build_phantom(qs)) # case #2: advanced sys.stdout.write('Slightly more complicated request\n') sys.stdout.write('-' * 80 + '\n') body = {'USERNAME': '******', 'PASSWORD': '******'} headers = { 'Host': 'some.cool.org', 'Content-type': 'application/x-www-form-urlencoded', 'X-custom-header': 'some-value', 'Connection': 'close' } method = 'PATCH' body_encoded = urllib.urlencode(body) query_strings = [] for id in range(3): params = urllib.urlencode({'where': '^right there', 'id': id}) query_strings.append('%s?%s' % ('/some/path', params)) hc = HttpCompiler(method=method, headers=headers) for qs in query_strings: sys.stdout.write(hc.build_phantom(qs, body=body_encoded)) # case #3: advanced reqs = [ ('/path/to/resource', { 'method': 'GET', 'headers': { 'Host': 'dom1.test.net', 'Connection': 'close' }, }), ('/just/one/more/resource', { 'method': 'POST', 'headers': { 'Host': 'dom2.test.net', 'Content-type': 'application/json' }, 'body': '{"a": "b"}', }), ] hc = HttpCompiler() for r in reqs: sys.stdout.write(hc.build_phantom(r[0], **r[1]))
def test_phantom_format(self): ''' Request size calculation. ''' headers = { 'Host': 'httpbin.org', 'Connection': 'close', } hc = HttpCompiler(method='PATCH', headers=headers) qs = '/path/to/check' req = hc.build_phantom(qs, body='Some stuff') p_len, p_req = req.split('\n', 1) self.assertTrue(int(p_len) == (len(p_req) - 1))
def test_eof(self): ''' Line endings satisfying RFC 2616. ''' hc = HttpCompiler() headers = { 'Host': 'httpbin.org', 'Connection': 'close', } qs = '/path/to/some/resource' req = hc.build_raw(qs, method='GET', headers=headers) # Two lines at the end of request self.assertTrue(req.endswith('\r\n\r\n')) # Each line ends with <CR><LF> self.assertTrue(all([l.endswith('\r') for l in req.split('\n') if l]))
def test_httpbin_resp(self): '''Check HTTP status code of responce from real public server. ''' hc = HttpCompiler() headers = { 'Host': 'httpbin.org', 'Connection': 'close', } qs = '/ip' req = hc.build_raw(qs, method='GET', headers=headers) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(TEST_SERV_ADDR) s.send(req) #s.send('GET / HTTP/1.1\r\nHost: httpbin.org\r\n\r\n') resp = s.recv(100) # 100 bytes enough for header. self.assertTrue('200 OK' in resp)
def test_constructor(self): ''' Instance attributes autosubstitution. ''' headers = { 'Host': 'httpbin.org', 'Connection': 'close', } hc = HttpCompiler(method='PATCH', headers=headers) qs = '/path/to/check' req = hc.build_raw(qs) p = HttpParser() p.execute(req, len(req)) result_hdrs = p.get_headers() self.assertTrue(p.get_method(), 'PATCH') self.assertTrue(all( [result_hdrs[h] == headers[h] for h in headers.keys()]))
def test_constructor(self): ''' Instance attributes autosubstitution. ''' headers = { 'Host': 'httpbin.org', 'Connection': 'close', } hc = HttpCompiler(method='PATCH', headers=headers) qs = '/path/to/check' req = hc.build_raw(qs) p = HttpParser() p.execute(req, len(req)) result_hdrs = p.get_headers() self.assertTrue(p.get_method(), 'PATCH') self.assertTrue( all([result_hdrs[h] == headers[h] for h in headers.keys()]))
def main(): # case #1: basic query_strings = [ '/api/v1/you', '/api/v1/can?run=false', '/api/v1/can?hide=true', ] headers = { 'Host': 'some.cool.org', 'Connection': 'close' } sys.stdout.write('Minimalistic HTTP GET request\n') sys.stdout.write('-' * 80 + '\n') hc = HttpCompiler(method='GET', headers=headers) #hc = HttpCompiler() for qs in query_strings: sys.stdout.write(hc.build_phantom(qs)) # case #2: advanced sys.stdout.write('Slightly more complicated request\n') sys.stdout.write('-' * 80 + '\n') body = {'USERNAME': '******', 'PASSWORD': '******'} headers = { 'Host': 'some.cool.org', 'Content-type': 'application/x-www-form-urlencoded', 'X-custom-header': 'some-value', 'Connection': 'close' } method = 'PATCH' body_encoded = urllib.urlencode(body) query_strings = [] for id in range(3): params = urllib.urlencode({'where': '^right there', 'id': id}) query_strings.append('%s?%s' % ('/some/path', params)) hc = HttpCompiler(method=method, headers=headers) for qs in query_strings: sys.stdout.write(hc.build_phantom(qs, body=body_encoded)) # case #3: advanced reqs = [ ( '/path/to/resource', { 'method': 'GET', 'headers': { 'Host': 'dom1.test.net', 'Connection': 'close' }, } ), ( '/just/one/more/resource', { 'method': 'POST', 'headers': { 'Host': 'dom2.test.net', 'Content-type': 'application/json' }, 'body': '{"a": "b"}', } ), ] hc = HttpCompiler() for r in reqs: sys.stdout.write(hc.build_phantom(r[0], **r[1]))