forked from tarttelin/Python-Stub-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
118 lines (100 loc) · 4.56 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from stubserver import StubServer,FTPStubServer
from ftplib import FTP
import unittest, urllib, urllib2
from unittest import TestCase
from StringIO import StringIO
class WebTest(object):
def setUp(self):
self.server = StubServer(8998)
self.server.run()
def tearDown(self):
self.server.stop()
self.server.verify()
def _make_request(self, url, method="GET", payload="", headers={}):
self.opener = urllib2.OpenerDirector()
self.opener.add_handler(urllib2.HTTPHandler())
request = urllib2.Request(url, headers=headers, data=payload)
request.get_method = lambda: method
response = self.opener.open(request)
response_code = getattr(response, 'code', -1)
return (response, response_code)
def test_get_with_file_call(self):
f = open('data.txt', 'w')
f.write("test file")
f.close()
self.server.expect(method="GET", url="/address/\d+$").and_return(mime_type="text/xml", file_content="./data.txt")
response, response_code = self._make_request("http://localhost:8998/address/25", method="GET")
expected = open("./data.txt", "r").read()
try:
self.assertEquals(expected, response.read())
finally:
response.close()
def test_put_with_capture(self):
capture = {}
self.server.expect(method="PUT", url="/address/\d+$", data_capture=capture).and_return(reply_code=201)
f, reply_code = self._make_request("http://localhost:8998/address/45", method="PUT", payload=str({"hello": "world", "hi": "mum"}))
try:
self.assertEquals("", f.read())
captured = eval(capture["body"])
self.assertEquals("world", captured["hello"])
self.assertEquals("mum", captured["hi"])
self.assertEquals(201, reply_code)
finally:
f.close()
def test_post_with_data_and_no_body_response(self):
self.server.expect(method="POST", url="address/\d+/inhabitant", data='<inhabitant name="Chris"/>').and_return(reply_code=204)
f, reply_code = self._make_request("http://localhost:8998/address/45/inhabitant", method="POST", payload='<inhabitant name="Chris"/>')
self.assertEquals(204, reply_code)
def test_get_with_data(self):
self.server.expect(method="GET", url="/monitor/server_status$").and_return(content="<html><body>Server is up</body></html>", mime_type="text/html")
f, reply_code = self._make_request("http://localhost:8998/monitor/server_status", method="GET")
try:
self.assertTrue("Server is up" in f.read())
self.assertEquals(200, reply_code)
finally:
f.close()
class FTPTest(TestCase):
def setUp(self):
self.port = 6666
self.server = FTPStubServer(self.port)
self.server.run()
def tearDown(self):
self.server.stop()
def test_put_test_file(self):
self.assertFalse(self.server.files("foo.txt"))
ftp = FTP()
ftp.set_debuglevel(0)
ftp.connect('localhost', self.port)
ftp.login('user1', 'passwd')
ftp.storlines('STOR foo.txt', StringIO('cant believe its not bitter'))
ftp.quit()
ftp.close()
self.assertTrue(self.server.files("foo.txt"))
def test_put_2_files_associates_the_correct_content_with_the_correct_filename(self):
ftp = FTP()
ftp.connect('localhost', self.port)
ftp.set_debuglevel(0)
ftp.login('user2','other_pass')
ftp.storlines('STOR robot.txt', StringIO("file1 content"*1024))
ftp.storlines('STOR monster.txt', StringIO("file2 content"))
ftp.quit()
ftp.close()
self.assertEquals("file1 content"*1024, self.server.files("robot.txt").strip())
self.assertEquals("file2 content", self.server.files("monster.txt").strip())
def test_retrieve_expected_file_returns_file(self):
expected_content = 'content of my file\nis a complete mystery to me.'
self.server.add_file('foo.txt', expected_content)
ftp = FTP()
ftp.set_debuglevel(2)
ftp.connect('localhost', self.port)
ftp.login('chris', 'tarttelin')
directory_content = []
ftp.retrlines('LIST', lambda x: directory_content.append(x))
file_content = []
ftp.retrlines('RETR foo.txt', lambda x: file_content.append(x))
ftp.quit()
ftp.close()
self.assertTrue('foo.txt' in '\n'.join(directory_content))
self.assertEquals(expected_content, '\n'.join(file_content))
if __name__=='__main__':
unittest.main()