def test_session(self): headers = testlib.randdict() cookies = testlib.randdict() s = urlfetch.Session(headers=headers, cookies=cookies) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) randstr = testlib.randstr header = (randstr(), randstr()) headers[header[0]] = header[1] s.putheader(*header) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) cookie = (randstr(), randstr()) cookies[cookie[0]] = cookie[1] s.putcookie(*cookie) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) header = headers.popitem() s.popheader(header[0]) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) cookie = cookies.popitem() s.popcookie(cookie[0]) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) s = urlfetch.Session() cookie = (randstr(), randstr()) r = s.get(testlib.url('setcookie/%s/%s' % cookie)) self.assertEqual(s.cookies[cookie[0]], cookie[1]) self.assertTrue(('%s=%s' % cookie) in s.cookiestring) cookie2 = (randstr(), randstr()) r = s.get(testlib.url('setcookie/%s/%s' % cookie2)) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookie)) self.assertEqual(s.cookies[cookie[0]], cookie[1]) self.assertTrue(('%s=%s' % cookie) in s.cookiestring) self.assertEqual(s.cookies[cookie2[0]], cookie2[1]) self.assertTrue(('%s=%s' % cookie2) in s.cookiestring)
def test_cookiestring_setter(self): headers = testlib.randdict() cookies = testlib.randdict() s1 = urlfetch.Session(headers=headers, cookies=cookies) headers = testlib.randdict() cookies = testlib.randdict() s2 = urlfetch.Session(headers=headers, cookies=cookies) s2.cookiestring = s1.cookiestring self.assertEqual(s1.cookies, s2.cookies) s1.cookiestring = '' self.assertEqual(s1.cookies, {})
def test_post_query_string(self): d = testlib.randdict() data = urlfetch.urlencode(d) qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) r = urlfetch.post(testlib.test_server_host + '?'+query_string, data=data) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertEqual(o['method'], 'POST') self.assertEqual(o['post'], d) self.assertEqual(o['get'], qs) self.assertEqual(o['query_string'], query_string)
def test_session_fragment_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) cookiepair = (testlib.randstr(), testlib.randstr()) headerpair = (testlib.randstr(), testlib.randstr()) s = urlfetch.Session(headers=dict((headerpair, )), cookies=dict((cookiepair, ))) r = s.get(testlib.url()) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(r.reqheaders[headerpair[0]], headerpair[1]) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookiepair)) r = s.get(testlib.url('?' + query_string + '#urlfetch')) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(r.reqheaders[headerpair[0]], headerpair[1]) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookiepair)) self.assertEqual(o['query_string'], query_string) self.assertEqual(o['get'], qs)
def test_session_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) cookiepair = (testlib.randstr(), testlib.randstr()) headerpair = (testlib.randstr(), testlib.randstr()) s = urlfetch.Session(headers=dict((headerpair,)), cookies=dict((cookiepair,))) r = s.get(testlib.test_server_host) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(r.reqheaders[headerpair[0]], headerpair[1]) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookiepair)) r = s.get(testlib.url('?' + query_string)) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(r.reqheaders[headerpair[0]], headerpair[1]) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookiepair)) self.assertEqual(o['query_string'], query_string) self.assertEqual(o['get'], qs)
def test_post_query_string(self): d = testlib.randdict() data = urlfetch.urlencode(d) qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) r = urlfetch.post(testlib.url('?' + query_string), data=data) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'POST') self.assertEqual(o['post'], d) self.assertEqual(o['get'], qs) self.assertEqual(o['query_string'], query_string)
def test_post(self): d = testlib.randdict() data = urlfetch.urlencode(d) r = urlfetch.post(testlib.test_server_host, data=data) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertEqual(o['method'], 'POST') self.assertEqual(o['post'], d)
def test_basic_auth_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) r = urlfetch.get(testlib.test_server_host + "basic_auth?" + query_string, auth=("urlfetch", "fetchurl")) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertEqual(o["method"], "GET") self.assertEqual(o["query_string"], query_string) self.assertEqual(o["get"], qs)
def test_fragment_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) r = urlfetch.get(testlib.test_server_host + "?" + query_string + "#urlfetch") o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertEqual(o["method"], "GET") self.assertEqual(o["query_string"], query_string) self.assertEqual(o["get"], qs)
def test_fetch(self): d = testlib.randdict() data = urlfetch.urlencode(d) r = urlfetch.fetch(testlib.test_server_host, data=data) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'POST') self.assertEqual(o['post'], d)
def test_post(self): d = testlib.randdict() data = urlfetch.urlencode(d) r = urlfetch.post(testlib.test_server_host, data=data) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'POST') self.assertEqual(o['post'], d)
def test_session(self): headers = testlib.randdict() cookies = testlib.randdict() s = urlfetch.Session(headers=headers, cookies=cookies) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) randstr = testlib.randstr header = (randstr(), randstr()) headers[header[0]] = header[1] s.putheader(*header) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) cookie = (randstr(), randstr()) cookies[cookie[0]] = cookie[1] s.putcookie(*cookie) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) header = headers.popitem() s.popheader(header[0]) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) cookie = cookies.popitem() s.popcookie(cookie[0]) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) s = urlfetch.Session() cookie = (randstr(), randstr()) r = s.get(testlib.url('setcookie/%s/%s' % cookie)) self.assertEqual(s.cookies[cookie[0]], cookie[1]) self.assertTrue(('%s=%s' % cookie) in s.cookiestring) cookie2 = (randstr(), randstr()) r = s.get(testlib.url('setcookie/%s/%s' % cookie2)) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookie)) self.assertEqual(s.cookies[cookie[0]], cookie[1]) self.assertTrue(('%s=%s' % cookie) in s.cookiestring) self.assertEqual(s.cookies[cookie2[0]], cookie2[1]) self.assertTrue(('%s=%s' % cookie2) in s.cookiestring)
def test_fragment_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) r = urlfetch.get(testlib.test_server_host + '?'+ query_string + '#urlfetch') o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(o['query_string'], query_string) self.assertEqual(o['get'], qs)
def test_basic_auth_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) r = urlfetch.get(testlib.test_server_host + 'basic_auth?'+ query_string, auth=('urlfetch', 'fetchurl')) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(o['query_string'], query_string) self.assertEqual(o['get'], qs)
def test_file_upload_multipart(self): content = open(path('test.file'), 'rb').read() files = {} files['test.file1'] = (b'test.file1', b'test.file', content) #files[b'test.file2'] = (b'test.file2', b'', content) files['test.file3'] = (b'test.file3', b'wangxiaobo', content) files['test.file4'] = (b'test.file4', b'wangtwo', content) data = testlib.randdict() params = testlib.randdict(5) r = urlfetch.post( testlib.test_server_host, data=data, params=params, files={ 'test.file1': open(path('test.file')), #'test.file2' : content, 'test.file3': ('wangxiaobo', open(path('test.file'))), 'test.file4': ('wangtwo', content) }, ) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'POST') self.assertEqual(o['post'], data) self.assertEqual(sorted(o['files'].keys()), sorted(files.keys())) for i in files: self.assertEqual(o['files'][i][0].encode('utf-8'), files[i][0]) self.assertEqual(o['files'][i][1].encode('utf-8'), files[i][1]) self.assertEqual(o['files'][i][2].encode('utf-8'), md5sum(files[i][2])) for k, v in params.items(): self.assertTrue(('%s=%s' % (k, v)) in r.url)
def test_fragment_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) r = urlfetch.get(testlib.url('?' + query_string + '#urlfetch')) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(r.links, []) self.assertEqual(o['method'], 'GET') self.assertEqual(o['query_string'], query_string) self.assertEqual(o['get'], qs)
def test_file_upload_multipart(self): content = open(path('test.file'), 'rb').read() files = {} files['test.file1'] = (b'test.file1', b'test.file', content) #files[b'test.file2'] = (b'test.file2', b'', content) files['test.file3'] = (b'test.file3', b'wangxiaobo', content) files['test.file4'] = (b'test.file4', b'wangtwo', content) data = testlib.randdict() params = testlib.randdict(5) r = urlfetch.post( testlib.test_server_host, data = data, params = params, files = { 'test.file1' : open(path('test.file')), #'test.file2' : content, 'test.file3' : ('wangxiaobo', open(path('test.file'))), 'test.file4' : ('wangtwo', content) }, ) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'POST') self.assertEqual(o['post'], data) self.assertEqual(sorted(o['files'].keys()), sorted(files.keys())) for i in files: self.assertEqual(o['files'][i][0].encode('utf-8'), files[i][0]) self.assertEqual(o['files'][i][1].encode('utf-8'), files[i][1]) self.assertEqual(o['files'][i][2].encode('utf-8'), md5sum(files[i][2])) for k, v in params.items(): self.assertTrue(('%s=%s' % (k, v)) in r.url)
def test_session_fragment_basic_auth_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) cookiepair = (testlib.randstr(), testlib.randstr()) headerpair = (testlib.randstr(), testlib.randstr()) s = urlfetch.Session(headers=dict((headerpair,)), cookies=dict((cookiepair,)), auth=('urlfetch', 'fetchurl')) r = s.get(testlib.test_server_host + 'basic_auth?' + query_string + '#urlfetch') o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertEqual(o['method'], 'GET') self.assertEqual(r.reqheaders[headerpair[0].title()], headerpair[1]) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookiepair)) self.assertEqual(o['query_string'], query_string) self.assertEqual(o['get'], qs)
def test_session(self): headers = testlib.randdict() cookies = testlib.randdict() s = urlfetch.Session(headers=headers, cookies=cookies) headers = dict((k.title(), v) for k, v in headers.items()) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) randstr = testlib.randstr header = (randstr(), randstr()) headers[header[0].title()] = header[1] s.putheader(*header) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) cookie = (randstr(), randstr()) cookies[cookie[0]] = cookie[1] s.putcookie(*cookie) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) header = headers.popitem() s.popheader(header[0]) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) cookie = cookies.popitem() s.popcookie(cookie[0]) self.assertEqual(s.snapshot(), {'headers': headers, 'cookies': cookies}) new_s = urlfetch.Session() new_s.loads(s.dumps()) self.assertEqual(s.snapshot(), new_s.snapshot()) new_s = urlfetch.Session() new_s.loads(s.dumps(cls='json'), cls='json') self.assertEqual(s.snapshot(), new_s.snapshot()) new_s = urlfetch.Session() new_s.loads(s.dumps(cls='pickle'), cls='pickle') self.assertEqual(s.snapshot(), new_s.snapshot()) new_s = urlfetch.Session() with open('test.dump', 'wb') as f: s.dump(f) with open('test.dump', 'rb') as f: new_s.load(f) self.assertEqual(s.snapshot(), new_s.snapshot()) new_s = urlfetch.Session() with open('test.dump', 'wb') as f: s.dump(f, cls='json') with open('test.dump', 'rb') as f: new_s.load(f, cls='json') self.assertEqual(s.snapshot(), new_s.snapshot()) new_s = urlfetch.Session() with open('test.dump', 'wb') as f: s.dump(f, cls='pickle') with open('test.dump', 'rb') as f: new_s.load(f, cls='pickle') self.assertEqual(s.snapshot(), new_s.snapshot()) os.unlink('test.dump') s = urlfetch.Session() cookie = (randstr(), randstr()) r = s.get(testlib.test_server_host + 'setcookie/%s/%s' % cookie) self.assertEqual(s.cookies[cookie[0]], cookie[1]) cookie2 = (randstr(), randstr()) r = s.get(testlib.test_server_host + 'setcookie/%s/%s' % cookie2) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookie)) self.assertEqual(s.cookies[cookie[0]], cookie[1]) self.assertEqual(s.cookies[cookie2[0]], cookie2[1])