1
from BaseHTTPServer import (
3
BaseHTTPRequestHandler,
7
from signal import SIGKILL
8
from StringIO import StringIO
9
1
from unittest import TestCase
11
from testtools import ExpectedException
13
from grackle.client import (
20
def __init__(self, func_or_method, *args):
21
self.func_or_method = func_or_method
30
self.func_or_method(*self.args)
33
def __exit__(self, exc_type, exc_val, traceback):
34
os.kill(self.pid, SIGKILL)
37
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
40
message = self.rfile.read(int(self.headers['content-length']))
41
if message == 'This is a message':
42
self.send_response(httplib.CREATED)
46
self.send_error(httplib.BAD_REQUEST)
49
def run_service(port):
50
service = HTTPServer(('', port), FakeGrackleRequestHandler)
51
service.serve_forever()
55
3
class TestPutMessage(TestCase):
57
5
def test_put_message(self):
58
client = GrackleClient('localhost', 8435)
59
with Forked(run_service, client.port):
60
client.put_message('arch1', 'asdf', StringIO('This is a message'))
61
with ExpectedException(Exception, 'wtf'):
62
client.put_message('arch1', 'asdf',
63
StringIO('This is not a message'))