##// END OF EJS Templates
cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp...
Augie Fackler -
r37733:1859b9a7 default
parent child Browse files
Show More
@@ -1,84 +1,90
1 1 from __future__ import absolute_import
2 2
3 3 import unittest
4 4 import silenttestrunner
5 5
6 6 from mercurial import (
7 7 error,
8 8 scmutil,
9 9 )
10 10
11 11 class mockfile(object):
12 12 def __init__(self, name, fs):
13 13 self.name = name
14 14 self.fs = fs
15 15
16 16 def __enter__(self):
17 17 return self
18 18
19 19 def __exit__(self, *args, **kwargs):
20 20 pass
21 21
22 22 def write(self, text):
23 23 self.fs.contents[self.name] = text
24 24
25 25 def read(self):
26 26 return self.fs.contents[self.name]
27 27
28 28 class mockvfs(object):
29 29 def __init__(self):
30 30 self.contents = {}
31 31
32 32 def read(self, path):
33 33 return mockfile(path, self).read()
34 34
35 35 def readlines(self, path):
36 36 # lines need to contain the trailing '\n' to mock the real readlines
37 37 return [l for l in mockfile(path, self).read().splitlines(True)]
38 38
39 39 def __call__(self, path, mode, atomictemp):
40 40 return mockfile(path, self)
41 41
42 42 class testsimplekeyvaluefile(unittest.TestCase):
43 43 def setUp(self):
44 44 self.vfs = mockvfs()
45 45
46 46 def testbasicwritingiandreading(self):
47 47 dw = {'key1': 'value1', 'Key2': 'value2'}
48 48 scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(dw)
49 49 self.assertEqual(sorted(self.vfs.read('kvfile').split('\n')),
50 50 ['', 'Key2=value2', 'key1=value1'])
51 51 dr = scmutil.simplekeyvaluefile(self.vfs, 'kvfile').read()
52 52 self.assertEqual(dr, dw)
53 53
54 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
55 # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
56 # the regex version.
57 assertRaisesRegex = (# camelcase-required
58 unittest.TestCase.assertRaisesRegexp)
59
54 60 def testinvalidkeys(self):
55 61 d = {'0key1': 'value1', 'Key2': 'value2'}
56 with self.assertRaisesRegexp(error.ProgrammingError,
62 with self.assertRaisesRegex(error.ProgrammingError,
57 63 'keys must start with a letter.*'):
58 64 scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
59 65
60 66 d = {'key1@': 'value1', 'Key2': 'value2'}
61 with self.assertRaisesRegexp(error.ProgrammingError, 'invalid key.*'):
67 with self.assertRaisesRegex(error.ProgrammingError, 'invalid key.*'):
62 68 scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
63 69
64 70 def testinvalidvalues(self):
65 71 d = {'key1': 'value1', 'Key2': 'value2\n'}
66 with self.assertRaisesRegexp(error.ProgrammingError, 'invalid val.*'):
72 with self.assertRaisesRegex(error.ProgrammingError, 'invalid val.*'):
67 73 scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
68 74
69 75 def testcorruptedfile(self):
70 76 self.vfs.contents['badfile'] = 'ababagalamaga\n'
71 with self.assertRaisesRegexp(error.CorruptedState,
77 with self.assertRaisesRegex(error.CorruptedState,
72 78 'dictionary.*element.*'):
73 79 scmutil.simplekeyvaluefile(self.vfs, 'badfile').read()
74 80
75 81 def testfirstline(self):
76 82 dw = {'key1': 'value1'}
77 83 scmutil.simplekeyvaluefile(self.vfs, 'fl').write(dw, firstline='1.0')
78 84 self.assertEqual(self.vfs.read('fl'), '1.0\nkey1=value1\n')
79 85 dr = scmutil.simplekeyvaluefile(self.vfs, 'fl')\
80 86 .read(firstlinenonkeyval=True)
81 87 self.assertEqual(dr, {'__firstline': '1.0', 'key1': 'value1'})
82 88
83 89 if __name__ == "__main__":
84 90 silenttestrunner.main(__name__)
@@ -1,130 +1,143
1 1 from __future__ import absolute_import
2 2
3 3 import unittest
4 4
5 5 from mercurial import (
6 6 error,
7 7 wireprotoframing as framing,
8 8 )
9 9
10 10 ffs = framing.makeframefromhumanstring
11 11
12 12 def sendframe(reactor, frame):
13 13 """Send a frame bytearray to a reactor."""
14 14 header = framing.parseheader(frame)
15 15 payload = frame[framing.FRAME_HEADER_SIZE:]
16 16 assert len(payload) == header.length
17 17
18 18 return reactor.onframerecv(framing.frame(header.requestid,
19 19 header.streamid,
20 20 header.streamflags,
21 21 header.typeid,
22 22 header.flags,
23 23 payload))
24 24
25 25 class SingleSendTests(unittest.TestCase):
26 26 """A reactor that can only send once rejects subsequent sends."""
27
28 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
29 # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
30 # the regex version.
31 assertRaisesRegex = (# camelcase-required
32 unittest.TestCase.assertRaisesRegexp)
33
27 34 def testbasic(self):
28 35 reactor = framing.clientreactor(hasmultiplesend=False, buffersends=True)
29 36
30 37 request, action, meta = reactor.callcommand(b'foo', {})
31 38 self.assertEqual(request.state, b'pending')
32 39 self.assertEqual(action, b'noop')
33 40
34 41 action, meta = reactor.flushcommands()
35 42 self.assertEqual(action, b'sendframes')
36 43
37 44 for frame in meta[b'framegen']:
38 45 self.assertEqual(request.state, b'sending')
39 46
40 47 self.assertEqual(request.state, b'sent')
41 48
42 with self.assertRaisesRegexp(error.ProgrammingError,
49 with self.assertRaisesRegex(error.ProgrammingError,
43 50 'cannot issue new commands'):
44 51 reactor.callcommand(b'foo', {})
45 52
46 with self.assertRaisesRegexp(error.ProgrammingError,
53 with self.assertRaisesRegex(error.ProgrammingError,
47 54 'cannot issue new commands'):
48 55 reactor.callcommand(b'foo', {})
49 56
50 57 class NoBufferTests(unittest.TestCase):
51 58 """A reactor without send buffering sends requests immediately."""
52 59 def testbasic(self):
53 60 reactor = framing.clientreactor(hasmultiplesend=True, buffersends=False)
54 61
55 62 request, action, meta = reactor.callcommand(b'command1', {})
56 63 self.assertEqual(request.requestid, 1)
57 64 self.assertEqual(action, b'sendframes')
58 65
59 66 self.assertEqual(request.state, b'pending')
60 67
61 68 for frame in meta[b'framegen']:
62 69 self.assertEqual(request.state, b'sending')
63 70
64 71 self.assertEqual(request.state, b'sent')
65 72
66 73 action, meta = reactor.flushcommands()
67 74 self.assertEqual(action, b'noop')
68 75
69 76 # And we can send another command.
70 77 request, action, meta = reactor.callcommand(b'command2', {})
71 78 self.assertEqual(request.requestid, 3)
72 79 self.assertEqual(action, b'sendframes')
73 80
74 81 for frame in meta[b'framegen']:
75 82 self.assertEqual(request.state, b'sending')
76 83
77 84 self.assertEqual(request.state, b'sent')
78 85
79 86 class BadFrameRecvTests(unittest.TestCase):
87 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
88 # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
89 # the regex version.
90 assertRaisesRegex = (# camelcase-required
91 unittest.TestCase.assertRaisesRegexp)
92
80 93 def testoddstream(self):
81 94 reactor = framing.clientreactor()
82 95
83 96 action, meta = sendframe(reactor, ffs(b'1 1 0 1 0 foo'))
84 97 self.assertEqual(action, b'error')
85 98 self.assertEqual(meta[b'message'],
86 99 b'received frame with odd numbered stream ID: 1')
87 100
88 101 def testunknownstream(self):
89 102 reactor = framing.clientreactor()
90 103
91 104 action, meta = sendframe(reactor, ffs(b'1 0 0 1 0 foo'))
92 105 self.assertEqual(action, b'error')
93 106 self.assertEqual(meta[b'message'],
94 107 b'received frame on unknown stream without beginning '
95 108 b'of stream flag set')
96 109
97 110 def testunhandledframetype(self):
98 111 reactor = framing.clientreactor(buffersends=False)
99 112
100 113 request, action, meta = reactor.callcommand(b'foo', {})
101 114 for frame in meta[b'framegen']:
102 115 pass
103 116
104 with self.assertRaisesRegexp(error.ProgrammingError,
117 with self.assertRaisesRegex(error.ProgrammingError,
105 118 'unhandled frame type'):
106 119 sendframe(reactor, ffs(b'1 0 stream-begin text-output 0 foo'))
107 120
108 121 class StreamTests(unittest.TestCase):
109 122 def testmultipleresponseframes(self):
110 123 reactor = framing.clientreactor(buffersends=False)
111 124
112 125 request, action, meta = reactor.callcommand(b'foo', {})
113 126
114 127 self.assertEqual(action, b'sendframes')
115 128 for f in meta[b'framegen']:
116 129 pass
117 130
118 131 action, meta = sendframe(
119 132 reactor,
120 133 ffs(b'%d 0 stream-begin 4 0 foo' % request.requestid))
121 134 self.assertEqual(action, b'responsedata')
122 135
123 136 action, meta = sendframe(
124 137 reactor,
125 138 ffs(b'%d 0 0 4 eos bar' % request.requestid))
126 139 self.assertEqual(action, b'responsedata')
127 140
128 141 if __name__ == '__main__':
129 142 import silenttestrunner
130 143 silenttestrunner.main(__name__)
@@ -1,191 +1,197
1 1 from __future__ import absolute_import, print_function
2 2
3 3 import unittest
4 4
5 5 from mercurial import (
6 6 util,
7 7 wireprotoframing as framing,
8 8 )
9 9
10 10 ffs = framing.makeframefromhumanstring
11 11
12 12 class FrameHumanStringTests(unittest.TestCase):
13 13 def testbasic(self):
14 14 self.assertEqual(ffs(b'1 1 0 1 0 '),
15 15 b'\x00\x00\x00\x01\x00\x01\x00\x10')
16 16
17 17 self.assertEqual(ffs(b'2 4 0 1 0 '),
18 18 b'\x00\x00\x00\x02\x00\x04\x00\x10')
19 19
20 20 self.assertEqual(ffs(b'2 4 0 1 0 foo'),
21 21 b'\x03\x00\x00\x02\x00\x04\x00\x10foo')
22 22
23 23 def testcborint(self):
24 24 self.assertEqual(ffs(b'1 1 0 1 0 cbor:15'),
25 25 b'\x01\x00\x00\x01\x00\x01\x00\x10\x0f')
26 26
27 27 self.assertEqual(ffs(b'1 1 0 1 0 cbor:42'),
28 28 b'\x02\x00\x00\x01\x00\x01\x00\x10\x18*')
29 29
30 30 self.assertEqual(ffs(b'1 1 0 1 0 cbor:1048576'),
31 31 b'\x05\x00\x00\x01\x00\x01\x00\x10\x1a'
32 32 b'\x00\x10\x00\x00')
33 33
34 34 self.assertEqual(ffs(b'1 1 0 1 0 cbor:0'),
35 35 b'\x01\x00\x00\x01\x00\x01\x00\x10\x00')
36 36
37 37 self.assertEqual(ffs(b'1 1 0 1 0 cbor:-1'),
38 38 b'\x01\x00\x00\x01\x00\x01\x00\x10 ')
39 39
40 40 self.assertEqual(ffs(b'1 1 0 1 0 cbor:-342542'),
41 41 b'\x05\x00\x00\x01\x00\x01\x00\x10:\x00\x05:\r')
42 42
43 43 def testcborstrings(self):
44 44 self.assertEqual(ffs(b"1 1 0 1 0 cbor:b'foo'"),
45 45 b'\x04\x00\x00\x01\x00\x01\x00\x10Cfoo')
46 46
47 47 self.assertEqual(ffs(b"1 1 0 1 0 cbor:u'foo'"),
48 48 b'\x04\x00\x00\x01\x00\x01\x00\x10cfoo')
49 49
50 50 def testcborlists(self):
51 51 self.assertEqual(ffs(b"1 1 0 1 0 cbor:[None, True, False, 42, b'foo']"),
52 52 b'\n\x00\x00\x01\x00\x01\x00\x10\x85\xf6\xf5\xf4'
53 53 b'\x18*Cfoo')
54 54
55 55 def testcbordicts(self):
56 56 self.assertEqual(ffs(b"1 1 0 1 0 "
57 57 b"cbor:{b'foo': b'val1', b'bar': b'val2'}"),
58 58 b'\x13\x00\x00\x01\x00\x01\x00\x10\xa2'
59 59 b'CbarDval2CfooDval1')
60 60
61 61 class FrameTests(unittest.TestCase):
62 62 def testdataexactframesize(self):
63 63 data = util.bytesio(b'x' * framing.DEFAULT_MAX_FRAME_SIZE)
64 64
65 65 stream = framing.stream(1)
66 66 frames = list(framing.createcommandframes(stream, 1, b'command',
67 67 {}, data))
68 68 self.assertEqual(frames, [
69 69 ffs(b'1 1 stream-begin command-request new|have-data '
70 70 b"cbor:{b'name': b'command'}"),
71 71 ffs(b'1 1 0 command-data continuation %s' % data.getvalue()),
72 72 ffs(b'1 1 0 command-data eos ')
73 73 ])
74 74
75 75 def testdatamultipleframes(self):
76 76 data = util.bytesio(b'x' * (framing.DEFAULT_MAX_FRAME_SIZE + 1))
77 77
78 78 stream = framing.stream(1)
79 79 frames = list(framing.createcommandframes(stream, 1, b'command', {},
80 80 data))
81 81 self.assertEqual(frames, [
82 82 ffs(b'1 1 stream-begin command-request new|have-data '
83 83 b"cbor:{b'name': b'command'}"),
84 84 ffs(b'1 1 0 command-data continuation %s' % (
85 85 b'x' * framing.DEFAULT_MAX_FRAME_SIZE)),
86 86 ffs(b'1 1 0 command-data eos x'),
87 87 ])
88 88
89 89 def testargsanddata(self):
90 90 data = util.bytesio(b'x' * 100)
91 91
92 92 stream = framing.stream(1)
93 93 frames = list(framing.createcommandframes(stream, 1, b'command', {
94 94 b'key1': b'key1value',
95 95 b'key2': b'key2value',
96 96 b'key3': b'key3value',
97 97 }, data))
98 98
99 99 self.assertEqual(frames, [
100 100 ffs(b'1 1 stream-begin command-request new|have-data '
101 101 b"cbor:{b'name': b'command', b'args': {b'key1': b'key1value', "
102 102 b"b'key2': b'key2value', b'key3': b'key3value'}}"),
103 103 ffs(b'1 1 0 command-data eos %s' % data.getvalue()),
104 104 ])
105 105
106 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
107 # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
108 # the regex version.
109 assertRaisesRegex = (# camelcase-required
110 unittest.TestCase.assertRaisesRegexp)
111
106 112 def testtextoutputformattingstringtype(self):
107 113 """Formatting string must be bytes."""
108 with self.assertRaisesRegexp(ValueError, 'must use bytes formatting '):
114 with self.assertRaisesRegex(ValueError, 'must use bytes formatting '):
109 115 list(framing.createtextoutputframe(None, 1, [
110 116 (b'foo'.decode('ascii'), [], [])]))
111 117
112 118 def testtextoutputargumentbytes(self):
113 with self.assertRaisesRegexp(ValueError, 'must use bytes for argument'):
119 with self.assertRaisesRegex(ValueError, 'must use bytes for argument'):
114 120 list(framing.createtextoutputframe(None, 1, [
115 121 (b'foo', [b'foo'.decode('ascii')], [])]))
116 122
117 123 def testtextoutputlabelbytes(self):
118 with self.assertRaisesRegexp(ValueError, 'must use bytes for labels'):
124 with self.assertRaisesRegex(ValueError, 'must use bytes for labels'):
119 125 list(framing.createtextoutputframe(None, 1, [
120 126 (b'foo', [], [b'foo'.decode('ascii')])]))
121 127
122 128 def testtextoutput1simpleatom(self):
123 129 stream = framing.stream(1)
124 130 val = list(framing.createtextoutputframe(stream, 1, [
125 131 (b'foo', [], [])]))
126 132
127 133 self.assertEqual(val, [
128 134 ffs(b'1 1 stream-begin text-output 0 '
129 135 b"cbor:[{b'msg': b'foo'}]"),
130 136 ])
131 137
132 138 def testtextoutput2simpleatoms(self):
133 139 stream = framing.stream(1)
134 140 val = list(framing.createtextoutputframe(stream, 1, [
135 141 (b'foo', [], []),
136 142 (b'bar', [], []),
137 143 ]))
138 144
139 145 self.assertEqual(val, [
140 146 ffs(b'1 1 stream-begin text-output 0 '
141 147 b"cbor:[{b'msg': b'foo'}, {b'msg': b'bar'}]")
142 148 ])
143 149
144 150 def testtextoutput1arg(self):
145 151 stream = framing.stream(1)
146 152 val = list(framing.createtextoutputframe(stream, 1, [
147 153 (b'foo %s', [b'val1'], []),
148 154 ]))
149 155
150 156 self.assertEqual(val, [
151 157 ffs(b'1 1 stream-begin text-output 0 '
152 158 b"cbor:[{b'msg': b'foo %s', b'args': [b'val1']}]")
153 159 ])
154 160
155 161 def testtextoutput2arg(self):
156 162 stream = framing.stream(1)
157 163 val = list(framing.createtextoutputframe(stream, 1, [
158 164 (b'foo %s %s', [b'val', b'value'], []),
159 165 ]))
160 166
161 167 self.assertEqual(val, [
162 168 ffs(b'1 1 stream-begin text-output 0 '
163 169 b"cbor:[{b'msg': b'foo %s %s', b'args': [b'val', b'value']}]")
164 170 ])
165 171
166 172 def testtextoutput1label(self):
167 173 stream = framing.stream(1)
168 174 val = list(framing.createtextoutputframe(stream, 1, [
169 175 (b'foo', [], [b'label']),
170 176 ]))
171 177
172 178 self.assertEqual(val, [
173 179 ffs(b'1 1 stream-begin text-output 0 '
174 180 b"cbor:[{b'msg': b'foo', b'labels': [b'label']}]")
175 181 ])
176 182
177 183 def testargandlabel(self):
178 184 stream = framing.stream(1)
179 185 val = list(framing.createtextoutputframe(stream, 1, [
180 186 (b'foo %s', [b'arg'], [b'label']),
181 187 ]))
182 188
183 189 self.assertEqual(val, [
184 190 ffs(b'1 1 stream-begin text-output 0 '
185 191 b"cbor:[{b'msg': b'foo %s', b'args': [b'arg'], "
186 192 b"b'labels': [b'label']}]")
187 193 ])
188 194
189 195 if __name__ == '__main__':
190 196 import silenttestrunner
191 197 silenttestrunner.main(__name__)
@@ -1,416 +1,422
1 1 from __future__ import absolute_import, print_function
2 2
3 3 import unittest
4 4
5 5 from mercurial.hgweb import (
6 6 request as requestmod,
7 7 )
8 8 from mercurial import (
9 9 error,
10 10 )
11 11
12 12 DEFAULT_ENV = {
13 13 r'REQUEST_METHOD': r'GET',
14 14 r'SERVER_NAME': r'testserver',
15 15 r'SERVER_PORT': r'80',
16 16 r'SERVER_PROTOCOL': r'http',
17 17 r'wsgi.version': (1, 0),
18 18 r'wsgi.url_scheme': r'http',
19 19 r'wsgi.input': None,
20 20 r'wsgi.errors': None,
21 21 r'wsgi.multithread': False,
22 22 r'wsgi.multiprocess': True,
23 23 r'wsgi.run_once': False,
24 24 }
25 25
26 26 def parse(env, reponame=None, altbaseurl=None, extra=None):
27 27 env = dict(env)
28 28 env.update(extra or {})
29 29
30 30 return requestmod.parserequestfromenv(env, reponame=reponame,
31 31 altbaseurl=altbaseurl)
32 32
33 33 class ParseRequestTests(unittest.TestCase):
34 34 def testdefault(self):
35 35 r = parse(DEFAULT_ENV)
36 36 self.assertEqual(r.url, b'http://testserver')
37 37 self.assertEqual(r.baseurl, b'http://testserver')
38 38 self.assertEqual(r.advertisedurl, b'http://testserver')
39 39 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
40 40 self.assertEqual(r.urlscheme, b'http')
41 41 self.assertEqual(r.method, b'GET')
42 42 self.assertIsNone(r.remoteuser)
43 43 self.assertIsNone(r.remotehost)
44 44 self.assertEqual(r.apppath, b'')
45 45 self.assertEqual(r.dispatchparts, [])
46 46 self.assertIsNone(r.dispatchpath)
47 47 self.assertIsNone(r.reponame)
48 48 self.assertEqual(r.querystring, b'')
49 49 self.assertEqual(len(r.qsparams), 0)
50 50 self.assertEqual(len(r.headers), 0)
51 51
52 52 def testcustomport(self):
53 53 r = parse(DEFAULT_ENV, extra={
54 54 r'SERVER_PORT': r'8000',
55 55 })
56 56
57 57 self.assertEqual(r.url, b'http://testserver:8000')
58 58 self.assertEqual(r.baseurl, b'http://testserver:8000')
59 59 self.assertEqual(r.advertisedurl, b'http://testserver:8000')
60 60 self.assertEqual(r.advertisedbaseurl, b'http://testserver:8000')
61 61
62 62 r = parse(DEFAULT_ENV, extra={
63 63 r'SERVER_PORT': r'4000',
64 64 r'wsgi.url_scheme': r'https',
65 65 })
66 66
67 67 self.assertEqual(r.url, b'https://testserver:4000')
68 68 self.assertEqual(r.baseurl, b'https://testserver:4000')
69 69 self.assertEqual(r.advertisedurl, b'https://testserver:4000')
70 70 self.assertEqual(r.advertisedbaseurl, b'https://testserver:4000')
71 71
72 72 def testhttphost(self):
73 73 r = parse(DEFAULT_ENV, extra={
74 74 r'HTTP_HOST': r'altserver',
75 75 })
76 76
77 77 self.assertEqual(r.url, b'http://altserver')
78 78 self.assertEqual(r.baseurl, b'http://altserver')
79 79 self.assertEqual(r.advertisedurl, b'http://testserver')
80 80 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
81 81
82 82 def testscriptname(self):
83 83 r = parse(DEFAULT_ENV, extra={
84 84 r'SCRIPT_NAME': r'',
85 85 })
86 86
87 87 self.assertEqual(r.url, b'http://testserver')
88 88 self.assertEqual(r.baseurl, b'http://testserver')
89 89 self.assertEqual(r.advertisedurl, b'http://testserver')
90 90 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
91 91 self.assertEqual(r.apppath, b'')
92 92 self.assertEqual(r.dispatchparts, [])
93 93 self.assertIsNone(r.dispatchpath)
94 94
95 95 r = parse(DEFAULT_ENV, extra={
96 96 r'SCRIPT_NAME': r'/script',
97 97 })
98 98
99 99 self.assertEqual(r.url, b'http://testserver/script')
100 100 self.assertEqual(r.baseurl, b'http://testserver')
101 101 self.assertEqual(r.advertisedurl, b'http://testserver/script')
102 102 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
103 103 self.assertEqual(r.apppath, b'/script')
104 104 self.assertEqual(r.dispatchparts, [])
105 105 self.assertIsNone(r.dispatchpath)
106 106
107 107 r = parse(DEFAULT_ENV, extra={
108 108 r'SCRIPT_NAME': r'/multiple words',
109 109 })
110 110
111 111 self.assertEqual(r.url, b'http://testserver/multiple%20words')
112 112 self.assertEqual(r.baseurl, b'http://testserver')
113 113 self.assertEqual(r.advertisedurl, b'http://testserver/multiple%20words')
114 114 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
115 115 self.assertEqual(r.apppath, b'/multiple words')
116 116 self.assertEqual(r.dispatchparts, [])
117 117 self.assertIsNone(r.dispatchpath)
118 118
119 119 def testpathinfo(self):
120 120 r = parse(DEFAULT_ENV, extra={
121 121 r'PATH_INFO': r'',
122 122 })
123 123
124 124 self.assertEqual(r.url, b'http://testserver')
125 125 self.assertEqual(r.baseurl, b'http://testserver')
126 126 self.assertEqual(r.advertisedurl, b'http://testserver')
127 127 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
128 128 self.assertEqual(r.apppath, b'')
129 129 self.assertEqual(r.dispatchparts, [])
130 130 self.assertEqual(r.dispatchpath, b'')
131 131
132 132 r = parse(DEFAULT_ENV, extra={
133 133 r'PATH_INFO': r'/pathinfo',
134 134 })
135 135
136 136 self.assertEqual(r.url, b'http://testserver/pathinfo')
137 137 self.assertEqual(r.baseurl, b'http://testserver')
138 138 self.assertEqual(r.advertisedurl, b'http://testserver/pathinfo')
139 139 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
140 140 self.assertEqual(r.apppath, b'')
141 141 self.assertEqual(r.dispatchparts, [b'pathinfo'])
142 142 self.assertEqual(r.dispatchpath, b'pathinfo')
143 143
144 144 r = parse(DEFAULT_ENV, extra={
145 145 r'PATH_INFO': r'/one/two/',
146 146 })
147 147
148 148 self.assertEqual(r.url, b'http://testserver/one/two/')
149 149 self.assertEqual(r.baseurl, b'http://testserver')
150 150 self.assertEqual(r.advertisedurl, b'http://testserver/one/two/')
151 151 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
152 152 self.assertEqual(r.apppath, b'')
153 153 self.assertEqual(r.dispatchparts, [b'one', b'two'])
154 154 self.assertEqual(r.dispatchpath, b'one/two')
155 155
156 156 def testscriptandpathinfo(self):
157 157 r = parse(DEFAULT_ENV, extra={
158 158 r'SCRIPT_NAME': r'/script',
159 159 r'PATH_INFO': r'/pathinfo',
160 160 })
161 161
162 162 self.assertEqual(r.url, b'http://testserver/script/pathinfo')
163 163 self.assertEqual(r.baseurl, b'http://testserver')
164 164 self.assertEqual(r.advertisedurl, b'http://testserver/script/pathinfo')
165 165 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
166 166 self.assertEqual(r.apppath, b'/script')
167 167 self.assertEqual(r.dispatchparts, [b'pathinfo'])
168 168 self.assertEqual(r.dispatchpath, b'pathinfo')
169 169
170 170 r = parse(DEFAULT_ENV, extra={
171 171 r'SCRIPT_NAME': r'/script1/script2',
172 172 r'PATH_INFO': r'/path1/path2',
173 173 })
174 174
175 175 self.assertEqual(r.url,
176 176 b'http://testserver/script1/script2/path1/path2')
177 177 self.assertEqual(r.baseurl, b'http://testserver')
178 178 self.assertEqual(r.advertisedurl,
179 179 b'http://testserver/script1/script2/path1/path2')
180 180 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
181 181 self.assertEqual(r.apppath, b'/script1/script2')
182 182 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
183 183 self.assertEqual(r.dispatchpath, b'path1/path2')
184 184
185 185 r = parse(DEFAULT_ENV, extra={
186 186 r'HTTP_HOST': r'hostserver',
187 187 r'SCRIPT_NAME': r'/script',
188 188 r'PATH_INFO': r'/pathinfo',
189 189 })
190 190
191 191 self.assertEqual(r.url, b'http://hostserver/script/pathinfo')
192 192 self.assertEqual(r.baseurl, b'http://hostserver')
193 193 self.assertEqual(r.advertisedurl, b'http://testserver/script/pathinfo')
194 194 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
195 195 self.assertEqual(r.apppath, b'/script')
196 196 self.assertEqual(r.dispatchparts, [b'pathinfo'])
197 197 self.assertEqual(r.dispatchpath, b'pathinfo')
198 198
199 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
200 # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
201 # the regex version.
202 assertRaisesRegex = (# camelcase-required
203 unittest.TestCase.assertRaisesRegexp)
204
199 205 def testreponame(self):
200 206 """repository path components get stripped from URL."""
201 207
202 with self.assertRaisesRegexp(error.ProgrammingError,
208 with self.assertRaisesRegex(error.ProgrammingError,
203 209 b'reponame requires PATH_INFO'):
204 210 parse(DEFAULT_ENV, reponame=b'repo')
205 211
206 with self.assertRaisesRegexp(error.ProgrammingError,
212 with self.assertRaisesRegex(error.ProgrammingError,
207 213 b'PATH_INFO does not begin with repo '
208 214 b'name'):
209 215 parse(DEFAULT_ENV, reponame=b'repo', extra={
210 216 r'PATH_INFO': r'/pathinfo',
211 217 })
212 218
213 with self.assertRaisesRegexp(error.ProgrammingError,
219 with self.assertRaisesRegex(error.ProgrammingError,
214 220 b'reponame prefix of PATH_INFO'):
215 221 parse(DEFAULT_ENV, reponame=b'repo', extra={
216 222 r'PATH_INFO': r'/repoextra/path',
217 223 })
218 224
219 225 r = parse(DEFAULT_ENV, reponame=b'repo', extra={
220 226 r'PATH_INFO': r'/repo/path1/path2',
221 227 })
222 228
223 229 self.assertEqual(r.url, b'http://testserver/repo/path1/path2')
224 230 self.assertEqual(r.baseurl, b'http://testserver')
225 231 self.assertEqual(r.advertisedurl, b'http://testserver/repo/path1/path2')
226 232 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
227 233 self.assertEqual(r.apppath, b'/repo')
228 234 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
229 235 self.assertEqual(r.dispatchpath, b'path1/path2')
230 236 self.assertEqual(r.reponame, b'repo')
231 237
232 238 r = parse(DEFAULT_ENV, reponame=b'prefix/repo', extra={
233 239 r'PATH_INFO': r'/prefix/repo/path1/path2',
234 240 })
235 241
236 242 self.assertEqual(r.url, b'http://testserver/prefix/repo/path1/path2')
237 243 self.assertEqual(r.baseurl, b'http://testserver')
238 244 self.assertEqual(r.advertisedurl,
239 245 b'http://testserver/prefix/repo/path1/path2')
240 246 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
241 247 self.assertEqual(r.apppath, b'/prefix/repo')
242 248 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
243 249 self.assertEqual(r.dispatchpath, b'path1/path2')
244 250 self.assertEqual(r.reponame, b'prefix/repo')
245 251
246 252 def testaltbaseurl(self):
247 253 # Simple hostname remap.
248 254 r = parse(DEFAULT_ENV, altbaseurl='http://altserver')
249 255
250 256 self.assertEqual(r.url, b'http://testserver')
251 257 self.assertEqual(r.baseurl, b'http://testserver')
252 258 self.assertEqual(r.advertisedurl, b'http://altserver')
253 259 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
254 260 self.assertEqual(r.urlscheme, b'http')
255 261 self.assertEqual(r.apppath, b'')
256 262 self.assertEqual(r.dispatchparts, [])
257 263 self.assertIsNone(r.dispatchpath)
258 264 self.assertIsNone(r.reponame)
259 265
260 266 # With a custom port.
261 267 r = parse(DEFAULT_ENV, altbaseurl='http://altserver:8000')
262 268 self.assertEqual(r.url, b'http://testserver')
263 269 self.assertEqual(r.baseurl, b'http://testserver')
264 270 self.assertEqual(r.advertisedurl, b'http://altserver:8000')
265 271 self.assertEqual(r.advertisedbaseurl, b'http://altserver:8000')
266 272 self.assertEqual(r.urlscheme, b'http')
267 273 self.assertEqual(r.apppath, b'')
268 274 self.assertEqual(r.dispatchparts, [])
269 275 self.assertIsNone(r.dispatchpath)
270 276 self.assertIsNone(r.reponame)
271 277
272 278 # With a changed protocol.
273 279 r = parse(DEFAULT_ENV, altbaseurl='https://altserver')
274 280 self.assertEqual(r.url, b'http://testserver')
275 281 self.assertEqual(r.baseurl, b'http://testserver')
276 282 self.assertEqual(r.advertisedurl, b'https://altserver')
277 283 self.assertEqual(r.advertisedbaseurl, b'https://altserver')
278 284 # URL scheme is defined as the actual scheme, not advertised.
279 285 self.assertEqual(r.urlscheme, b'http')
280 286 self.assertEqual(r.apppath, b'')
281 287 self.assertEqual(r.dispatchparts, [])
282 288 self.assertIsNone(r.dispatchpath)
283 289 self.assertIsNone(r.reponame)
284 290
285 291 # Need to specify explicit port number for proper https:// alt URLs.
286 292 r = parse(DEFAULT_ENV, altbaseurl='https://altserver:443')
287 293 self.assertEqual(r.url, b'http://testserver')
288 294 self.assertEqual(r.baseurl, b'http://testserver')
289 295 self.assertEqual(r.advertisedurl, b'https://altserver')
290 296 self.assertEqual(r.advertisedbaseurl, b'https://altserver')
291 297 self.assertEqual(r.urlscheme, b'http')
292 298 self.assertEqual(r.apppath, b'')
293 299 self.assertEqual(r.dispatchparts, [])
294 300 self.assertIsNone(r.dispatchpath)
295 301 self.assertIsNone(r.reponame)
296 302
297 303 # With only PATH_INFO defined.
298 304 r = parse(DEFAULT_ENV, altbaseurl='http://altserver', extra={
299 305 r'PATH_INFO': r'/path1/path2',
300 306 })
301 307 self.assertEqual(r.url, b'http://testserver/path1/path2')
302 308 self.assertEqual(r.baseurl, b'http://testserver')
303 309 self.assertEqual(r.advertisedurl, b'http://altserver/path1/path2')
304 310 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
305 311 self.assertEqual(r.urlscheme, b'http')
306 312 self.assertEqual(r.apppath, b'')
307 313 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
308 314 self.assertEqual(r.dispatchpath, b'path1/path2')
309 315 self.assertIsNone(r.reponame)
310 316
311 317 # Path on alt URL.
312 318 r = parse(DEFAULT_ENV, altbaseurl='http://altserver/altpath')
313 319 self.assertEqual(r.url, b'http://testserver')
314 320 self.assertEqual(r.baseurl, b'http://testserver')
315 321 self.assertEqual(r.advertisedurl, b'http://altserver/altpath')
316 322 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
317 323 self.assertEqual(r.urlscheme, b'http')
318 324 self.assertEqual(r.apppath, b'/altpath')
319 325 self.assertEqual(r.dispatchparts, [])
320 326 self.assertIsNone(r.dispatchpath)
321 327 self.assertIsNone(r.reponame)
322 328
323 329 # With a trailing slash.
324 330 r = parse(DEFAULT_ENV, altbaseurl='http://altserver/altpath/')
325 331 self.assertEqual(r.url, b'http://testserver')
326 332 self.assertEqual(r.baseurl, b'http://testserver')
327 333 self.assertEqual(r.advertisedurl, b'http://altserver/altpath/')
328 334 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
329 335 self.assertEqual(r.urlscheme, b'http')
330 336 self.assertEqual(r.apppath, b'/altpath/')
331 337 self.assertEqual(r.dispatchparts, [])
332 338 self.assertIsNone(r.dispatchpath)
333 339 self.assertIsNone(r.reponame)
334 340
335 341 # PATH_INFO + path on alt URL.
336 342 r = parse(DEFAULT_ENV, altbaseurl='http://altserver/altpath', extra={
337 343 r'PATH_INFO': r'/path1/path2',
338 344 })
339 345 self.assertEqual(r.url, b'http://testserver/path1/path2')
340 346 self.assertEqual(r.baseurl, b'http://testserver')
341 347 self.assertEqual(r.advertisedurl,
342 348 b'http://altserver/altpath/path1/path2')
343 349 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
344 350 self.assertEqual(r.urlscheme, b'http')
345 351 self.assertEqual(r.apppath, b'/altpath')
346 352 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
347 353 self.assertEqual(r.dispatchpath, b'path1/path2')
348 354 self.assertIsNone(r.reponame)
349 355
350 356 # PATH_INFO + path on alt URL with trailing slash.
351 357 r = parse(DEFAULT_ENV, altbaseurl='http://altserver/altpath/', extra={
352 358 r'PATH_INFO': r'/path1/path2',
353 359 })
354 360 self.assertEqual(r.url, b'http://testserver/path1/path2')
355 361 self.assertEqual(r.baseurl, b'http://testserver')
356 362 self.assertEqual(r.advertisedurl,
357 363 b'http://altserver/altpath//path1/path2')
358 364 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
359 365 self.assertEqual(r.urlscheme, b'http')
360 366 self.assertEqual(r.apppath, b'/altpath/')
361 367 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
362 368 self.assertEqual(r.dispatchpath, b'path1/path2')
363 369 self.assertIsNone(r.reponame)
364 370
365 371 # Local SCRIPT_NAME is ignored.
366 372 r = parse(DEFAULT_ENV, altbaseurl='http://altserver', extra={
367 373 r'SCRIPT_NAME': r'/script',
368 374 r'PATH_INFO': r'/path1/path2',
369 375 })
370 376 self.assertEqual(r.url, b'http://testserver/script/path1/path2')
371 377 self.assertEqual(r.baseurl, b'http://testserver')
372 378 self.assertEqual(r.advertisedurl, b'http://altserver/path1/path2')
373 379 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
374 380 self.assertEqual(r.urlscheme, b'http')
375 381 self.assertEqual(r.apppath, b'')
376 382 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
377 383 self.assertEqual(r.dispatchpath, b'path1/path2')
378 384 self.assertIsNone(r.reponame)
379 385
380 386 # Use remote's path for script name, app path
381 387 r = parse(DEFAULT_ENV, altbaseurl='http://altserver/altroot', extra={
382 388 r'SCRIPT_NAME': r'/script',
383 389 r'PATH_INFO': r'/path1/path2',
384 390 })
385 391 self.assertEqual(r.url, b'http://testserver/script/path1/path2')
386 392 self.assertEqual(r.baseurl, b'http://testserver')
387 393 self.assertEqual(r.advertisedurl,
388 394 b'http://altserver/altroot/path1/path2')
389 395 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
390 396 self.assertEqual(r.urlscheme, b'http')
391 397 self.assertEqual(r.apppath, b'/altroot')
392 398 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
393 399 self.assertEqual(r.dispatchpath, b'path1/path2')
394 400 self.assertIsNone(r.reponame)
395 401
396 402 # reponame is factored in properly.
397 403 r = parse(DEFAULT_ENV, reponame=b'repo',
398 404 altbaseurl='http://altserver/altroot',
399 405 extra={
400 406 r'SCRIPT_NAME': r'/script',
401 407 r'PATH_INFO': r'/repo/path1/path2',
402 408 })
403 409
404 410 self.assertEqual(r.url, b'http://testserver/script/repo/path1/path2')
405 411 self.assertEqual(r.baseurl, b'http://testserver')
406 412 self.assertEqual(r.advertisedurl,
407 413 b'http://altserver/altroot/repo/path1/path2')
408 414 self.assertEqual(r.advertisedbaseurl, b'http://altserver')
409 415 self.assertEqual(r.apppath, b'/altroot/repo')
410 416 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
411 417 self.assertEqual(r.dispatchpath, b'path1/path2')
412 418 self.assertEqual(r.reponame, b'repo')
413 419
414 420 if __name__ == '__main__':
415 421 import silenttestrunner
416 422 silenttestrunner.main(__name__)
General Comments 0
You need to be logged in to leave comments. Login now