blob: 6e62f4e500d88f5fda345c1f685f46a85193d9d9 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Tests for the testing_helpers module."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
12from testing import testing_helpers
13
14
15class 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
63class 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))