Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
fix(middleware): remove unused server side version check (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Anderson committed Aug 2, 2016
1 parent 43354be commit 6d6a10e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 52 deletions.
30 changes: 1 addition & 29 deletions rootfs/api/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,14 @@
See https://docs.djangoproject.com/en/1.6/topics/http/middleware/
"""

import json

from django.http import HttpResponse
from rest_framework import status

from api import __version__


class APIVersionMiddleware(object):
"""
Return an error if a client request is incompatible with this REST API
version, and include that REST API version with each response.
Include that REST API version with each response.
"""

def process_request(self, request):
"""
Return a 405 "Not Allowed" if the request's client major version
doesn't match this controller's REST API major version (currently "1").
"""
try:
client_version = request.META['HTTP_DEIS_VERSION']
server_version = __version__.rsplit('.', 2)[0]
if client_version != server_version:
message = {
'error': 'Client and server versions do not match. ' +
'Client version: {} '.format(client_version) +
'Server version: {}'.format(server_version)
}
return HttpResponse(
json.dumps(message),
content_type='application/json',
status=status.HTTP_405_METHOD_NOT_ALLOWED
)
except KeyError:
pass

def process_response(self, request, response):
"""
Include the controller's REST API major and minor version in
Expand Down
26 changes: 3 additions & 23 deletions rootfs/api/tests/test_api_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,9 @@ def setUp(self):

def test_deis_version_header_good(self):
"""
Test that when the version header is sent, the request is accepted.
Test that when the version header is sent.
"""
response = self.client.get(
'/v2/apps',
HTTP_DEIS_VERSION=__version__.rsplit('.', 2)[0]
)
self.assertEqual(response.status_code, 200, response.data)
response = self.client.get('/v2/apps')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.has_header('DEIS_API_VERSION'), True)
self.assertEqual(response['DEIS_API_VERSION'], __version__.rsplit('.', 1)[0])

def test_deis_version_header_bad(self):
"""
Test that when an improper version header is sent, the request is declined.
"""
response = self.client.get(
'/v2/apps',
HTTP_DEIS_VERSION='1234.5678'
)
self.assertEqual(response.status_code, 405, response.content)

def test_deis_version_header_not_present(self):
"""
Test that when the version header is not present, the request is accepted.
"""
response = self.client.get('/v2/apps')
self.assertEqual(response.status_code, 200, response.data)

0 comments on commit 6d6a10e

Please sign in to comment.