Improve Accept and Content-Type handling

Originally, if WSME received an Accept or Content-Type header that
was not aligned with what it was prepared to handle it would error
out with a 500 status code. This is not good behavior for a web
service.

In the process of trying to fix this it was discovered that the
content-negotiation code within WSME (the code that, in part,
looks for a suitable protocol handler for a request) and tests of
that code are incorrect, violating expected HTTP behaviors. GET
requests are passing Content-Type headers to declare the desired
type of representation in the response. This is what Accept is for.

Unfortunately the server-side code was perfectly willing to accept
this behavior. These changes correct that.

Closes-Bug: 1419110
Change-Id: I2b5c0075611490c047b27b1b43b0505fc5534b3b
This commit is contained in:
Chris Dent
2015-02-09 14:52:07 +00:00
parent bad1c3edfb
commit 8710dabb65
7 changed files with 140 additions and 16 deletions

View File

@@ -136,12 +136,26 @@ class TestCRUDController():
DBSession.flush()
pid = p.id
r = self.app.get('/person?ref.id=%s' % pid,
headers={'Content-Type': 'application/json'})
headers={'Accept': 'application/json'})
r = json.loads(r.text)
print(r)
assert r['name'] == u('Pierre-Joseph')
assert r['birthdate'] == u('1809-01-15')
def test_GET_bad_accept(self):
p = DBPerson(
name=u('Pierre-Joseph'),
birthdate=datetime.date(1809, 1, 15))
DBSession.add(p)
DBSession.flush()
pid = p.id
r = self.app.get('/person?ref.id=%s' % pid,
headers={'Accept': 'text/plain'},
status=406)
assert r.text == ("Unacceptable Accept type: text/plain not in "
"['application/json', 'text/javascript', "
"'application/javascript', 'text/xml']")
def test_update(self):
p = DBPerson(
name=u('Pierre-Joseph'),