Unit testing 单元测试http会话cookies python3
我正在单元测试一个用Python 3.2编写的WSGI应用程序 我一直在寻找将会话cookie从一个请求传递到下一个请求的方法 self.cli是http.client.HTTPConnection cookie是http.cookies.SimpleCookieUnit testing 单元测试http会话cookies python3,unit-testing,session,cookies,python-3.x,Unit Testing,Session,Cookies,Python 3.x,我正在单元测试一个用Python 3.2编写的WSGI应用程序 我一直在寻找将会话cookie从一个请求传递到下一个请求的方法 self.cli是http.client.HTTPConnection cookie是http.cookies.SimpleCookie def testLogonCookie(self): self.cli.connect() self.cli.request("POST", '/login', b'user=GUI&pass=Junkie')
def testLogonCookie(self):
self.cli.connect()
self.cli.request("POST", '/login', b'user=GUI&pass=Junkie')
res = self.cli.getresponse()
heads = res.getheader('Set-Cookie')
print(heads)
txt = str(res.readall())
self.assertGreater(txt.find('Lorum ipsum'), -1, 'testLogin')
head = {"HTTP_COOKIE": heads}
print(head)
####Exception here
self.cli.request('GET', '/loggedon', head)
res = self.cli.getresponse()
txt = str(res.readall())
print(txt)
self.assertGreater(txt.find('user=GUI_Junkie'), -1, 'testLogonCookie')
第一个打印记录道为SID=0422f293-58e4-45a9-ab07-a4881c7b98d0;expires=2014-03-29 17:39:55.868140
第二个是{'HTTP_COOKIE':'SID=0422f293-58e4-45a9-ab07-a4881c7b98d0;expires=2014-03-2917:39:55.868140'}
我得到的例外是TypeError:'str'不支持缓冲区接口
我怀疑我必须解析
getheader
并将其放入dict中,但我想知道是否有更简单的方法来实现它。最后,我只想将cookie从一个请求推送到另一个请求。请求调用是错误的。第三个参数是请求的主体<代码>请求('GET','/loggedon',headers=head)
def testLogonCookie(self):
self.cli.connect()
self.cli.request("POST", '/login', b'user=GUI&pass=Junkie')
res = self.cli.getresponse()
heads = res.getheader('Set-Cookie')
txt = str(res.readall())
self.assertGreater(txt.find('Lorum ipsum'), -1, 'testLogin')
head = {'Cookie': heads}
self.cli.request('GET', '/loggedon', headers=head)
res = self.cli.getresponse()
txt = str(res.readall())
self.assertGreater(txt.find('user=GUI_Junkie'), -1, 'testLogonCookie')