1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Tests for L{twisted.application.strports}.
8 from twisted.trial.unittest import TestCase
9 from twisted.application import strports
10 from twisted.application import internet
11 from twisted.internet.test.test_endpoints import ParserTestCase
12 from twisted.internet.protocol import Factory
13 from twisted.internet.endpoints import TCP4ServerEndpoint, UNIXServerEndpoint
17 class DeprecatedParseTestCase(ParserTestCase):
19 L{strports.parse} is deprecated. It's an alias for a method that is now
20 private in L{twisted.internet.endpoints}.
23 def parse(self, *a, **kw):
24 result = strports.parse(*a, **kw)
25 warnings = self.flushWarnings([self.parse])
26 self.assertEqual(len(warnings), 1)
28 warnings[0]['message'],
29 "twisted.application.strports.parse was deprecated "
30 "in Twisted 10.2.0: in favor of twisted.internet.endpoints.serverFromString")
34 def test_simpleNumeric(self):
36 Base numeric ports should be parsed as TCP.
38 self.assertEqual(self.parse('80', self.f),
39 ('TCP', (80, self.f), {'interface':'', 'backlog':50}))
42 def test_allKeywords(self):
44 A collection of keyword arguments with no prefixed type, like 'port=80',
45 will be parsed as keyword arguments to 'tcp'.
47 self.assertEqual(self.parse('port=80', self.f),
48 ('TCP', (80, self.f), {'interface':'', 'backlog':50}))
52 class ServiceTestCase(TestCase):
54 Tests for L{strports.service}.
57 def test_service(self):
59 L{strports.service} returns a L{StreamServerEndpointService}
60 constructed with an endpoint produced from
61 L{endpoint.serverFromString}, using the same syntax.
63 reactor = object() # the cake is a lie
66 svc = strports.service(
67 'tcp:'+str(aGoodPort), aFactory, reactor=reactor)
68 self.assertIsInstance(svc, internet.StreamServerEndpointService)
70 # See twisted.application.test.test_internet.TestEndpointService.
71 # test_synchronousRaiseRaisesSynchronously
72 self.assertEqual(svc._raiseSynchronously, True)
73 self.assertIsInstance(svc.endpoint, TCP4ServerEndpoint)
74 # Maybe we should implement equality for endpoints.
75 self.assertEqual(svc.endpoint._port, aGoodPort)
76 self.assertIdentical(svc.factory, aFactory)
77 self.assertIdentical(svc.endpoint._reactor, reactor)
80 def test_serviceDefaultReactor(self):
82 L{strports.service} will use the default reactor when none is provided
85 from twisted.internet import reactor as globalReactor
86 aService = strports.service("tcp:80", None)
87 self.assertIdentical(aService.endpoint._reactor, globalReactor)
90 def test_serviceDeprecatedDefault(self):
92 L{strports.service} still accepts a 'default' argument, which will
93 affect the parsing of 'default' (i.e. 'not containing a colon')
94 endpoint descriptions, but this behavior is deprecated.
96 svc = strports.service("8080", None, "unix")
97 self.assertIsInstance(svc.endpoint, UNIXServerEndpoint)
98 warnings = self.flushWarnings([self.test_serviceDeprecatedDefault])
99 self.assertEqual(warnings[0]['category'], DeprecationWarning)
101 warnings[0]['message'],
102 "The 'default' parameter was deprecated in Twisted 10.2.0. "
103 "Use qualified endpoint descriptions; for example, 'tcp:8080'.")
104 self.assertEqual(len(warnings), 1)
106 # Almost the same case, but slightly tricky - explicitly passing the old
107 # default value, None, also must trigger a deprecation warning.
108 svc = strports.service("tcp:8080", None, None)
109 self.assertIsInstance(svc.endpoint, TCP4ServerEndpoint)
110 warnings = self.flushWarnings([self.test_serviceDeprecatedDefault])
111 self.assertEqual(warnings[0]['category'], DeprecationWarning)
113 warnings[0]['message'],
114 "The 'default' parameter was deprecated in Twisted 10.2.0.")
115 self.assertEqual(len(warnings), 1)
118 def test_serviceDeprecatedUnqualified(self):
120 Unqualified strport descriptions, i.e. "8080", are deprecated.
122 svc = strports.service("8080", None)
123 self.assertIsInstance(svc.endpoint, TCP4ServerEndpoint)
124 warnings = self.flushWarnings(
125 [self.test_serviceDeprecatedUnqualified])
126 self.assertEqual(warnings[0]['category'], DeprecationWarning)
128 warnings[0]['message'],
129 "Unqualified strport description passed to 'service'."
130 "Use qualified endpoint descriptions; for example, 'tcp:8080'.")
131 self.assertEqual(len(warnings), 1)