Commit cbc03cb3 authored by Giorgos Verigakis's avatar Giorgos Verigakis
Browse files

Add a Date header in responses

Make use of it in tests.
parent 922b804c
......@@ -2,6 +2,9 @@
# Copyright (c) 2010 Greek Research and Technology Network
#
from email.utils import parsedate
from time import mktime
import datetime
from django.utils import simplejson as json
......@@ -134,12 +137,10 @@ class APITestCase(TestCase):
response = self.client.get('/api/v1.1/servers/detail')
vms_from_api_initial = json.loads(response.content)['servers']['values']
then = datetime.datetime.now().isoformat().split('.')[0] + 'Z'
#isoformat also gives miliseconds that are not needed
response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % then)
ts = mktime(parsedate(response['Date']))
since = datetime.datetime.fromtimestamp(ts).isoformat() + 'Z'
response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % since)
self.assertEqual(len(response.content), 0)
#no changes were made
#now create a machine. Then check if it is on the list
request = {
......@@ -158,7 +159,7 @@ class APITestCase(TestCase):
response = self.client.post(path, json.dumps(request), content_type='application/json')
self.assertEqual(response.status_code, 202)
response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % then)
response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % since)
vms_from_api_after = json.loads(response.content)['servers']['values']
#make sure the newly created server is included on the updated list
self.assertEqual(len(vms_from_api_after), 1)
......
......@@ -6,7 +6,9 @@ from datetime import timedelta, tzinfo
from functools import wraps
from random import choice
from string import ascii_letters, digits
from time import time
from traceback import format_exc
from wsgiref.handlers import format_date_time
from django.conf import settings
from django.http import HttpResponse
......@@ -122,6 +124,16 @@ def get_request_dict(request):
raise BadRequest('Unsupported Content-Type.')
def update_response_headers(request, response):
if request.serialization == 'xml':
response['Content-Type'] = 'application/xml'
elif request.serialization == 'atom':
response['Content-Type'] = 'application/atom+xml'
else:
response['Content-Type'] = 'application/json'
response['Date'] = format_date_time(time())
def render_metadata(request, metadata, use_values=False, status=200):
if request.serialization == 'xml':
data = render_to_string('metadata.xml', {'metadata': metadata})
......@@ -148,14 +160,7 @@ def render_fault(request, fault):
data = json.dumps(d)
resp = HttpResponse(data, status=fault.code)
if request.serialization == 'xml':
resp['Content-Type'] = 'application/xml'
elif request.serialization == 'atom':
resp['Content-Type'] = 'application/atom+xml'
else:
resp['Content-Type'] = 'application/json'
update_response_headers(request, resp)
return resp
......@@ -197,13 +202,7 @@ def api_method(http_method=None, atom_allowed=False):
raise BadRequest('Method not allowed.')
resp = func(request, *args, **kwargs)
if request.serialization == 'xml':
resp['Content-Type'] = 'application/xml'
elif request.serialization == 'atom':
resp['Content-Type'] = 'application/atom+xml'
else:
resp['Content-Type'] = 'application/json'
update_response_headers(request, resp)
return resp
except Fault, fault:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment