/usr/lib/python3/dist-packages/klein/test/test_app.py is in python3-klein 17.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | from __future__ import absolute_import, division
from twisted.trial import unittest
import sys
from mock import Mock, patch
from twisted.python.components import registerAdapter
from klein import Klein
from klein.app import KleinRequest
from klein.interfaces import IKleinRequest
from klein.test.util import EqualityTestsMixin
class DummyRequest(object):
def __init__(self, n):
self.n = n
def __eq__(self, other):
return other.n == self.n
def __repr__(self):
return '<DummyRequest({n})>'.format(n=self.n)
registerAdapter(KleinRequest, DummyRequest, IKleinRequest)
class KleinEqualityTestCase(unittest.TestCase, EqualityTestsMixin):
"""
Tests for L{Klein}'s implementation of C{==} and C{!=}.
"""
class _One(object):
app = Klein()
def __eq__(self, other):
return True
def __ne__(self, other):
return False
def __hash__(self):
return id(self)
_another = Klein()
def anInstance(self):
# This is actually a new Klein instance every time since Klein.__get__
# creates a new Klein instance for every instance it is retrieved from.
# The different _One instance, at least, will not cause the Klein
# instances to be not-equal to each other since an instance of _One is
# equal to everything.
return self._One().app
def anotherInstance(self):
return self._another
class KleinTestCase(unittest.TestCase):
def test_route(self):
"""
L{Klein.route} adds functions as routable endpoints.
"""
app = Klein()
@app.route("/foo")
def foo(request):
return "foo"
c = app.url_map.bind("foo")
self.assertEqual(c.match("/foo"), ("foo", {}))
self.assertEqual(len(app.endpoints), 1)
self.assertEqual(app.execute_endpoint("foo", DummyRequest(1)), "foo")
def test_submountedRoute(self):
"""
L{Klein.subroute} adds functions as routable endpoints.
"""
app = Klein()
with app.subroute("/sub") as app:
@app.route("/prefixed_uri")
def foo_endpoint(request):
return b"foo"
c = app.url_map.bind("sub/prefixed_uri")
self.assertEqual(
c.match("/sub/prefixed_uri"), ("foo_endpoint", {}))
self.assertEqual(
len(app.endpoints), 1)
self.assertEqual(
app.execute_endpoint("foo_endpoint", DummyRequest(1)), b"foo")
def test_stackedRoute(self):
"""
L{Klein.route} can be stacked to create multiple endpoints of
a single function.
"""
app = Klein()
@app.route("/foo")
@app.route("/bar", endpoint="bar")
def foobar(request):
return "foobar"
self.assertEqual(len(app.endpoints), 2)
c = app.url_map.bind("foo")
self.assertEqual(c.match("/foo"), ("foobar", {}))
self.assertEqual(app.execute_endpoint("foobar", DummyRequest(1)), "foobar")
self.assertEqual(c.match("/bar"), ("bar", {}))
self.assertEqual(app.execute_endpoint("bar", DummyRequest(2)), "foobar")
def test_branchRoute(self):
"""
L{Klein.route} should create a branch path which consumes all children
when the branch keyword argument is True.
"""
app = Klein()
@app.route("/foo/", branch=True)
def foo(request):
return "foo"
c = app.url_map.bind("foo")
self.assertEqual(c.match("/foo/"), ("foo", {}))
self.assertEqual(
c.match("/foo/bar"),
("foo_branch", {'__rest__': 'bar'}))
self.assertEquals(app.endpoints["foo"].__name__, "foo")
self.assertEquals(
app.endpoints["foo_branch"].__name__,
"foo")
def test_classicalRoute(self):
"""
L{Klein.route} may be used a method decorator when a L{Klein} instance
is defined as a class variable.
"""
bar_calls = []
class Foo(object):
app = Klein()
@app.route("/bar")
def bar(self, request):
bar_calls.append((self, request))
return "bar"
foo = Foo()
c = foo.app.url_map.bind("bar")
self.assertEqual(c.match("/bar"), ("bar", {}))
self.assertEquals(foo.app.execute_endpoint("bar", DummyRequest(1)), "bar")
self.assertEqual(bar_calls, [(foo, DummyRequest(1))])
def test_classicalRouteWithTwoInstances(self):
"""
Multiple instances of a class with a L{Klein} attribute and
L{Klein.route}'d methods can be created and their L{Klein}s used
independently.
"""
class Foo(object):
app = Klein()
def __init__(self):
self.bar_calls = []
@app.route("/bar")
def bar(self, request):
self.bar_calls.append((self, request))
return "bar"
foo_1 = Foo()
foo_1_app = foo_1.app
foo_2 = Foo()
foo_2_app = foo_2.app
dr1 = DummyRequest(1)
dr2 = DummyRequest(2)
foo_1_app.execute_endpoint('bar', dr1)
foo_2_app.execute_endpoint('bar', dr2)
self.assertEqual(foo_1.bar_calls, [(foo_1, dr1)])
self.assertEqual(foo_2.bar_calls, [(foo_2, dr2)])
def test_classicalRouteWithBranch(self):
"""
Multiple instances of a class with a L{Klein} attribute and
L{Klein.route}'d methods can be created and their L{Klein}s used
independently.
"""
class Foo(object):
app = Klein()
def __init__(self):
self.bar_calls = []
@app.route("/bar/", branch=True)
def bar(self, request):
self.bar_calls.append((self, request))
return "bar"
foo_1 = Foo()
foo_1_app = foo_1.app
foo_2 = Foo()
foo_2_app = foo_2.app
dr1 = DummyRequest(1)
dr2 = DummyRequest(2)
foo_1_app.execute_endpoint('bar_branch', dr1)
foo_2_app.execute_endpoint('bar_branch', dr2)
self.assertEqual(foo_1.bar_calls, [(foo_1, dr1)])
self.assertEqual(foo_2.bar_calls, [(foo_2, dr2)])
def test_branchDoesntRequireTrailingSlash(self):
"""
L{Klein.route} should create a branch path which consumes all children,
when the branch keyword argument is True and there is no trailing /
on the path.
"""
app = Klein()
@app.route("/foo", branch=True)
def foo(request):
return "foo"
c = app.url_map.bind("foo")
self.assertEqual(c.match("/foo/bar"),
("foo_branch", {"__rest__": "bar"}))
@patch('klein.app.KleinResource')
@patch('klein.app.Site')
@patch('klein.app.log')
@patch('klein.app.reactor')
def test_run(self, reactor, mock_log, mock_site, mock_kr):
"""
L{Klein.run} configures a L{KleinResource} and a L{Site}
listening on the specified interface and port, and logs
to stdout.
"""
app = Klein()
app.run("localhost", 8080)
reactor.listenTCP.assert_called_with(
8080, mock_site.return_value, backlog=50, interface="localhost")
reactor.run.assert_called_with()
mock_site.assert_called_with(mock_kr.return_value)
mock_kr.assert_called_with(app)
mock_log.startLogging.assert_called_with(sys.stdout)
@patch('klein.app.KleinResource')
@patch('klein.app.Site')
@patch('klein.app.log')
@patch('klein.app.reactor')
def test_runWithLogFile(self, reactor, mock_log, mock_site, mock_kr):
"""
L{Klein.run} logs to the specified C{logFile}.
"""
app = Klein()
logFile = Mock()
app.run("localhost", 8080, logFile=logFile)
reactor.listenTCP.assert_called_with(
8080, mock_site.return_value, backlog=50, interface="localhost")
reactor.run.assert_called_with()
mock_site.assert_called_with(mock_kr.return_value)
mock_kr.assert_called_with(app)
mock_log.startLogging.assert_called_with(logFile)
@patch('klein.app.KleinResource')
@patch('klein.app.log')
@patch('klein.app.endpoints.serverFromString')
@patch('klein.app.reactor')
def test_runTCP6(self, reactor, mock_sfs, mock_log, mock_kr):
"""
L{Klein.run} called with tcp6 endpoint description.
"""
app = Klein()
interface = "2001\:0DB8\:f00e\:eb00\:\:1"
spec = "tcp6:8080:interface={0}".format(interface)
app.run(endpoint_description=spec)
reactor.run.assert_called_with()
mock_sfs.assert_called_with(reactor, spec)
mock_log.startLogging.assert_called_with(sys.stdout)
mock_kr.assert_called_with(app)
@patch('klein.app.KleinResource')
@patch('klein.app.log')
@patch('klein.app.endpoints.serverFromString')
@patch('klein.app.reactor')
def test_runSSL(self, reactor, mock_sfs, mock_log, mock_kr):
"""
L{Klein.run} called with SSL endpoint specification.
"""
app = Klein()
key = "key.pem"
cert = "cert.pem"
dh_params = "dhparam.pem"
spec_template = "ssl:443:privateKey={0}:certKey={1}"
spec = spec_template.format(key, cert, dh_params)
app.run(endpoint_description=spec)
reactor.run.assert_called_with()
mock_sfs.assert_called_with(reactor, spec)
mock_log.startLogging.assert_called_with(sys.stdout)
mock_kr.assert_called_with(app)
@patch('klein.app.KleinResource')
def test_resource(self, mock_kr):
"""
L{Klien.resource} returns a L{KleinResource}.
"""
app = Klein()
resource = app.resource()
mock_kr.assert_called_with(app)
self.assertEqual(mock_kr.return_value, resource)
|