本文整理汇总了Python中pyramid.testing.DummyRequest.route_url方法的典型用法代码示例。如果您正苦于以下问题:Python DummyRequest.route_url方法的具体用法?Python DummyRequest.route_url怎么用?Python DummyRequest.route_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid.testing.DummyRequest
的用法示例。
在下文中一共展示了DummyRequest.route_url方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_shortener
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [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)
示例2: test_shortener_create_1
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [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")
示例3: test_shortener_baseurl
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [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"
)
示例4: _create_request
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def _create_request():
mock_dumps = Mock(return_value='TOKEN')
request = DummyRequest()
request.domain = 'www.howtoreachtheark.now'
request.registry.notification_serializer = Mock(dumps=mock_dumps)
request.route_url = Mock()
request.route_url.return_value = 'UNSUBSCRIBE_URL'
return request
示例5: test_admins_remove_returns_redirect_when_too_few_admins
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_admins_remove_returns_redirect_when_too_few_admins(User):
User.admins.return_value = [Mock(username="fred")]
request = DummyRequest(params={"remove": "fred"})
request.route_url = Mock()
response = admin.admins_remove(request)
assert isinstance(response, httpexceptions.HTTPSeeOther)
示例6: test_staff_remove_returns_redirect_on_success
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_staff_remove_returns_redirect_on_success(User):
User.admins.return_value = [Mock(username="fred"),
Mock(username="bob"),
Mock(username="frank")]
request = DummyRequest(params={"remove": "fred"})
request.route_url = Mock()
response = admin.admins_remove(request)
assert isinstance(response, httpexceptions.HTTPSeeOther)
示例7: test_staff_remove_calls_get_by_username
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_staff_remove_calls_get_by_username(User):
User.staff_members.return_value = [Mock(username="fred"),
Mock(username="bob"),
Mock(username="frank")]
request = DummyRequest(params={"remove": "fred"})
request.route_url = Mock()
admin.staff_remove(request)
User.get_by_username.assert_called_once_with("fred")
示例8: test_admins_remove_does_not_delete_last_admin
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_admins_remove_does_not_delete_last_admin(User):
User.admins.return_value = [Mock(username="fred")]
request = DummyRequest(params={"remove": "fred"})
request.route_url = Mock()
user = Mock(admin=True)
User.get_by_username.return_value = user
admin.admins_remove(request)
assert user.admin is True
示例9: test_web_client_functionalities
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_web_client_functionalities(self):
from pyramid.testing import DummyRequest
from c2cgeoportal.models import DBSession, User
from c2cgeoportal.tests.functional import mapserv_url
from c2cgeoportal.views.entry import Entry
request = DummyRequest()
request.static_url = lambda url: 'http://example.com/dummy/static/url'
request.route_url = lambda url: mapserv_url
request.user = None
request1 = DummyRequest()
request1.static_url = lambda url: 'http://example.com/dummy/static/url'
request1.route_url = lambda url: mapserv_url
request1.user = DBSession.query(User).filter(User.username == '__test_user1').one()
request2 = DummyRequest()
request2.static_url = lambda url: 'http://example.com/dummy/static/url'
request2.route_url = lambda url: mapserv_url
request2.user = DBSession.query(User).filter(User.username == '__test_user2').one()
settings = {
'functionalities': {
'anonymous': {
"__test_s": "anonymous",
"__test_a": ["a1", "a2"]
},
'registered': {
"__test_s": "registered",
"__test_a": ["r1", "r2"]
},
'available_in_templates': ['__test_s', '__test_a'],
},
'mapserv_url': mapserv_url,
}
request.registry.settings = settings
request1.registry.settings = settings
request2.registry.settings = settings
annon = Entry(request)._getVars()
u1 = Entry(request1)._getVars()
u2 = Entry(request2)._getVars()
self.assertEquals(annon['functionality'], {"__test_s": ["anonymous"], "__test_a": ["a1", "a2"]});
self.assertEquals(u1['functionality'], {"__test_s": ["registered"], "__test_a": ["r1", "r2"]});
self.assertEquals(u2['functionality'], {"__test_s": ["db"], "__test_a": ["db1", "db2"]});
示例10: test_shortener
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [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 = {
'url': 'https://example.com/short/truite'
}
result = shortener.create()
self.assertEqual(result['short_url'], 'https://example.com/s/truite')
request.params = {}
request.matchdict = {}
self.assertRaises(HTTPBadRequest, shortener.create)
request.params = {
'url': 'https://other-site.com/hi'
}
self.assertRaises(HTTPBadRequest, shortener.create)
示例11: test_shortener
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [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 = {
"url": "https://example.com/short/truite"
}
result = shortener.create()
self.assertEqual(result["short_url"], "https://example.com/s/truite")
request.params = {}
request.matchdict = {}
self.assertRaises(HTTPBadRequest, shortener.create)
request.params = {
"url": "https://other-site.com/hi"
}
self.assertRaises(HTTPBadRequest, shortener.create)
示例12: test_staff_remove_sets_staff_to_False
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_staff_remove_sets_staff_to_False(User):
User.staff_members.return_value = [Mock(username="fred"),
Mock(username="bob"),
Mock(username="frank")]
request = DummyRequest(params={"remove": "fred"})
request.route_url = Mock()
user = Mock(staff=True)
User.get_by_username.return_value = user
admin.staff_remove(request)
assert user.staff is False
示例13: test_admins_remove_sets_admin_to_False
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_admins_remove_sets_admin_to_False(User):
User.admins.return_value = [Mock(username="fred"),
Mock(username="bob"),
Mock(username="frank")]
request = DummyRequest(params={"remove": "fred"})
request.route_url = Mock()
user = Mock(admin=True)
User.get_by_username.return_value = user
admin.admins_remove(request)
assert user.admin is False
示例14: test_shortener_noreplace_2
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_shortener_noreplace_2(self):
from pyramid.testing import DummyRequest
from c2cgeoportal_geoportal.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/s/truite"
}
result = shortener.create()
self.assertEqual(result["short_url"], "https://example.com/s/truite")
示例15: test_upload
# 需要导入模块: from pyramid.testing import DummyRequest [as 别名]
# 或者: from pyramid.testing.DummyRequest import route_url [as 别名]
def test_upload():
"""
Reference: Resumable uploads over HTTP. Protocol specification
http://www.grid.net.ru/nginx/resumable_uploads.en.html
"""
with tempfile.TemporaryDirectory() as tempdir:
#Example 1: Request from client containing the first segment of the file
#
#POST /upload HTTP/1.1
#Host: example.com
#Content-Length: 51201
#Content-Type: application/octet-stream
#Content-Disposition: attachment; filename="big.TXT"
#X-Content-Range: bytes 0-51200/511920
#Session-ID: 1111215056
#
#<bytes 0-51200>
#env = copy.copy(request_environ)
#env.update({'REQUEST_METHOD': 'POST'})
request = DummyRequest(headers={
'Content-Length': 51201,
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment; filename="big.TXT"',
'X-Content-Range': 'bytes 0-51200/511920',
'Session-ID': 1111215056,
})
request.registry.settings = {'upload.path': tempdir}
request.body = ('x' * 51201).encode('utf-8') # fake body content
response = Upload(request).post()
#Example 2: Response to a request containing first segment of a file
#
#HTTP/1.1 201 Created
#Date: Thu, 02 Sep 2010 12:54:40 GMT
#Content-Length: 14
#Connection: close
#Range: 0-51200/511920
#
#0-51200/511920
assert request.response.status_code == 201
assert response == '0-51200/511920'
assert request.response.headers['Range'] == '0-51200/511920'
# Simulate writing the middle of file
with open(os.path.join(tempdir, '1111215056', 'big.TXT', '51200'), 'wb') as file:
file.write(('z' * 409608).encode('utf-8'))
#Example 3: Request from client containing the last segment of the file
#
#POST /upload HTTP/1.1
#Host: example.com
#Content-Length: 51111
#Content-Type: application/octet-stream
#Content-Disposition: attachment; filename="big.TXT"
#X-Content-Range: bytes 460809-511919/511920
#Session-ID: 1111215056
#
#<bytes 460809-511919>
request = DummyRequest(headers={
'Content-Length': 51111,
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment; filename="big.TXT"',
'X-Content-Range': 'bytes 460809-511919/511920',
'Session-ID': 1111215056,
})
request.registry.settings = {'upload.path': tempdir}
request.route_url = Mock(return_value='')
request.body = ('y' * 51111).encode('utf-8') # fake body content
response = Upload(request).post()
#Example 4: Response to a request containing last segment of a file
#
#HTTP/1.1 200 OK
#Date: Thu, 02 Sep 2010 12:54:43 GMT
#Content-Type: text/html
#Connection: close
#Content-Length: 2270
#
#<response body>
assert response['files'][0]['name'] == 'big.TXT'
assert response['files'][0]['size'] == 511920
with open(os.path.join(tempdir, 'big.TXT'), 'rb') as file:
data = file.read()
assert chr(data[0]) == 'x'
assert chr(data[-1]) == 'y'