Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Tests for the testing_helpers module.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
| 12 | from testing import testing_helpers |
| 13 | |
| 14 | |
| 15 | class TestingHelpersTest(unittest.TestCase): |
| 16 | |
| 17 | def testMakeMonorailRequest(self): |
| 18 | mr = testing_helpers.MakeMonorailRequest( |
| 19 | path='/foo?key1=2&key2=&key3') |
| 20 | |
| 21 | self.assertEqual(None, mr.GetIntParam('foo')) |
| 22 | self.assertEqual(2, mr.GetIntParam('key1')) |
| 23 | self.assertEqual(None, mr.GetIntParam('key2')) |
| 24 | self.assertEqual(None, mr.GetIntParam('key3')) |
| 25 | self.assertEqual(3, mr.GetIntParam('key2', default_value=3)) |
| 26 | self.assertEqual(3, mr.GetIntParam('foo', default_value=3)) |
| 27 | |
| 28 | def testGetRequestObjectsBasics(self): |
| 29 | request, mr = testing_helpers.GetRequestObjects( |
| 30 | path='/foo/bar/wee?sna=foo', |
| 31 | params={'ya': 'hoo'}, method='POST') |
| 32 | |
| 33 | # supplied as part of the url |
| 34 | self.assertEqual('foo', mr.GetParam('sna')) |
| 35 | |
| 36 | # supplied as a param |
| 37 | self.assertEqual('hoo', mr.GetParam('ya')) |
| 38 | |
| 39 | # default Host header |
| 40 | self.assertEqual('127.0.0.1', request.host) |
| 41 | |
| 42 | def testGetRequestObjectsHeaders(self): |
| 43 | # with some headers |
| 44 | request, _mr = testing_helpers.GetRequestObjects( |
| 45 | headers={'Accept-Language': 'en', 'Host': 'pickledsheep.com'}, |
| 46 | path='/foo/bar/wee?sna=foo') |
| 47 | |
| 48 | # default Host header |
| 49 | self.assertEqual('pickledsheep.com', request.host) |
| 50 | |
| 51 | # user specified headers |
| 52 | self.assertEqual('en', request.headers['Accept-Language']) |
| 53 | |
| 54 | def testGetRequestObjectsUserInfo(self): |
| 55 | user_id = '123' |
| 56 | |
| 57 | _request, mr = testing_helpers.GetRequestObjects( |
| 58 | user_info={'user_id': user_id}) |
| 59 | |
| 60 | self.assertEqual(user_id, mr.auth.user_id) |
| 61 | |
| 62 | |
| 63 | class BlankTest(unittest.TestCase): |
| 64 | |
| 65 | def testBlank(self): |
| 66 | blank = testing_helpers.Blank( |
| 67 | foo='foo', |
| 68 | bar=123, |
| 69 | inc=lambda x: x + 1) |
| 70 | |
| 71 | self.assertEqual('foo', blank.foo) |
| 72 | self.assertEqual(123, blank.bar) |
| 73 | self.assertEqual(5, blank.inc(4)) |