~mandel/ubuntu-sso-client/libsoup-ssl-dialog

« back to all changes in this revision

Viewing changes to ubuntu_sso/utils/webclient/tests/test_webclient.py

  • Committer: Tarmac
  • Author(s): Alejandro J. Cura
  • Date: 2012-03-14 19:43:10 UTC
  • mfrom: (846.54.6 updated-txweb)
  • mto: This revision was merged to the branch mainline in revision 949.
  • Revision ID: tarmac-20120314194310-tudnz9qdwa30fp7a
- Use HTTPClientFactory to allow replacing the reactor used to connect (LP: #929207).

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
)
39
39
from ubuntu_sso.utils import webclient
40
40
from ubuntu_sso.utils.ui import SSL_DETAILS_TEMPLATE
41
 
from ubuntu_sso.utils.webclient import gsettings
 
41
from ubuntu_sso.utils.webclient import gsettings, txweb
42
42
from ubuntu_sso.utils.webclient.common import BaseWebClient, HeaderDict, oauth
43
43
from ubuntu_sso.utils.webclient.tests import BaseMockWebServer
44
44
 
241
241
    """Test for the webclient."""
242
242
 
243
243
    timeout = 1
 
244
    webclient_factory = webclient.webclient_factory
244
245
 
245
246
    @defer.inlineCallbacks
246
247
    def setUp(self):
248
249
        self.ws = MockWebServer()
249
250
        self.addCleanup(self.ws.stop)
250
251
        self.base_iri = self.ws.get_iri()
251
 
        self.wc = webclient.webclient_factory()
 
252
        self.wc = self.webclient_factory()
252
253
        self.addCleanup(self.wc.shutdown)
253
254
 
254
255
    @defer.inlineCallbacks
359
360
                        "The type of %r must be bytes" % result.content)
360
361
 
361
362
 
 
363
class FakeSavingReactor(object):
 
364
    """A fake reactor that saves connection attempts."""
 
365
 
 
366
    def __init__(self):
 
367
        """Initialize this fake instance."""
 
368
        self.connections = []
 
369
 
 
370
    def connectTCP(self, host, port, factory, *args):
 
371
        """Fake the connection."""
 
372
        self.connections.append((host, port, args))
 
373
        factory.response_headers = {}
 
374
        factory.deferred = defer.succeed("response content")
 
375
 
 
376
    def connectSSL(self, host, port, factory, *args):
 
377
        """Fake the connection."""
 
378
        self.connections.append((host, port, args))
 
379
        factory.response_headers = {}
 
380
        factory.deferred = defer.succeed("response content")
 
381
 
 
382
 
 
383
class TxWebClientTestCase(WebClientTestCase):
 
384
    """Test case for txweb."""
 
385
 
 
386
    webclient_factory = txweb.WebClient
 
387
 
 
388
 
 
389
class TxWebClientReactorReplaceableTestCase(TestCase):
 
390
    """In the txweb client the reactor is replaceable."""
 
391
 
 
392
    timeout = 3
 
393
    FAKE_HOST = u"fake"
 
394
    FAKE_IRI_TEMPLATE = u"%%s://%s/fake_page" % FAKE_HOST
 
395
 
 
396
    @defer.inlineCallbacks
 
397
    def _test_replaceable_reactor(self, iri):
 
398
        """The reactor can be replaced with the tunnel client."""
 
399
        fake_reactor = FakeSavingReactor()
 
400
        wc = txweb.WebClient(fake_reactor)
 
401
        _response = yield wc.request(iri)
 
402
        host, _port, _args = fake_reactor.connections[0]
 
403
        self.assertEqual(host, self.FAKE_HOST)
 
404
 
 
405
    def test_replaceable_reactor_http(self):
 
406
        """Test the replaceable reactor with an http iri."""
 
407
        return self._test_replaceable_reactor(self.FAKE_IRI_TEMPLATE % "http")
 
408
 
 
409
    def test_replaceable_reactor_https(self):
 
410
        """Test the replaceable reactor with an https iri."""
 
411
        return self._test_replaceable_reactor(self.FAKE_IRI_TEMPLATE % "https")
 
412
 
 
413
 
362
414
class TimestampCheckerTestCase(TestCase):
363
415
    """Tests for the timestampchecker classmethod."""
364
416