本文整理匯總了Python中webtest.TestRequest類的典型用法代碼示例。如果您正苦於以下問題:Python TestRequest類的具體用法?Python TestRequest怎麽用?Python TestRequest使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了TestRequest類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_logout
def test_logout(self):
url = '/logout/'
request_with_all = TestRequest.blank(
url, method='GET', referrer='http://localhost/content/list/',
headers=self.mk_session()[1])
request_without_ref = TestRequest.blank(
url, method='GET', headers=self.mk_session()[1])
request_with_invalid_ref = TestRequest.blank(
url, method='GET', referrer='http://example.com/page/',
headers=self.mk_session()[1])
for request in (request_with_all,
request_without_ref,
request_with_invalid_ref):
resp = self.app.request(request)
self.assertEqual(resp.status_int, 302)
if request == request_with_all:
self.assertEqual(resp.location, request.referrer)
else:
self.assertEqual(resp.location, 'http://localhost/')
# check that the session is no longer authenticated
session = Session(
request,
id=request.cookies['beaker.session.id'],
use_cookies=False)
self.assertNotIn('auth.userid', session)
self.app.reset()
示例2: test_redirect_to_login
def test_redirect_to_login(self):
url = '/login/hub/?%s' % urlencode({'_LOCALE_': 'urd_IN'})
request_with_all = TestRequest.blank(
url, method='GET', referrer='http://localhost/content/list/')
request_without_ref = TestRequest.blank(url, method='GET')
request_with_invalid_ref = TestRequest.blank(
url, method='GET', referrer='http://example.com/page/')
for request in (request_with_all,
request_without_ref,
request_with_invalid_ref):
resp = self.app.request(request)
self.assertEqual(resp.status_int, 302)
parts = urlparse(resp.location)
params = parse_qs(parts.query)
self.assertIn('service', params)
self.assertIn('_LOCALE_', params)
self.assertEqual(params['_LOCALE_'][0], request.params['_LOCALE_'])
callback_url = params['service'][0]
parts = urlparse(callback_url)
self.assertEqual(parts[:3], ('http', 'localhost', '/login/'))
if request is request_with_all:
params = parse_qs(parts.query)
self.assertIn('url', params)
self.assertEqual(
params['url'][0], request.referrer)
else:
self.assertFalse(parts.query)
示例3: test_12_github_webhook_negative
def test_12_github_webhook_negative(self):
request = TestRequest.blank('/jobs/%s/github-webhook' % self.test_state['job_id'])
request.method = 'POST'
request.body = 'unexpected content'
_response = self.app.do_request(request, 400, True)
request = TestRequest.blank('/jobs/%s/github-webhook' % self.test_state['job_id'])
request.method = 'POST'
request.body = json.dumps({'ref': 'refs/heads/wrongbranch'})
_response = self.app.do_request(request, 400, True)
示例4: test_login
def test_login(self):
ticket = 'iamaticket'
redirect_url = 'http://localhost/content/list/'
user_data = {
'uuid': 'imauuid',
'username': 'foo',
'app_data': {'display_name': 'foobar'}
}
responses.add(
responses.GET, re.compile(r'.*/sso/validate.*'),
body=json.dumps(user_data),
status=200,
content_type='application/json')
request_with_url = TestRequest.blank(
'/login/?%s' % urlencode({'ticket': ticket, 'url': redirect_url}))
request_without_url = TestRequest.blank(
'/login/?%s' % urlencode({'ticket': ticket}))
for request in (request_with_url, request_without_url):
resp = self.app.request(request)
self.assertEqual(resp.status_int, 302)
if request == request_with_url:
self.assertEqual(resp.location, redirect_url)
else:
self.assertEqual(resp.location, 'http://localhost/')
# check that session contains user data
self.assertIn('beaker.session.id', self.app.cookies)
session = Session(
request,
id=self.app.cookies['beaker.session.id'],
use_cookies=False)
self.assertEqual(session['auth.userid'], user_data['uuid'])
self.assertEqual(session[USER_DATA_SESSION_KEY], user_data)
self.app.reset()
responses.reset()
responses.add(
responses.GET, re.compile(r'.*/sso/validate.*'),
body=json.dumps('no\n'), status=200,
content_type='application/json')
resp = self.app.request(request_with_url)
self.assertEqual(resp.status_int, 302)
self.assertEqual(resp.location, redirect_url)
self.assertNotIn('beaker.session.id', self.app.cookies)
示例5: wapp_post
def wapp_post(wapp, resourcetype, data, test_session_id):
"""
We build the request as a blank and copy it to allow restrest to work
"""
URL = get_url(resourcetype, id_=None, method="POST")
headerd = get_cookie_hdr(test_session_id)
###
data_as_json = json.dumps(data)
err.write("\n"+"URL: %s" % URL)
err.write("\n"+"body: %s" % data_as_json[:100])
err.write("\n"+"hdrs: %s" % headerd)
req = TestRequest.blank(URL, method="POST",
body=data_as_json,
headers=headerd)
req.content_type = "application/json; charset=utf-8"
for k, v in wapp.extra_environ.items():
req.environ.setdefault(k, v)
reqcp = req.copy()
resp = wapp.do_request(req, status="*", expect_errors=True)
capture_conversation(reqcp, resp)
###
return resp
示例6: set_user_cookie
def set_user_cookie(self, user_id):
request = TestRequest.blank('', {})
request.registry = self.testapp.app.registry
remember_headers = remember(request, user_id)
cookie_value = remember_headers[0][1].split('"')[1]
self.testapp.cookies['auth_tkt'] = cookie_value
return request
示例7: test_04_update_build_state
def test_04_update_build_state(self):
request = TestRequest.blank('/jobs/%s/builds/%s/state' % (self.test_state['job_id'], self.test_state['build_number']), content_type='application/json')
request.method = 'PUT'
request.body = json.dumps({"status": "pending", "config": {"name": "test"}})
response = self.app.do_request(request, 200, False)
result = json.loads(response.body)
assert result.has_key('job_id'), "ID entry went missing"
assert result.has_key('build_number'), "build_number went missing"
assert result.has_key('state'), "state went missing"
assert result['state'].has_key('status'), "status went missing"
assert result['state']['status'] == 'pending', "wrong status"
assert result['state'].has_key('config'), "config went missing"
assert result['state']['config'].has_key('name'), "config/name went missing"
assert result['state']['config']['name'] == 'test', "wrong name"
response = self.app.request('/jobs/%s/builds/%s/state' % (self.test_state['job_id'], self.test_state['build_number']))
result = json.loads(response.body)
assert result.has_key('job_id'), "ID entry went missing"
assert result.has_key('build_number'), "build_number went missing"
assert result.has_key('state'), "state went missing"
assert result['state'].has_key('status'), "status went missing"
assert result['state']['status'] == 'pending', "wrong status"
assert result['state'].has_key('config'), "config went missing"
assert result['state']['config'].has_key('name'), "config/name went missing"
assert result['state']['config']['name'] == 'test', "wrong name"
示例8: _gen_request
def _gen_request(self, method, url, params='', headers=None, extra_environ=None,
status=None, upload_files=None, expect_errors=False,
content_type=None, body=None):
"""
Do a generic request.
"""
environ = self._make_environ(extra_environ)
# @@: Should this be all non-strings?
if isinstance(params, (list, tuple, dict)):
params = urllib.urlencode(params)
if hasattr(params, 'items'):
params = urllib.urlencode(params.items())
if upload_files:
params = cgi.parse_qsl(params, keep_blank_values=True)
content_type, params = self.encode_multipart(
params, upload_files)
environ['CONTENT_TYPE'] = content_type
elif params:
environ.setdefault('CONTENT_TYPE', 'application/x-www-form-urlencoded')
if '?' in url:
url, environ['QUERY_STRING'] = url.split('?', 1)
else:
environ['QUERY_STRING'] = ''
if content_type is not None:
environ['CONTENT_TYPE'] = content_type
environ['CONTENT_LENGTH'] = str(len(params))
environ['REQUEST_METHOD'] = method
environ['wsgi.input'] = StringIO(params)
req = TestRequest.blank(url, environ)
if headers:
req.headers.update(headers)
if body:
req.body = body
return self.do_request(req, status=status,
expect_errors=expect_errors)
示例9: test_02_trigger_build
def test_02_trigger_build(self):
request = TestRequest.blank('/jobs/%s/builds' % self.test_state['job_id'])
request.method = 'POST'
response = self.app.do_request(request, 201, False)
result = json.loads(response.body)
assert result.has_key('job_id'), "ID entry went missing"
assert result.has_key('build_number'), "build_number went missing"
self.test_state['build_number'] = result['build_number']
示例10: test_08_update_workspace
def test_08_update_workspace(self):
request = TestRequest.blank('/jobs/%s/builds/%s/workspace' % (self.test_state['job_id'], self.test_state['build_number']), content_type='application/octet-stream')
request.method = 'PUT'
request.body = 'updated_test_content'
_ = self.app.do_request(request, 204, False)
response = self.app.request('/jobs/%s/builds/%s/workspace' % (self.test_state['job_id'], self.test_state['build_number']))
assert response.body == 'updated_test_content', "Wrong data"
示例11: test_signature_mangled
def test_signature_mangled(self):
app.get('/')
orig_cookie = app.cookies['manhattan']
# truncate the last 4 chars, which will blow the sig
bad_cookie = orig_cookie[:-4]
bad_request = TestRequest.blank('/', cookies={'manhattan': bad_cookie})
with self.assertRaises(BadSignature):
app.request(bad_request)
示例12: http
def http(string, handle_errors=True):
app = zope.testbrowser.wsgi.Layer.get_app()
if app is None:
raise NotInBrowserLayer(NotInBrowserLayer.__doc__)
request = TestRequest.from_file(StringIO(string))
request.environ['wsgi.handleErrors'] = handle_errors
response = request.get_response(app)
return FakeResponse(response)
示例13: test_signature_mangled
def test_signature_mangled(self):
app.get('/')
orig_cookie = app.cookies['manhattan']
# truncate the last 4 chars, which will blow the sig
bad_cookie = orig_cookie[:-4]
bad_request = TestRequest.blank('/', cookies={'manhattan': bad_cookie})
app.request(bad_request)
new_cookie = app.cookies['manhattan']
self.assertNotEqual(bad_cookie, new_cookie)
示例14: test_05_update_with_wrong_assignee
def test_05_update_with_wrong_assignee(self):
task_id = self.test_state.get('id')
if task_id is None:
raise SkipTest("Skipping test for single task update, no recorded state")
new_task_data = json.dumps({'command': 'something_else', 'assignee': 'my-id-not-same'})
request = TestRequest.blank('/tasks/%s' % task_id, content_type='application/json')
request.method = 'PUT'
request.body = new_task_data
response = self.app.do_request(request, 409, False)
示例15: test_01_create_artifact
def test_01_create_artifact(self):
request = TestRequest.blank('/jobs/%s/builds/%s/artifacts' % (self.test_state['job_id'], self.test_state['build_number']), content_type='application/octet-stream')
request.method = 'POST'
request.body = 'test_content'
response = self.app.do_request(request, 201, False)
result = json.loads(response.body)
assert result.has_key('job_id'), "ID entry went missing"
assert result.has_key('build_number'), "build_number went missing"
assert result.has_key('artifact_id'), "Artifact ID went missing"
self.test_state['artifact_id'] = str(result['artifact_id'])