~lutostag/ubuntu/trusty/maas/1.5.4+keystone

« back to all changes in this revision

Viewing changes to src/apiclient/tests/test_maas_client.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-03-04 11:49:44 UTC
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: package-import@ubuntu.com-20130304114944-azcvu9anlf8mizpa
Tags: upstream-1.3+bzr1452+dfsg
ImportĀ upstreamĀ versionĀ 1.3+bzr1452+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
__metaclass__ = type
13
13
__all__ = []
14
14
 
 
15
import gzip
 
16
from io import BytesIO
15
17
import json
16
18
from random import randint
 
19
import urllib2
17
20
from urlparse import (
18
21
    parse_qs,
19
22
    urljoin,
29
32
    parse_headers_and_body_with_django,
30
33
    parse_headers_and_body_with_mimer,
31
34
    )
 
35
from maastesting.fixtures import TempWDFixture
32
36
from maastesting.factory import factory
 
37
from maastesting.httpd import HTTPServerFixture
33
38
from maastesting.testcase import TestCase
34
39
from testtools.matchers import (
35
40
    AfterPreprocessing,
55
60
        self.assertEqual(
56
61
            contents, MAASDispatcher().dispatch_query(url, {}).read())
57
62
 
 
63
    def test_request_from_http(self):
 
64
        # We can't just call self.make_file because HTTPServerFixture will only
 
65
        # serve content from the current WD. And we don't want to create random
 
66
        # content in the original WD.
 
67
        self.useFixture(TempWDFixture())
 
68
        name = factory.getRandomString()
 
69
        content = factory.getRandomString().encode('ascii')
 
70
        factory.make_file(location='.', name=name, contents=content)
 
71
        with HTTPServerFixture() as httpd:
 
72
            url = urljoin(httpd.url, name)
 
73
            response = MAASDispatcher().dispatch_query(url, {})
 
74
            self.assertEqual(200, response.code)
 
75
            self.assertEqual(content, response.read())
 
76
 
 
77
    def test_supports_content_encoding_gzip(self):
 
78
        # The client will set the Accept-Encoding: gzip header, and it will
 
79
        # also decompress the response if it comes back with Content-Encoding:
 
80
        # gzip.
 
81
        self.useFixture(TempWDFixture())
 
82
        name = factory.getRandomString()
 
83
        content = factory.getRandomString(300).encode('ascii')
 
84
        factory.make_file(location='.', name=name, contents=content)
 
85
        called = []
 
86
        orig_urllib = urllib2.urlopen
 
87
 
 
88
        def logging_urlopen(*args, **kwargs):
 
89
            called.append((args, kwargs))
 
90
            return orig_urllib(*args, **kwargs)
 
91
        self.patch(urllib2, 'urlopen', logging_urlopen)
 
92
        with HTTPServerFixture() as httpd:
 
93
            url = urljoin(httpd.url, name)
 
94
            res = MAASDispatcher().dispatch_query(url, {})
 
95
            self.assertEqual(200, res.code)
 
96
            self.assertEqual(content, res.read())
 
97
        request = called[0][0][0]
 
98
        self.assertEqual([((request,), {})], called)
 
99
        self.assertEqual('gzip', request.headers.get('Accept-encoding'))
 
100
 
 
101
    def test_doesnt_override_accept_encoding_headers(self):
 
102
        # If someone passes their own Accept-Encoding header, then dispatch
 
103
        # just passes it through.
 
104
        self.useFixture(TempWDFixture())
 
105
        name = factory.getRandomString()
 
106
        content = factory.getRandomString(300).encode('ascii')
 
107
        factory.make_file(location='.', name=name, contents=content)
 
108
        with HTTPServerFixture() as httpd:
 
109
            url = urljoin(httpd.url, name)
 
110
            headers = {'Accept-encoding': 'gzip'}
 
111
            res = MAASDispatcher().dispatch_query(url, headers)
 
112
            self.assertEqual(200, res.code)
 
113
            self.assertEqual('gzip', res.info().get('Content-Encoding'))
 
114
            raw_content = res.read()
 
115
        read_content = gzip.GzipFile(
 
116
            mode='rb', fileobj=BytesIO(raw_content)).read()
 
117
        self.assertEqual(content, read_content)
 
118
 
58
119
 
59
120
def make_url():
60
121
    """Create an arbitrary URL."""