34
34
return threads.values()
39
def __init__(self, port, messages=None):
39
def __init__(self, messages):
42
40
self.messages = messages
43
self.read_end, self.write_end = os.pipe()
46
os.write(self.write_end, 'asdf')
53
os.read(self.read_end, 1)
56
def start_server(self):
57
service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
58
service.messages = self.messages
59
for archive_id, messages in service.messages.iteritems():
60
for message in messages:
61
message.setdefault('headers', {})
63
service.serve_forever()
65
def __exit__(self, exc_type, exc_val, traceback):
66
os.kill(self.pid, SIGKILL)
69
SUPPORTED_ORDERS = set(
70
['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
74
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
77
message = self.rfile.read(int(self.headers['content-length']))
78
if message == 'This is a message':
79
self.send_response(httplib.CREATED)
83
self.send_error(httplib.BAD_REQUEST)
86
scheme, netloc, path, params, query_string, fragments = (
88
parts = path.split('/')
89
if parts[1] == 'archive':
90
self.get_messages(parts[2], query_string)
92
42
def get_messages(self, archive_id, query_string):
93
43
query = parse_qs(query_string)
94
44
parameters = simplejson.loads(query['parameters'][0])
95
45
order = parameters.get('order')
96
messages = self.server.messages[archive_id]
46
messages = self.messages[archive_id]
97
47
if order is not None :
98
48
if order not in SUPPORTED_ORDERS:
99
self.send_response(httplib.BAD_REQUEST)
100
self.wfile.write('Unsupported order')
49
raise UnsupportedOrder
102
50
elif order.startswith('thread_'):
103
51
threaded = threaded_messages(messages)
115
63
messages = [m for m in messages
116
64
if 'message_ids' not in parameters or
117
65
m['message_id'] in parameters['message_ids']]
118
self.send_response(httplib.OK)
120
66
limit = parameters.get('limit', 100)
121
67
memo = parameters.get('memo')
122
68
message_id_indices = dict(
143
89
(k, v) for k, v in message['headers'].iteritems()
144
90
if k in parameters['headers'])
145
91
message['headers'] = headers
92
max_body = parameters.get('max_body_length')
93
if max_body is not None:
94
message['body'] = message['body'][:max_body]
146
95
new_messages.append(message)
148
97
'messages': new_messages,
149
98
'next_memo': next_memo,
150
99
'previous_memo': previous_memo
152
self.wfile.write(simplejson.dumps(response))
107
def __init__(self, port, messages=None):
110
self.messages = messages
111
self.read_end, self.write_end = os.pipe()
114
os.write(self.write_end, 'asdf')
121
os.read(self.read_end, 1)
124
def start_server(self):
125
service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
126
service.store = GrackleStore(self.messages)
127
for archive_id, messages in service.store.messages.iteritems():
128
for message in messages:
129
message.setdefault('headers', {})
131
service.serve_forever()
133
def __exit__(self, exc_type, exc_val, traceback):
134
os.kill(self.pid, SIGKILL)
137
SUPPORTED_ORDERS = set(
138
['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
142
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
145
message = self.rfile.read(int(self.headers['content-length']))
146
if message == 'This is a message':
147
self.send_response(httplib.CREATED)
151
self.send_error(httplib.BAD_REQUEST)
154
scheme, netloc, path, params, query_string, fragments = (
156
parts = path.split('/')
157
if parts[1] == 'archive':
159
response = self.server.store.get_messages(
160
parts[2], query_string)
161
self.send_response(httplib.OK)
163
self.wfile.write(simplejson.dumps(response))
164
except UnsupportedOrder:
165
self.send_response(httplib.BAD_REQUEST)
166
self.wfile.write('Unsupported order')
155
170
def fake_grackle_service(client, messages=None):
320
335
first_message = response['messages'][0]
321
336
self.assertEqual('foo', first_message['message_id'])
322
337
self.assertEqual({'From': 'me', 'To': 'you'}, first_message['headers'])
339
def test_get_messages_max_body_length(self):
340
client = GrackleClient('localhost', 8440)
341
with fake_grackle_service(client,
343
{'message_id': 'foo', 'body': u'abcdefghi'}
345
response = client.get_messages('baz', max_body_length=3)
346
first_message = response['messages'][0]
347
self.assertEqual('abc', first_message['body'])