3
from landscape.lib.fetch import fetch
4
from landscape.tests.helpers import LandscapeTest
7
class CurlStub(object):
9
def __init__(self, result=None):
11
self.performed = False
14
def setopt(self, option, value):
16
raise AssertionError("setopt() can't be called after perform()")
17
self.options[option] = value
21
raise AssertionError("Can't perform twice")
22
self.options[pycurl.WRITEFUNCTION](self.result)
27
def __eq__(self, other):
31
class FetchTest(LandscapeTest):
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,
41
pycurl.WRITEFUNCTION: Any()})
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,
51
pycurl.WRITEFUNCTION: Any(),
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,
63
pycurl.WRITEFUNCTION: Any(),
65
pycurl.POSTFIELDSIZE: 4,
66
pycurl.READFUNCTION: Any()})
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,
76
pycurl.WRITEFUNCTION: Any(),
77
pycurl.CAINFO: "cainfo"})
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)
85
def test_headers(self):
86
curl = CurlStub("result")
87
result = fetch("http://example.com", headers={"a":"1", "b":"2"},
89
self.assertEquals(result, "result")
90
self.assertEquals(curl.options,
91
{pycurl.URL: "http://example.com",
92
pycurl.FOLLOWLOCATION: True,
94
pycurl.WRITEFUNCTION: Any(),
95
pycurl.HTTPHEADER: ["a: 1", "b: 2"]})
97
def test_create_curl(self):
100
curl = CurlStub("result")
105
pycurl.Curl = pycurl_Curl
106
result = fetch("http://example.com")
108
self.assertEquals(result, "result")
109
self.assertEquals(curl.options,
110
{pycurl.URL: "http://example.com",
111
pycurl.FOLLOWLOCATION: True,
113
pycurl.WRITEFUNCTION: Any()})