Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """Tests for URL handler registration helper functions.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | import webapp2 |
| 14 | |
| 15 | from framework import registerpages_helpers |
| 16 | |
| 17 | |
| 18 | class SendRedirectInScopeTest(unittest.TestCase): |
| 19 | |
| 20 | def testMakeRedirectInScope_Error(self): |
| 21 | self.assertRaises( |
| 22 | AssertionError, |
| 23 | registerpages_helpers.MakeRedirectInScope, 'no/initial/slash', 'p') |
| 24 | self.assertRaises( |
| 25 | AssertionError, |
| 26 | registerpages_helpers.MakeRedirectInScope, '', 'p') |
| 27 | |
| 28 | def testMakeRedirectInScope_Normal(self): |
| 29 | factory = registerpages_helpers.MakeRedirectInScope('/', 'p') |
| 30 | # Non-dasher, normal case |
| 31 | request = webapp2.Request.blank( |
| 32 | path='/p/foo', headers={'Host': 'example.com'}) |
| 33 | response = webapp2.Response() |
| 34 | redirector = factory(request, response) |
| 35 | redirector.get() |
| 36 | self.assertEqual(response.location, '//example.com/p/foo/') |
| 37 | self.assertEqual(response.status, '301 Moved Permanently') |
| 38 | |
| 39 | def testMakeRedirectInScope_Temporary(self): |
| 40 | factory = registerpages_helpers.MakeRedirectInScope( |
| 41 | '/', 'p', permanent=False) |
| 42 | request = webapp2.Request.blank( |
| 43 | path='/p/foo', headers={'Host': 'example.com'}) |
| 44 | response = webapp2.Response() |
| 45 | redirector = factory(request, response) |
| 46 | redirector.get() |
| 47 | self.assertEqual(response.location, '//example.com/p/foo/') |
| 48 | self.assertEqual(response.status, '302 Moved Temporarily') |
| 49 | |
| 50 | def testMakeRedirectInScope_KeepQueryString(self): |
| 51 | factory = registerpages_helpers.MakeRedirectInScope( |
| 52 | '/', 'p', keep_qs=True) |
| 53 | request = webapp2.Request.blank( |
| 54 | path='/p/foo?q=1', headers={'Host': 'example.com'}) |
| 55 | response = webapp2.Response() |
| 56 | redirector = factory(request, response) |
| 57 | redirector.get() |
| 58 | self.assertEqual(response.location, '//example.com/p/foo/?q=1') |
| 59 | self.assertEqual(response.status, '302 Moved Temporarily') |