~ralsina/ubuntuone-control-panel/opt-parsing

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/web_client/tests/test_libsoup.py

  • Committer: Tarmac
  • Author(s): Natalia B. Bidart
  • Date: 2012-02-08 16:34:09 UTC
  • mfrom: (255.2.10 use-webclient)
  • Revision ID: tarmac-20120208163409-2rkfpahrubzzpyxj
 - Replaced custom webclient with the one from ubuntu-sso-client
   (LP: #926311).
- Removed the dependency on qt4reactor for Linux implementation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
 
3
 
# Authors: Alejandro J. Cura <alecu@canonical.com>
4
 
#
5
 
# Copyright 2011 Canonical Ltd.
6
 
#
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.
10
 
#
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.
15
 
#
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/>.
18
 
 
19
 
"""Integration tests for the libsoup based webservice client."""
20
 
 
21
 
import time
22
 
 
23
 
from twisted.application import internet, service
24
 
from twisted.internet import defer
25
 
from twisted.web import http, resource, server
26
 
 
27
 
from ubuntuone.controlpanel.tests import TestCase
28
 
from ubuntuone.controlpanel.web_client.libsoup import LibsoupTimestampChecker
29
 
 
30
 
 
31
 
class MockResource(resource.Resource):
32
 
    """A mock resource."""
33
 
 
34
 
    isLeaf = True
35
 
 
36
 
    def __init__(self):
37
 
        """Initialize this mock instance."""
38
 
        resource.Resource.__init__(self)
39
 
        self.request_headers = []
40
 
 
41
 
    # pylint: disable=C0103
42
 
    def render_GET(self, request):
43
 
        """Render some content."""
44
 
        self.request_headers.append(request.requestHeaders)
45
 
        return "hello!"
46
 
 
47
 
 
48
 
class MockWebService(object):
49
 
    """A mock webservice for testing"""
50
 
 
51
 
    def __init__(self):
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()
61
 
 
62
 
    def get_url(self):
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
67
 
 
68
 
    def stop(self):
69
 
        """Shut down the service."""
70
 
        #pylint: disable=E1101
71
 
        self.service_collection.stopService()
72
 
 
73
 
 
74
 
class LibsoupTimestampCheckerTestCase(TestCase):
75
 
    """Tests for LibsoupTimestampChecker."""
76
 
 
77
 
    timeout = 3
78
 
 
79
 
    @defer.inlineCallbacks
80
 
    def setUp(self):
81
 
        yield super(LibsoupTimestampCheckerTestCase, self).setUp()
82
 
        self.ws = MockWebService()
83
 
        self.addCleanup(self.ws.stop)
84
 
 
85
 
    @defer.inlineCallbacks
86
 
    def test_gets_server_date(self):
87
 
        """The server date is gotten right."""
88
 
        fake_time = 1
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())
93
 
        result = yield d
94
 
        result_time = http.stringToDatetime(result)
95
 
        self.assertEqual(result_time, fake_time)
96
 
 
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"])