-
Notifications
You must be signed in to change notification settings - Fork 41
/
test_mocket.py
85 lines (69 loc) · 2.67 KB
/
test_mocket.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
from __future__ import unicode_literals
import socket
from unittest import TestCase
from mocket.mocket import Mocket, mocketize, MocketEntry
import redis
class MocketTestCase(TestCase):
def setUp(self):
Mocket.reset()
def test_lastrequest(self):
self.assertEqual(Mocket.last_request(), None)
Mocket._requests.extend([1, 2, 3])
self.assertEqual(Mocket.last_request(), 3)
def test_reset(self):
Mocket._requests.extend([1, 2, 3])
self.assertEqual(Mocket._requests, [1, 2, 3])
Mocket.reset()
self.assertEqual(Mocket._requests, [])
def test_gethostname(self):
hostname = socket.gethostname()
Mocket.enable()
self.assertEqual(socket.gethostname(), 'localhost')
Mocket.disable()
self.assertEqual(socket.gethostname(), hostname)
def test_gethostbyname(self):
host = socket.gethostbyname('localhost')
Mocket.enable()
self.assertEqual(socket.gethostbyname('localhost'), '127.0.0.1')
Mocket.disable()
self.assertEqual(socket.gethostbyname('localhost'), host)
def test_register(self):
entry_1 = MocketEntry(('localhost', 80), True)
entry_2 = MocketEntry(('localhost', 80), True)
entry_3 = MocketEntry(('localhost', 8080), True)
Mocket.register(entry_1, entry_2, entry_3)
self.assertEqual(Mocket._entries, {
('localhost', 80): [entry_1, entry_2],
('localhost', 8080): [entry_3],
})
def test_collect(self):
request = 'GET /get/p/?b=2&a=1 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: testme.org\r\nConnection: close\r\nUser-Agent: Python-urllib/2.6\r\n\r\n'
Mocket.collect(request)
self.assertEqual(Mocket.last_request(), request)
self.assertEqual(Mocket._requests, [request])
def test_remove_last(self):
Mocket._requests = [1, 2]
Mocket.remove_last_request()
self.assertEqual(Mocket._requests, [1])
def test_remove_last_empty(self):
Mocket.remove_last_request()
self.assertEqual(Mocket._requests, [])
def test_getentry(self):
entry = MocketEntry(('localhost', 80), True)
Mocket.register(entry)
self.assertEqual(Mocket.get_entry('localhost', 80, True), entry)
@mocketize
def test_unmocked_redis(self):
rc = redis.StrictRedis(host='8.8.8.8')
try:
rc.get('foo')
except redis.ConnectionError:
pass
class MocketizeTestCase(TestCase):
def mocketize_setup(self):
pass
def mocketize_teardown(self):
pass
@mocketize
def test_gethostname(self):
self.assertEqual(socket.gethostname(), 'localhost')