forked from benoitc/gunicorn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.py
119 lines (90 loc) · 2.91 KB
/
t.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
119
# -*- coding: utf-8 -
# Copyright 2009 Paul J. Davis <[email protected]>
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.
from __future__ import with_statement
import array
import os
import tempfile
dirname = os.path.dirname(__file__)
from gunicorn.http.parser import RequestParser
from gunicorn.config import Config
from gunicorn.six import BytesIO
def data_source(fname):
buf = BytesIO()
with open(fname) as handle:
for line in handle:
line = line.rstrip("\n").replace("\\r\\n", "\r\n")
buf.write(line.encode('latin1'))
return buf
class request(object):
def __init__(self, name):
self.fname = os.path.join(dirname, "requests", name)
def __call__(self, func):
def run():
src = data_source(self.fname)
func(src, RequestParser(src))
run.func_name = func.func_name
return run
class FakeSocket(object):
def __init__(self, data):
self.tmp = tempfile.TemporaryFile()
if data:
self.tmp.write(data.getvalue())
self.tmp.flush()
self.tmp.seek(0)
def fileno(self):
return self.tmp.fileno()
def len(self):
return self.tmp.len
def recv(self, length=None):
return self.tmp.read()
def recv_into(self, buf, length):
tmp_buffer = self.tmp.read(length)
v = len(tmp_buffer)
for i, c in enumerate(tmp_buffer):
buf[i] = c
return v
def send(self, data):
self.tmp.write(data)
self.tmp.flush()
def seek(self, offset, whence=0):
self.tmp.seek(offset, whence)
class http_request(object):
def __init__(self, name):
self.fname = os.path.join(dirname, "requests", name)
def __call__(self, func):
def run():
fsock = FakeSocket(data_source(self.fname))
req = Request(Config(), fsock, ('127.0.0.1', 6000), ('127.0.0.1', 8000))
func(req)
run.func_name = func.func_name
return run
def eq(a, b):
assert a == b, "%r != %r" % (a, b)
def ne(a, b):
assert a != b, "%r == %r" % (a, b)
def lt(a, b):
assert a < b, "%r >= %r" % (a, b)
def gt(a, b):
assert a > b, "%r <= %r" % (a, b)
def isin(a, b):
assert a in b, "%r is not in %r" % (a, b)
def isnotin(a, b):
assert a not in b, "%r is in %r" % (a, b)
def has(a, b):
assert hasattr(a, b), "%r has no attribute %r" % (a, b)
def hasnot(a, b):
assert not hasattr(a, b), "%r has an attribute %r" % (a, b)
def istype(a, b):
assert isinstance(a, b), "%r is not an instance of %r" % (a, b)
def raises(exctype, func, *args, **kwargs):
try:
func(*args, **kwargs)
except exctype:
pass
else:
func_name = getattr(func, "func_name", "<builtin_function>")
raise AssertionError("Function %s did not raise %s" % (
func_name, exctype.__name__))