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

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
6.1.7 by Aaron Bentley
Retrieve messages.
8
import simplejson
4 by Aaron Bentley
Initial test.
9
from StringIO import StringIO
3 by Aaron Bentley
Add test framework.
10
from unittest import TestCase
6.1.7 by Aaron Bentley
Retrieve messages.
11
from urlparse import urlparse
6.1.9 by Aaron Bentley
Test filtering by message-id.
12
from urlparse import parse_qs
3 by Aaron Bentley
Add test framework.
13
5 by Aaron Bentley
Actual fake service working.
14
from testtools import ExpectedException
15
6.1.2 by Aaron Bentley
Test message path.
16
from grackle.client import (
17
    GrackleClient,
18
    )
4 by Aaron Bentley
Initial test.
19
20
6.1.6 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
21
class ForkedFake:
5 by Aaron Bentley
Actual fake service working.
22
6.1.6 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
23
    def __init__(self, port, messages=None):
5 by Aaron Bentley
Actual fake service working.
24
        self.pid = None
6.1.6 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
25
        self.port = port
26
        self.messages = messages
27
        self.read_end, self.write_end = os.pipe()
28
29
    def is_ready(self):
30
        os.write(self.write_end, 'asdf')
5 by Aaron Bentley
Actual fake service working.
31
32
    def __enter__(self):
33
        pid = os.fork()
6.1.6 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
34
        if pid == 0:
35
            self.start_server()
36
        self.pid = pid
37
        os.read(self.read_end, 1)
38
        return
5 by Aaron Bentley
Actual fake service working.
39
6.1.6 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
40
    def start_server(self):
41
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
42
        service.messages = self.messages
43
        self.is_ready()
44
        service.serve_forever()
5 by Aaron Bentley
Actual fake service working.
45
46
    def __exit__(self, exc_type, exc_val, traceback):
47
        os.kill(self.pid, SIGKILL)
48
49
50
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
51
6.1.5 by Aaron Bentley
Start working on GET.
52
    def do_POST(self):
5 by Aaron Bentley
Actual fake service working.
53
        message = self.rfile.read(int(self.headers['content-length']))
54
        if message == 'This is a message':
6 by Aaron Bentley
Use constants.
55
            self.send_response(httplib.CREATED)
5 by Aaron Bentley
Actual fake service working.
56
            self.end_headers()
57
            self.wfile.close()
58
        else:
6 by Aaron Bentley
Use constants.
59
            self.send_error(httplib.BAD_REQUEST)
5 by Aaron Bentley
Actual fake service working.
60
6.1.7 by Aaron Bentley
Retrieve messages.
61
    def do_GET(self):
6.1.9 by Aaron Bentley
Test filtering by message-id.
62
        scheme, netloc, path, params, query_string, fragments = (
63
            urlparse(self.path))
6.1.7 by Aaron Bentley
Retrieve messages.
64
        archive = os.path.split(path)[1]
6.1.9 by Aaron Bentley
Test filtering by message-id.
65
        query = parse_qs(query_string)
66
        parameters = simplejson.loads(query['parameters'][0])
6.1.7 by Aaron Bentley
Retrieve messages.
67
        self.send_response(httplib.OK)
68
        self.end_headers()
6.1.9 by Aaron Bentley
Test filtering by message-id.
69
        messages = [m for m in self.server.messages[archive] if 'message_ids'
70
                    not in parameters or m['message-id'] in
71
                    parameters['message_ids']]
72
        self.wfile.write(simplejson.dumps(messages))
6.1.7 by Aaron Bentley
Retrieve messages.
73
5 by Aaron Bentley
Actual fake service working.
74
6.1.5 by Aaron Bentley
Start working on GET.
75
def fake_grackle_service(client, messages=None):
76
    if messages is None:
77
        messages = {}
6.1.6 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
78
    return ForkedFake(client.port, messages)
6.1.5 by Aaron Bentley
Start working on GET.
79
4 by Aaron Bentley
Initial test.
80
3 by Aaron Bentley
Add test framework.
81
class TestPutMessage(TestCase):
82
83
    def test_put_message(self):
6.1.6 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
84
        client = GrackleClient('localhost', 8436)
6.1.5 by Aaron Bentley
Start working on GET.
85
        with fake_grackle_service(client):
6.1.1 by Aaron Bentley
Fix URLs etc.
86
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
5 by Aaron Bentley
Actual fake service working.
87
            with ExpectedException(Exception, 'wtf'):
6.1.1 by Aaron Bentley
Fix URLs etc.
88
                client.put_message('arch1', 'asdf',
89
                    StringIO('This is not a message'))
6.1.5 by Aaron Bentley
Start working on GET.
90
91
92
class TestGetMessages(TestCase):
93
94
    def test_get_messages(self):
95
        client = GrackleClient('localhost', 8435)
96
        with fake_grackle_service(client,
97
            {'baz':
98
            [{'message-id': 'foo'},
99
             {'message-id': 'bar'}]}):
6.1.9 by Aaron Bentley
Test filtering by message-id.
100
            response = client.get_messages('baz')
6.1.7 by Aaron Bentley
Retrieve messages.
101
        self.assertEqual(['bar', 'foo'], sorted(m['message-id'] for m in
102
            response))
6.1.9 by Aaron Bentley
Test filtering by message-id.
103
104
    def test_get_messages_by_id(self):
105
        client = GrackleClient('localhost', 8437)
106
        with fake_grackle_service(client,
107
            {'baz':
108
            [{'message-id': 'foo'},
109
             {'message-id': 'bar'}]}):
110
            message, = client.get_messages('baz', message_ids=['foo'])
111
        self.assertEqual('foo', message['message-id'])