From 44e2d7ae8bb82d7a3b4ec3d22cafd44d2f8e026a Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Tue, 15 May 2018 16:55:42 +0100 Subject: [PATCH] [placement] default to accept of application/json when */* In change Ice27c7080fc2df097cb387f7438c0aaf32b4c63d the placement api was changed to set the accept header to application/json if the accept header was not set. This was done to ensure that error responses are formatted as JSON in the face of automation that webob does when formatting errors. That change was incomplete. Apparently many clients, notably requests, which backs keystoneauth1, sets a default Accept header of '*/*'. This is a reasonable default but means our error formatting default can go awry. This fixes it in the simplest way possible. See the referenced change above for discussion on why doing this with a microversion would be pointless. Tested with a simple gabbi confirmation. Change-Id: I3df5b9fb8fe7c0e41f7e1f495758973423485e11 Closes-Bug: #1771384 --- nova/api/openstack/placement/requestlog.py | 5 +++-- .../api/openstack/placement/gabbits/basic-http.yaml | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/nova/api/openstack/placement/requestlog.py b/nova/api/openstack/placement/requestlog.py index 8b3639e76754..da7be6a37f4a 100644 --- a/nova/api/openstack/placement/requestlog.py +++ b/nova/api/openstack/placement/requestlog.py @@ -36,9 +36,10 @@ class RequestLog(object): LOG.debug('Starting request: %s "%s %s"', environ['REMOTE_ADDR'], environ['REQUEST_METHOD'], self._get_uri(environ)) - # Set the accept header if it is not otherwise set. This + # Set the accept header if it is not otherwise set or is '*/*'. This # ensures that error responses will be in JSON. - if not environ.get('HTTP_ACCEPT'): + accept = environ.get('HTTP_ACCEPT') + if not accept or accept == '*/*': environ['HTTP_ACCEPT'] = 'application/json' if LOG.isEnabledFor(logging.INFO): return self._log_app(environ, start_response) diff --git a/nova/tests/functional/api/openstack/placement/gabbits/basic-http.yaml b/nova/tests/functional/api/openstack/placement/gabbits/basic-http.yaml index 162301e90141..28ee38210bac 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/basic-http.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/basic-http.yaml @@ -184,3 +184,13 @@ tests: response_forbidden_headers: - last-modified - cache-control + +- name: test starred accept and errors + GET: /resource_providers/foo + request_headers: + accept: "*/*" + status: 404 + response_headers: + content-type: application/json + response_json_paths: + $.errors[0].title: Not Found