本文整理汇总了Python中pyramid.testing.DummyRequest.params方法的典型用法代码示例。如果您正苦于以下问题:Python DummyRequest.params方法的具体用法?Python DummyRequest.params怎么用?Python DummyRequest.params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid.testing.DummyRequest
的用法示例。
在下文中一共展示了DummyRequest.params方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_decimal_json
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_decimal_json(self):
from decimal import Decimal
from pyramid.testing import DummyRequest
from c2cgeoportal import DecimalJSON
renderer = DecimalJSON()(None)
request = DummyRequest()
request.user = None
system = {"request": request}
self.assertEquals(
renderer({"a": Decimal("3.3")}, system),
'{"a": 3.3}'
)
self.assertEquals(request.response.content_type, "application/json")
request.params = {"callback": "abc"}
self.assertEquals(
renderer({"a": Decimal("3.3")}, system),
'abc({"a": 3.3});'
)
self.assertEquals(request.response.content_type, "text/javascript")
renderer = DecimalJSON(jsonp_param_name="cb")(None)
request.params = {"cb": "def"}
self.assertEquals(
renderer({"a": Decimal("3.3")}, system),
'def({"a": 3.3});'
)
self.assertEquals(request.response.content_type, "text/javascript")
示例2: test_shortener
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_shortener(self):
from pyramid.testing import DummyRequest
from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest
from c2cgeoportal.views.shortener import Shortener
def route_url(name, *elements, **kw):
return "https://example.com/short/" + kw["ref"]
request = DummyRequest()
request.user = None
request.host = "example.com:443"
request.server_name = "example.com"
request.route_url = route_url
request.registry.settings["shortener"] = {
"base_url": "https://example.com/s/"
}
shortener = Shortener(request)
request.params = {}
request.matchdict = {
"ref": "AAAAAA"
}
self.assertRaises(HTTPNotFound, shortener.get)
request.params = {}
request.matchdict = {}
self.assertRaises(HTTPBadRequest, shortener.create)
request.params = {
"url": "https://other-site.com/hi"
}
self.assertRaises(HTTPBadRequest, shortener.create)
示例3: test_shortener_create_1
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_shortener_create_1(self):
from pyramid.testing import DummyRequest
from pyramid.httpexceptions import HTTPFound
from c2cgeoportal.views.shortener import Shortener
def route_url(name, *elements, **kw):
return "https://example.com/short/" + kw["ref"]
request = DummyRequest()
request.user = None
request.host = "example.com:443"
request.server_name = "example.com"
request.route_url = route_url
request.registry.settings["shortener"] = {
"base_url": "https://example.com/s/"
}
shortener = Shortener(request)
request.params = {
"url": "https://example.com/hi"
}
result = shortener.create()
index = result["short_url"].rfind("/")
self.assertEqual(
result["short_url"][:index],
"https://example.com/s"
)
request.params = {}
request.matchdict = {
"ref": result["short_url"][index + 1:]
}
result = shortener.get()
self.assertEqual(type(result), HTTPFound)
self.assertEqual(result.location, "https://example.com/hi")
示例4: test_login
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_login(self):
from pyramid.testing import DummyRequest
from c2cgeoportal.views.entry import Entry
request = DummyRequest()
request.user = None
entry = Entry(request)
request.path = '/for_test'
expected = {
'lang': 'fr',
'came_from': '/for_test',
}
self.assertEqual(entry.loginform403(), expected)
request.params = {
'came_from': '/for_a_second_test',
}
entry = Entry(request)
expected = {
'lang': 'fr',
'came_from': '/for_a_second_test',
}
self.assertEqual(entry.loginform(), expected)
entry = Entry(request)
request.params = {}
expected = {
'lang': 'fr',
'came_from': '/',
}
self.assertEqual(entry.loginform(), expected)
示例5: test_shortener
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_shortener(self):
from pyramid.testing import DummyRequest
from pyramid.httpexceptions import HTTPFound, HTTPNotFound, \
HTTPBadRequest
from c2cgeoportal.views.shortener import shortener
def route_url(name, *elements, **kw):
return 'https://example.com/s/' + kw['ref']
request = DummyRequest()
request.user = None
request.host = 'example.com:443'
request.server_name = 'example.com'
request.route_url = route_url
shortener = shortener(request)
request.params = {
'url': 'https://example.com/hi'
}
result = shortener.create()
index = result['short_url'].rfind('/')
self.assertEqual(
result['short_url'][:index],
'https://example.com/s'
)
request.params = {}
request.matchdict = {
'ref': result['short_url'][index + 1:]
}
result = shortener.get()
self.assertEqual(type(result), HTTPFound)
self.assertEqual(result.location, 'https://example.com/hi')
request.params = {}
request.matchdict = {
'ref': 'AAAAAA'
}
self.assertRaises(HTTPNotFound, shortener.get)
request.params = {}
request.matchdict = {}
self.assertRaises(HTTPBadRequest, shortener.create)
request.params = {
'url': 'https://other-site.com/hi'
}
self.assertRaises(HTTPBadRequest, shortener.create)
示例6: test_unicode_param
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_unicode_param(self):
""" Pull unicode params off of request object """
request = DummyRequest()
request.params = {'field': 'myfield'}
field = param(request, 'field')
self.assertEquals(field, 'myfield')
self.assertTrue(isinstance(field, six.text_type))
示例7: test_timedelta_param
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_timedelta_param(self):
""" Pull timedelta off of request object """
request = DummyRequest()
diff = 3600
request.params = {'field': diff}
field = param(request, 'field', type=datetime.timedelta)
self.assertEquals(field, datetime.timedelta(seconds=diff))
示例8: test_oauth2_step2
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_oauth2_step2(self):
token_uri = 'http://example.com/oauth2/token'
client_id = '1234'
client_secret = 'secret'
redirect_url = 'http://localhost/oauth2/callback'
scope = 'scope1 scope2'
request = DummyRequest()
response = oauth2_step2(request, token_uri, client_id, client_secret,
redirect_url, scope)
self.assertEqual(response.status, '400 Bad Request')
self.assertEqual(response.message, 'Missing required code')
request.params = {'code': 'abcdef'}
response = oauth2_step2(request, token_uri, client_id, client_secret,
redirect_url, scope)
self.assertEqual(response.status, '400 Bad Request')
self.assertEqual(response.message, 'Missing required state')
request.params['state'] = 'random-string'
response = oauth2_step2(request, token_uri, client_id, client_secret,
redirect_url, scope)
self.assertEqual(response.status, '401 Unauthorized')
self.assertEqual(response.message, 'Missing internal state. You may be a victim of CSRF')
request.session = {'state': 'other-string'}
response = oauth2_step2(request, token_uri, client_id, client_secret,
redirect_url, scope)
self.assertEqual(response.status, '401 Unauthorized')
self.assertEqual(response.message, 'State parameter does not match internal state. You may be a victim of CSRF')
with patch('requests.post') as fake:
fake.return_value.status_code = 401
fake.return_value.text = 'Unauthorized request'
request.session['state'] = 'random-string'
response = oauth2_step2(request, token_uri,
client_id, client_secret,
redirect_url, scope)
self.assertEqual(response.status, '401 Unauthorized')
self.assertEqual(response.message, 'Unauthorized request')
with patch('requests.post') as fake:
fake.return_value.status_code = 200
fake.return_value.json = lambda: {
'access_token': 'qwerty'
}
request.session['state'] = 'random-string'
response = oauth2_step2(request, token_uri,
client_id, client_secret,
redirect_url, scope)
self.assertEqual(response, 'qwerty')
with patch('requests.post') as fake:
fake.return_value.status_code = 200
fake.return_value.json = lambda: None
fake.return_value.text = 'access_token=qwerty'
request.session['state'] = 'random-string'
response = oauth2_step2(request, token_uri,
client_id, client_secret,
redirect_url, scope)
self.assertEqual(response, 'qwerty')
示例9: test_oauth2_step1
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_oauth2_step1(self):
with patch('uuid.uuid4') as fake:
fake.return_value = 'random-string'
request = DummyRequest()
request.params = {'next_url': 'http://localhost/'}
request.session = {}
response = oauth2_step1(
request=request,
auth_uri='http://example.com/oauth2/auth',
client_id='1234',
redirect_url='http://localhost/oauth2/callback',
scope='scope1 scope2'
)
self.assertEqual(response.status, '302 Found')
url = urlparse.urlparse(response.location)
self.assertEqual(url.netloc, 'example.com')
self.assertEqual(url.path, '/oauth2/auth')
query = urlparse.parse_qs(url.query)
self.assertEqual(query, {
'scope': ['scope1 scope2'],
'state': ['random-string'],
'redirect_uri': ['http://localhost/oauth2/callback'],
'response_type': ['code'],
'client_id': ['1234'],
})
self.assertEqual(request.session['next_url'], 'http://localhost/')
示例10: test_validate_failure
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_validate_failure(self):
""" if param() fails validation check, raise exception """
request = DummyRequest()
request.params = {'field': 'myfield'}
validate = lambda x: x.startswith('foo')
with self.assertRaises(HTTPBadRequest):
param(request, 'field', validate=validate)
示例11: test_bytes_param
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_bytes_param(self):
""" Pull binary string param off of request object """
request = DummyRequest()
request.params = {'field': 'myfield'}
field = param(request, 'field', type=bytes)
self.assertEquals(field, b'myfield')
self.assertTrue(isinstance(field, six.binary_type))
示例12: test_validate
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_validate(self):
""" param() can run a validation check on parameter values """
request = DummyRequest()
request.params = {'field': 'foobar'}
validate = lambda x: x.startswith('foo')
field = param(request, 'field', validate=validate)
self.assertEquals(field, 'foobar')
示例13: test_datetime_param
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_datetime_param(self):
""" Pull datetime off of request object """
request = DummyRequest()
now = int(time.time())
request.params = {'field': now}
field = param(request, 'field', type=datetime)
self.assertEquals(calendar.timegm(field.utctimetuple()), now)
示例14: test_shortener_baseurl
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_shortener_baseurl(self):
from pyramid.testing import DummyRequest
from c2cgeoportal.views.shortener import Shortener
def route_url(name, *elements, **kw):
return "https://example.com/short/" + kw["ref"]
request = DummyRequest()
request.user = None
request.host = "example.com:443"
request.server_name = "example.com"
request.route_url = route_url
request.registry.settings["shortener"] = {
"base_url": "http://my_host/my_short/"
}
shortener = Shortener(request)
request.params = {
"url": "https://example.com/hi"
}
result = shortener.create()
index = result["short_url"].rfind("/")
self.assertEqual(
result["short_url"][:index],
"http://my_host/my_short"
)
示例15: test_unicode_param_explicit
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import params [as 别名]
def test_unicode_param_explicit(self):
""" Specifying type=unicode checks arg type before returning it """
request = DummyRequest()
request.params = {'field': 'myfield'}
field = param(request, 'field', type=six.text_type)
self.assertEquals(field, 'myfield')
self.assertTrue(isinstance(field, six.text_type))