commit 8b01f7f3f7b0ecc5d25002b569db81afa4c953eb
Author: Giorgos Verigakis <verigak@gmail.com>
Date:   Thu Apr 7 13:09:53 2011 +0300

    Add a Date header in responses
    
    Make use of it in tests.

diff --git a/api/tests.py b/api/tests.py
index 118fb64..0740dad 100644
--- a/api/tests.py
+++ b/api/tests.py
@@ -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)
diff --git a/api/util.py b/api/util.py
index b82aaa3..6cae11c 100644
--- a/api/util.py
+++ b/api/util.py
@@ -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:
