如何发送用户名:密码unittest的app.get()请求? [英] How to send username:password to unittest's app.get() request?
问题描述
self.app = application.app.test_client()这是我在Flask-RESTful中进行单元测试的一部分。
rv = self.app.get('api / v1.0 / {0}'.format(ios_sync_timestamp))
eq_(rv.status_code,200)
$ c $在命令行中,我可以使用curl将用户名:密码发送给服务:
curl -d用户名:密码http:// localhost:5000 / api / v1.0 / 1234567
如何在我的单元测试的get()中获得相同的结果?
由于我的get / put / post需要身份验证否则测试将会失败。 RFC 1945,超文本传输协议--HTTP / 1.0
11.1基本认证计划
...
要接收授权,客户端发送t他的用户ID和密码
由一个单独的冒号(:)字符分隔,在credentials.string的base64 [5]
编码字符串中。
如果用户代理希望发送用户IDAladdin和密码
打开芝麻,它会使用以下标题字段:
授权:基本QWxhZGRpbjpvcGVuIHNlc2FtZQ ==
$因此,如果你真的使用http基本身份验证,你可以像下面的解决方案,虽然你的卷曲
用法建议一些其他的认证方案。 from base64 import b64encode
headers = {$格式(用户名,密码))
}
rv = self.app.get({0}:{1} ('api / v1.0 / {0}'.format(ios_sync_timestamp),headers = headers)
This is part of my unit test in Flask-RESTful.
self.app = application.app.test_client()
rv = self.app.get('api/v1.0/{0}'.format(ios_sync_timestamp))
eq_(rv.status_code,200)
Within the command line I could use curl to send the username:password to the service:
curl -d username:password http://localhost:5000/api/v1.0/1234567
How do I achieve the same within my unit test's get() ?
Since my get/put/post require authentication otherwise the test would fail.
解决方案 From RFC 1945, Hypertext Transfer Protocol -- HTTP/1.0
11.1 Basic Authentication Scheme
...
To receive authorization, the client sends the user-ID and password,
separated by a single colon (":") character, within a base64 [5]
encoded string in the credentials.string.
...
If the user agent wishes to send the user-ID "Aladdin" and password
open sesame", it would use the following header field:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
So if you really use http basic authentication you can solution like below, although your curl
usage suggests some other authentication scheme.
from base64 import b64encode
headers = {
'Authorization': 'Basic ' + b64encode("{0}:{1}".format(username, password))
}
rv = self.app.get('api/v1.0/{0}'.format(ios_sync_timestamp), headers=headers)
这篇关于如何发送用户名:密码unittest的app.get()请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!