-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_mock.py
100 lines (76 loc) · 3.62 KB
/
test_mock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2024 UW-IT, University of Washington
# SPDX-License-Identifier: Apache-2.0
from unittest import TestCase, skipIf
from restclients_core.dao import DAO
from os.path import abspath, dirname
from restclients_core.dao import MockDAO
from restclients_core.exceptions import DataFailureException
class TDAO(DAO):
def service_name(self):
return 'testing'
def service_mock_paths(self):
# tests/dao_implementation/resources/
return [abspath(dirname(__file__) + "/resources/")]
class TestMock(TestCase):
def test_found_resource(self):
response = TDAO().getURL('/found.json', {})
self.assertEquals(response.status, 200)
self.assertEquals(response.read(), b'{"OK": true }\n')
def test_missing_resource(self):
response = TDAO().getURL('/missing.json', {})
self.assertEquals(response.status, 404)
def test_file_headers(self):
response = TDAO().getURL('/with_headers.json', {})
self.assertEquals(response.status, 202)
self.assertEquals(response.headers["Custom"], "My Custom Value")
self.assertEquals(response.getheader("Custom"), "My Custom Value")
def test_plain_file_headers(self):
response = TDAO().getURL('/with_only_headers.json', {})
self.assertEquals(response.status, 200)
self.assertEquals(response.headers["Custom2"], "My Custom Value 2")
def test_registered_paths(self):
response = TDAO().getURL('/override.json', {})
self.assertEquals(response.status, 200)
self.assertEquals(response.read(), b'{"override": false }\n')
override = abspath(dirname(__file__) + "/resource_override/")
MockDAO.register_mock_path(override)
response = TDAO().getURL('/override.json', {})
self.assertEquals(response.status, 200)
self.assertEquals(response.read(), b'{"override": true }\n')
def test_binary_data(self):
response = TDAO().getURL('/image.jpg', {})
self.assertEquals(response.status, 200)
def test_params_none(self):
response = TDAO().getURL('/search?'
'first=a&second=b.POST')
self.assertEquals(response.status, 200)
def test_out_of_order_params(self):
response = TDAO().getURL('/search?'
'first=a&second=b&third=c&fourth=d')
self.assertEquals(response.status, 200)
def test_extra_params(self):
response = TDAO().getURL('/search?'
'first=a&second=b&third=c&fourth=d&fifth=e')
self.assertEquals(response.status, 404)
def test_quoted_params(self):
response = TDAO().getURL('/search?'
'first=a&second=a%3Ab%3Ac')
self.assertEquals(response.status, 200)
def test_no_body_file(self):
response = TDAO().getURL('/search?'
'first=abc')
self.assertEquals(response.status, 200)
def test_multiple_files(self):
with self.assertRaises(DataFailureException):
TDAO().getURL('/search?'
'first=a&second=b')
def test_quote_in_file_path(self):
response = TDAO().getURL('/test%3folder/test.json')
self.assertEquals(response.status, 200)
def test_quote_in_file_path_and_file(self):
response = TDAO().getURL('/test%3folder/test.json')
self.assertEquals(response.status, 200)
@skipIf(True, "I'm not sure that this case actually exists - ezturner")
def test_quote_in_file_path_and_url_but_not_file(self):
response = TDAO().getURL('/test%3folder/test%3man.json')
self.assertEquals(response.status, 200)