~unity-2d-team/unity-2d/Shell-MultiMonitor

« back to all changes in this revision

Viewing changes to grackle/tests/test_client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-10 11:06:36 UTC
  • Revision ID: aaron@canonical.com-20120110110636-fztfdo4gn1qfqty3
Add skeleton grackle-put-message.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from BaseHTTPServer import (
2
 
    HTTPServer,
3
 
    BaseHTTPRequestHandler,
4
 
    )
5
 
import httplib
6
 
import os
7
 
from signal import SIGKILL
8
 
from StringIO import StringIO
9
 
from unittest import TestCase
10
 
 
11
 
from testtools import ExpectedException
12
 
 
13
 
from grackle import client
14
 
 
15
 
 
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':
39
 
            self.send_response(httplib.CREATED)
40
 
            self.end_headers()
41
 
            self.wfile.close()
42
 
        else:
43
 
            self.send_error(httplib.BAD_REQUEST)
44
 
 
45
 
 
46
 
def run_service():
47
 
    service = HTTPServer(('', 8435), FakeGrackleRequestHandler)
48
 
    service.serve_forever()
49
 
 
50
 
 
51
 
 
52
 
class TestPutMessage(TestCase):
53
 
 
54
 
    def test_put_message(self):
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'))