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