Skip to content

Commit 385fc8d

Browse files
committed
[runtests] Created custom assertRequestRedirect test method
This cuts down on repetition and will help migrate away from urllib2 in a future commit.
1 parent d83a2f6 commit 385fc8d

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

runtests.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -317,30 +317,34 @@ def openGitHubHook(n=1, reponame='', payload=None):
317317

318318

319319
class GitHubBrowserTests(TracGitHubTests):
320-
321-
def testLinkToChangeset(self):
322-
self.makeGitCommit(GIT, 'myfile', 'for browser tests')
323-
changeset = self.openGitHubHook().read().rstrip()[-40:]
320+
def assertRequestRedirects(self, url, redirection, status=302):
321+
"""
322+
A custom assertion to check that requesting the given url (GET) returns a redirection
323+
response (302 by default) pointed to the given redirection URL.
324+
"""
324325
try:
325-
urllib2.urlopen(u('changeset', changeset))
326+
urllib2.urlopen(url)
326327
except urllib2.HTTPError as exc:
327-
self.assertEqual(exc.code, 302)
328-
self.assertEqual(exc.headers['Location'],
329-
'https://github.com/aaugustin/trac-github/commit/%s' % changeset)
328+
self.assertEqual(exc.code, status)
329+
self.assertEqual(exc.headers['Location'], redirection)
330330
else:
331331
self.fail("URL didn't redirect")
332332

333+
def testLinkToChangeset(self):
334+
self.makeGitCommit(GIT, 'myfile', 'for browser tests')
335+
changeset = self.openGitHubHook().rstrip()[-40:]
336+
self.assertRequestRedirects(
337+
u('changeset', changeset),
338+
'https://github.com/aaugustin/trac-github/commit/%s' % changeset
339+
)
340+
333341
def testAlternateLinkToChangeset(self):
334342
self.makeGitCommit(ALTGIT, 'myfile', 'for browser tests')
335-
changeset = self.openGitHubHook(1, 'alt').read().rstrip()[-40:]
336-
try:
337-
urllib2.urlopen(u('changeset', changeset, 'alt'))
338-
except urllib2.HTTPError as exc:
339-
self.assertEqual(exc.code, 302)
340-
self.assertEqual(exc.headers['Location'],
341-
'https://github.com/follower/trac-github/commit/%s' % changeset)
342-
else:
343-
self.fail("URL didn't redirect")
343+
changeset = self.openGitHubHook(1, 'alt').rstrip()[-40:]
344+
self.assertRequestRedirects(
345+
u('changeset', changeset, 'alt'),
346+
'https://github.com/follower/trac-github/commit/%s' % changeset
347+
)
344348

345349
def testNonGitHubLinkToChangeset(self):
346350
changeset = self.makeGitCommit(NOGHGIT, 'myfile', 'for browser tests')
@@ -350,27 +354,19 @@ def testNonGitHubLinkToChangeset(self):
350354

351355
def testLinkToPath(self):
352356
self.makeGitCommit(GIT, 'myfile', 'for more browser tests')
353-
changeset = self.openGitHubHook().read().rstrip()[-40:]
354-
try:
355-
urllib2.urlopen(u('changeset', changeset, 'myfile'))
356-
except urllib2.HTTPError as exc:
357-
self.assertEqual(exc.code, 302)
358-
self.assertEqual(exc.headers['Location'],
359-
'https://github.com/aaugustin/trac-github/blob/%s/myfile' % changeset)
360-
else:
361-
self.fail("URL didn't redirect")
357+
changeset = self.openGitHubHook().rstrip()[-40:]
358+
self.assertRequestRedirects(
359+
u('changeset', changeset, 'myfile'),
360+
'https://github.com/aaugustin/trac-github/blob/%s/myfile' % changeset
361+
)
362362

363363
def testAlternateLinkToPath(self):
364364
self.makeGitCommit(ALTGIT, 'myfile', 'for more browser tests')
365-
changeset = self.openGitHubHook(1, 'alt').read().rstrip()[-40:]
366-
try:
367-
urllib2.urlopen(u('changeset', changeset, 'alt/myfile'))
368-
except urllib2.HTTPError as exc:
369-
self.assertEqual(exc.code, 302)
370-
self.assertEqual(exc.headers['Location'],
371-
'https://github.com/follower/trac-github/blob/%s/myfile' % changeset)
372-
else:
373-
self.fail("URL didn't redirect")
365+
changeset = self.openGitHubHook(1, 'alt').rstrip()[-40:]
366+
self.assertRequestRedirects(
367+
u('changeset', changeset, 'alt/myfile'),
368+
'https://github.com/follower/trac-github/blob/%s/myfile' % changeset
369+
)
374370

375371
def testNonGitHubLinkToPath(self):
376372
changeset = self.makeGitCommit(NOGHGIT, 'myfile', 'for more browser tests')

0 commit comments

Comments
 (0)