~didrocks/unity/altf10

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
13 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
13 by Aaron Bentley
Retrieve messages.
11
from urlparse import urlparse
15 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
8 by Aaron Bentley
Test message path.
16
from grackle.client import (
17
    GrackleClient,
18
    )
4 by Aaron Bentley
Initial test.
19
20
12 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
12 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
12 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()
12 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
12 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
11 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
13 by Aaron Bentley
Retrieve messages.
61
    def do_GET(self):
15 by Aaron Bentley
Test filtering by message-id.
62
        scheme, netloc, path, params, query_string, fragments = (
63
            urlparse(self.path))
13 by Aaron Bentley
Retrieve messages.
64
        archive = os.path.split(path)[1]
15 by Aaron Bentley
Test filtering by message-id.
65
        query = parse_qs(query_string)
66
        parameters = simplejson.loads(query['parameters'][0])
13 by Aaron Bentley
Retrieve messages.
67
        self.send_response(httplib.OK)
68
        self.end_headers()
15 by Aaron Bentley
Test filtering by message-id.
69
        messages = [m for m in self.server.messages[archive] if 'message_ids'
17 by Aaron Bentley
Switch hyphens to underscores.
70
                    not in parameters or m['message_id'] in
15 by Aaron Bentley
Test filtering by message-id.
71
                    parameters['message_ids']]
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
72
        response = {
73
            'messages': messages,
74
            'next_memo': None,
75
            'previous_memo': None
76
            }
77
        self.wfile.write(simplejson.dumps(response))
13 by Aaron Bentley
Retrieve messages.
78
5 by Aaron Bentley
Actual fake service working.
79
11 by Aaron Bentley
Start working on GET.
80
def fake_grackle_service(client, messages=None):
81
    if messages is None:
82
        messages = {}
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
83
    return ForkedFake(client.port, messages)
11 by Aaron Bentley
Start working on GET.
84
4 by Aaron Bentley
Initial test.
85
3 by Aaron Bentley
Add test framework.
86
class TestPutMessage(TestCase):
87
88
    def test_put_message(self):
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
89
        client = GrackleClient('localhost', 8436)
11 by Aaron Bentley
Start working on GET.
90
        with fake_grackle_service(client):
7 by Aaron Bentley
Fix URLs etc.
91
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
5 by Aaron Bentley
Actual fake service working.
92
            with ExpectedException(Exception, 'wtf'):
7 by Aaron Bentley
Fix URLs etc.
93
                client.put_message('arch1', 'asdf',
94
                    StringIO('This is not a message'))
11 by Aaron Bentley
Start working on GET.
95
96
97
class TestGetMessages(TestCase):
98
99
    def test_get_messages(self):
100
        client = GrackleClient('localhost', 8435)
101
        with fake_grackle_service(client,
102
            {'baz':
17 by Aaron Bentley
Switch hyphens to underscores.
103
            [{'message_id': 'foo'},
104
             {'message_id': 'bar'}]}):
15 by Aaron Bentley
Test filtering by message-id.
105
            response = client.get_messages('baz')
17 by Aaron Bentley
Switch hyphens to underscores.
106
        self.assertEqual(['bar', 'foo'], sorted(m['message_id'] for m in
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
107
            response['messages']))
108
        self.assertIs(None, response['next_memo'])
109
        self.assertIs(None, response['previous_memo'])
15 by Aaron Bentley
Test filtering by message-id.
110
111
    def test_get_messages_by_id(self):
112
        client = GrackleClient('localhost', 8437)
113
        with fake_grackle_service(client,
114
            {'baz':
17 by Aaron Bentley
Switch hyphens to underscores.
115
            [{'message_id': 'foo'},
116
             {'message_id': 'bar'}]}):
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
117
            response = client.get_messages('baz', message_ids=['foo'])
118
        message, = response['messages']
17 by Aaron Bentley
Switch hyphens to underscores.
119
        self.assertEqual('foo', message['message_id'])