~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to landscape/lib/tests/test_fetch.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import pycurl
 
2
 
 
3
from landscape.lib.fetch import fetch
 
4
from landscape.tests.helpers import LandscapeTest
 
5
 
 
6
 
 
7
class CurlStub(object):
 
8
 
 
9
    def __init__(self, result=None):
 
10
        self.options = {}
 
11
        self.performed = False
 
12
        self.result = result
 
13
 
 
14
    def setopt(self, option, value):
 
15
        if self.performed:
 
16
            raise AssertionError("setopt() can't be called after perform()")
 
17
        self.options[option] = value
 
18
 
 
19
    def perform(self):
 
20
        if self.performed:
 
21
            raise AssertionError("Can't perform twice")
 
22
        self.options[pycurl.WRITEFUNCTION](self.result)
 
23
        self.performed = True
 
24
 
 
25
 
 
26
class Any(object):
 
27
    def __eq__(self, other):
 
28
        return True
 
29
 
 
30
 
 
31
class FetchTest(LandscapeTest):
 
32
 
 
33
    def test_basic(self):
 
34
        curl = CurlStub("result")
 
35
        result = fetch("http://example.com", curl=curl)
 
36
        self.assertEquals(result, "result")
 
37
        self.assertEquals(curl.options,
 
38
                          {pycurl.URL: "http://example.com",
 
39
                           pycurl.FOLLOWLOCATION: True,
 
40
                           pycurl.MAXREDIRS: 5,
 
41
                           pycurl.WRITEFUNCTION: Any()})
 
42
 
 
43
    def test_post(self):
 
44
        curl = CurlStub("result")
 
45
        result = fetch("http://example.com", post=True, curl=curl)
 
46
        self.assertEquals(result, "result")
 
47
        self.assertEquals(curl.options,
 
48
                          {pycurl.URL: "http://example.com",
 
49
                           pycurl.FOLLOWLOCATION: True,
 
50
                           pycurl.MAXREDIRS: 5,
 
51
                           pycurl.WRITEFUNCTION: Any(),
 
52
                           pycurl.POST: True})
 
53
 
 
54
    def test_post_data(self):
 
55
        curl = CurlStub("result")
 
56
        result = fetch("http://example.com", post=True, data="data", curl=curl)
 
57
        self.assertEquals(result, "result")
 
58
        self.assertEquals(curl.options[pycurl.READFUNCTION](), "data")
 
59
        self.assertEquals(curl.options,
 
60
                          {pycurl.URL: "http://example.com",
 
61
                           pycurl.FOLLOWLOCATION: True,
 
62
                           pycurl.MAXREDIRS: 5,
 
63
                           pycurl.WRITEFUNCTION: Any(),
 
64
                           pycurl.POST: True,
 
65
                           pycurl.POSTFIELDSIZE: 4,
 
66
                           pycurl.READFUNCTION: Any()})
 
67
 
 
68
    def test_cainfo(self):
 
69
        curl = CurlStub("result")
 
70
        result = fetch("https://example.com", cainfo="cainfo", curl=curl)
 
71
        self.assertEquals(result, "result")
 
72
        self.assertEquals(curl.options,
 
73
                          {pycurl.URL: "https://example.com",
 
74
                           pycurl.FOLLOWLOCATION: True,
 
75
                           pycurl.MAXREDIRS: 5,
 
76
                           pycurl.WRITEFUNCTION: Any(),
 
77
                           pycurl.CAINFO: "cainfo"})
 
78
 
 
79
    def test_cainfo_on_http(self):
 
80
        curl = CurlStub("result")
 
81
        result = fetch("http://example.com", cainfo="cainfo", curl=curl)
 
82
        self.assertEquals(result, "result")
 
83
        self.assertTrue(pycurl.CAINFO not in curl.options)
 
84
 
 
85
    def test_headers(self):
 
86
        curl = CurlStub("result")
 
87
        result = fetch("http://example.com", headers={"a":"1", "b":"2"},
 
88
                       curl=curl)
 
89
        self.assertEquals(result, "result")
 
90
        self.assertEquals(curl.options,
 
91
                          {pycurl.URL: "http://example.com",
 
92
                           pycurl.FOLLOWLOCATION: True,
 
93
                           pycurl.MAXREDIRS: 5,
 
94
                           pycurl.WRITEFUNCTION: Any(),
 
95
                           pycurl.HTTPHEADER: ["a: 1", "b: 2"]})
 
96
 
 
97
    def test_create_curl(self):
 
98
        curls = []
 
99
        def pycurl_Curl():
 
100
            curl = CurlStub("result")
 
101
            curls.append(curl)
 
102
            return curl
 
103
        Curl = pycurl.Curl
 
104
        try:
 
105
            pycurl.Curl = pycurl_Curl
 
106
            result = fetch("http://example.com")
 
107
            curl = curls[0]
 
108
            self.assertEquals(result, "result")
 
109
            self.assertEquals(curl.options,
 
110
                              {pycurl.URL: "http://example.com",
 
111
                               pycurl.FOLLOWLOCATION: True,
 
112
                               pycurl.MAXREDIRS: 5,
 
113
                               pycurl.WRITEFUNCTION: Any()})
 
114
        finally:
 
115
            pycurl.Curl = Curl