1
# -*- coding: utf-8 -*-
3
# Authors: Alejandro J. Cura <alecu@canonical.com>
5
# Copyright 2011 Canonical Ltd.
7
# This program is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License version 3, as published
9
# by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
# PURPOSE. See the GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License along
17
# with this program. If not, see <http://www.gnu.org/licenses/>.
19
"""Integration tests for the libsoup based webservice client."""
23
from twisted.application import internet, service
24
from twisted.internet import defer
25
from twisted.web import http, resource, server
27
from ubuntuone.controlpanel.tests import TestCase
28
from ubuntuone.controlpanel.web_client.libsoup import LibsoupTimestampChecker
31
class MockResource(resource.Resource):
32
"""A mock resource."""
37
"""Initialize this mock instance."""
38
resource.Resource.__init__(self)
39
self.request_headers = []
41
# pylint: disable=C0103
42
def render_GET(self, request):
43
"""Render some content."""
44
self.request_headers.append(request.requestHeaders)
48
class MockWebService(object):
49
"""A mock webservice for testing"""
52
"""Start up this instance."""
53
self.root = MockResource()
54
site = server.Site(self.root)
55
application = service.Application('web')
56
self.service_collection = service.IServiceCollection(application)
57
#pylint: disable=E1101
58
self.tcpserver = internet.TCPServer(0, site)
59
self.tcpserver.setServiceParent(self.service_collection)
60
self.service_collection.startService()
63
"""Build the url for this mock server."""
64
#pylint: disable=W0212
65
port_num = self.tcpserver._port.getHost().port
66
return "http://localhost:%d/" % port_num
69
"""Shut down the service."""
70
#pylint: disable=E1101
71
self.service_collection.stopService()
74
class LibsoupTimestampCheckerTestCase(TestCase):
75
"""Tests for LibsoupTimestampChecker."""
79
@defer.inlineCallbacks
81
yield super(LibsoupTimestampCheckerTestCase, self).setUp()
82
self.ws = MockWebService()
83
self.addCleanup(self.ws.stop)
85
@defer.inlineCallbacks
86
def test_gets_server_date(self):
87
"""The server date is gotten right."""
89
self.patch(time, "time", lambda: fake_time)
90
checker = LibsoupTimestampChecker()
91
self.addCleanup(checker.shutdown)
92
d = checker.get_server_date_header(self.ws.get_url())
94
result_time = http.stringToDatetime(result)
95
self.assertEqual(result_time, fake_time)
97
@defer.inlineCallbacks
98
def test_server_date_sends_nocache_headers(self):
99
"""Getting the server date sends the no-cache headers."""
100
checker = LibsoupTimestampChecker()
101
self.addCleanup(checker.shutdown)
102
yield checker.get_server_date_header(self.ws.get_url())
103
self.assertEqual(len(self.ws.root.request_headers), 1)
104
headers = self.ws.root.request_headers[0]
105
result = headers.getRawHeaders("Cache-Control")
106
self.assertEqual(result, ["no-cache"])