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 the testing_helpers module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from testing import testing_helpers |
| 14 | |
| 15 | |
| 16 | class TestingHelpersTest(unittest.TestCase): |
| 17 | |
| 18 | def testMakeMonorailRequest(self): |
| 19 | mr = testing_helpers.MakeMonorailRequest( |
| 20 | path='/foo?key1=2&key2=&key3') |
| 21 | |
| 22 | self.assertEqual(None, mr.GetIntParam('foo')) |
| 23 | self.assertEqual(2, mr.GetIntParam('key1')) |
| 24 | self.assertEqual(None, mr.GetIntParam('key2')) |
| 25 | self.assertEqual(None, mr.GetIntParam('key3')) |
| 26 | self.assertEqual(3, mr.GetIntParam('key2', default_value=3)) |
| 27 | self.assertEqual(3, mr.GetIntParam('foo', default_value=3)) |
| 28 | |
| 29 | def testGetRequestObjectsBasics(self): |
| 30 | request, mr = testing_helpers.GetRequestObjects( |
| 31 | path='/foo/bar/wee?sna=foo', |
| 32 | params={'ya': 'hoo'}, method='POST') |
| 33 | |
| 34 | # supplied as part of the url |
| 35 | self.assertEqual('foo', mr.GetParam('sna')) |
| 36 | |
| 37 | # supplied as a param |
| 38 | self.assertEqual('hoo', mr.GetParam('ya')) |
| 39 | |
| 40 | # default Host header |
| 41 | self.assertEqual('127.0.0.1', request.host) |
| 42 | |
| 43 | def testGetRequestObjectsHeaders(self): |
| 44 | # with some headers |
| 45 | request, _mr = testing_helpers.GetRequestObjects( |
| 46 | headers={'Accept-Language': 'en', 'Host': 'pickledsheep.com'}, |
| 47 | path='/foo/bar/wee?sna=foo') |
| 48 | |
| 49 | # default Host header |
| 50 | self.assertEqual('pickledsheep.com', request.host) |
| 51 | |
| 52 | # user specified headers |
| 53 | self.assertEqual('en', request.headers['Accept-Language']) |
| 54 | |
| 55 | def testGetRequestObjectsUserInfo(self): |
| 56 | user_id = '123' |
| 57 | |
| 58 | _request, mr = testing_helpers.GetRequestObjects( |
| 59 | user_info={'user_id': user_id}) |
| 60 | |
| 61 | self.assertEqual(user_id, mr.auth.user_id) |
| 62 | |
| 63 | |
| 64 | class BlankTest(unittest.TestCase): |
| 65 | |
| 66 | def testBlank(self): |
| 67 | blank = testing_helpers.Blank( |
| 68 | foo='foo', |
| 69 | bar=123, |
| 70 | inc=lambda x: x + 1) |
| 71 | |
| 72 | self.assertEqual('foo', blank.foo) |
| 73 | self.assertEqual(123, blank.bar) |
| 74 | self.assertEqual(5, blank.inc(4)) |