4
by Aaron Bentley
Initial test. |
1 |
from BaseHTTPServer import ( |
2 |
HTTPServer, |
|
3 |
BaseHTTPRequestHandler, |
|
4 |
)
|
|
6
by Aaron Bentley
Use constants. |
5 |
import httplib |
4
by Aaron Bentley
Initial test. |
6 |
import os |
5
by Aaron Bentley
Actual fake service working. |
7 |
from signal import SIGKILL |
4
by Aaron Bentley
Initial test. |
8 |
from StringIO import StringIO |
3
by Aaron Bentley
Add test framework. |
9 |
from unittest import TestCase |
10 |
||
5
by Aaron Bentley
Actual fake service working. |
11 |
from testtools import ExpectedException |
12 |
||
4
by Aaron Bentley
Initial test. |
13 |
from grackle import client |
14 |
||
15 |
||
5
by Aaron Bentley
Actual fake service working. |
16 |
class Forked: |
17 |
||
18 |
def __init__(self, func_or_method): |
|
19 |
self.func_or_method = func_or_method |
|
20 |
self.pid = None |
|
21 |
||
22 |
def __enter__(self): |
|
23 |
pid = os.fork() |
|
24 |
if pid != 0: |
|
25 |
self.pid = pid |
|
26 |
return
|
|
27 |
self.func_or_method() |
|
28 |
||
29 |
||
30 |
def __exit__(self, exc_type, exc_val, traceback): |
|
31 |
os.kill(self.pid, SIGKILL) |
|
32 |
||
33 |
||
34 |
class FakeGrackleRequestHandler(BaseHTTPRequestHandler): |
|
35 |
||
36 |
def do_POST(self): |
|
37 |
message = self.rfile.read(int(self.headers['content-length'])) |
|
38 |
if message == 'This is a message': |
|
6
by Aaron Bentley
Use constants. |
39 |
self.send_response(httplib.CREATED) |
5
by Aaron Bentley
Actual fake service working. |
40 |
self.end_headers() |
41 |
self.wfile.close() |
|
42 |
else: |
|
6
by Aaron Bentley
Use constants. |
43 |
self.send_error(httplib.BAD_REQUEST) |
5
by Aaron Bentley
Actual fake service working. |
44 |
|
45 |
||
46 |
def run_service(): |
|
47 |
service = HTTPServer(('', 8435), FakeGrackleRequestHandler) |
|
48 |
service.serve_forever() |
|
49 |
||
4
by Aaron Bentley
Initial test. |
50 |
|
51 |
||
3
by Aaron Bentley
Add test framework. |
52 |
class TestPutMessage(TestCase): |
53 |
||
54 |
def test_put_message(self): |
|
5
by Aaron Bentley
Actual fake service working. |
55 |
with Forked(run_service): |
56 |
client.put_message('arch1', StringIO('This is a message')) |
|
57 |
with ExpectedException(Exception, 'wtf'): |
|
58 |
client.put_message('arch1', StringIO('This is not a message')) |