Skip to content

Commit 3b0e71d

Browse files
committed
fix request.content_type if WebOb<1.7.4
1 parent 84ffed9 commit 3b0e71d

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

watcher/common.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,14 @@ def is_content_json(req):
163163
:param req: the request
164164
:return: bool
165165
"""
166-
return req.content_type == 'application/json' \
167-
and int(req.content_length) > 0
166+
content_type = ''
167+
try:
168+
content_type = req.content_type
169+
except AttributeError:
170+
content_type = req.environ.get(['CONTENT_TYPE'], '')
171+
finally:
172+
return 'application/json' in content_type \
173+
and int(req.content_length) > 0
168174

169175

170176
def is_uid_string(string):

watcher/tests/test_common.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
import six
1616
import unittest
17-
18-
from pycadf import cadftaxonomy as taxonomy
17+
import webob
1918

2019
import watcher.common as common
2120

21+
from . import fake
2222

2323
class TestCommon(unittest.TestCase):
2424
def test_is_version_string(self):
@@ -82,6 +82,42 @@ def test_get_project_id_from_path(self):
8282
stim.get('help')
8383
)
8484

85+
def test_is_content_json(self):
86+
stimuli = [
87+
{
88+
'request': fake.create_request(
89+
path='/v2.1/os-aggregates/0123456789abcdef0123456789abcdef/action',
90+
method='POST',
91+
body_dict={"add_host": {"host": "21549b2f665945baaa7101926a00143c"}},
92+
),
93+
'expected': True
94+
},
95+
{
96+
'request': fake.create_request(
97+
path='/v2.1/os-aggregates/0123456789abcdef0123456789abcdef/action'
98+
),
99+
'expected': False
100+
},
101+
{
102+
'request': webob.Request.blank(
103+
path='/v1/foobar'
104+
),
105+
'expected': False
106+
}
107+
]
108+
109+
for s in stimuli:
110+
req = s.get('request')
111+
expected = s.get('expected')
112+
actual = common.is_content_json(req)
113+
114+
self.assertEqual(
115+
actual,
116+
expected,
117+
"should be '{0}' but got '{1}'".format(expected, actual)
118+
)
119+
120+
85121

86122
if __name__ == '__main__':
87123
unittest.main()

0 commit comments

Comments
 (0)