~hudson-openstack/burrow/trunk

« back to all changes in this revision

Viewing changes to burrow/frontend/wsgi.py

  • Committer: Tarmac
  • Author(s): Eric Day
  • Date: 2011-08-10 05:48:15 UTC
  • mfrom: (16.3.23 backend-unittests)
  • Revision ID: tarmac-20110810054815-xzg9slms24xpdnrp
Added HTTP backend unit tests that work against a running server (auto started/killed with test).

Show diffs side-by-side

added added

removed removed

Lines of Context:
173
173
    @webob.dec.wsgify
174
174
    def _delete_message(self, req, account, queue, message):
175
175
        filters = self._parse_filters(req)
176
 
        try:
177
 
            message = self.backend.delete_message(account, queue, message,
178
 
                filters)
179
 
        except burrow.backend.NotFound:
180
 
            return self._response(status=404)
181
 
        return self._response(body=message)
 
176
        body = lambda: self.backend.delete_message(account, queue, message,
 
177
            filters)
 
178
        return self._response(body=body)
182
179
 
183
180
    @webob.dec.wsgify
184
181
    def _get_message(self, req, account, queue, message):
185
182
        filters = self._parse_filters(req)
186
 
        try:
187
 
            message = self.backend.get_message(account, queue, message,
188
 
                filters)
189
 
        except burrow.backend.NotFound:
190
 
            return self._response(status=404)
191
 
        return self._response(body=message)
 
183
        body = lambda: self.backend.get_message(account, queue, message,
 
184
            filters)
 
185
        return self._response(body=body)
192
186
 
193
187
    @webob.dec.wsgify
194
188
    def _post_message(self, req, account, queue, message):
195
189
        attributes = self._parse_attributes(req)
196
190
        filters = self._parse_filters(req)
197
 
        try:
198
 
            message = self.backend.update_message(account, queue, message,
199
 
                attributes, filters)
200
 
        except burrow.backend.NotFound:
201
 
            return self._response(status=404)
202
 
        return self._response(body=message)
 
191
        body = lambda: self.backend.update_message(account, queue, message,
 
192
            attributes, filters)
 
193
        return self._response(body=body)
203
194
 
204
195
    @webob.dec.wsgify
205
196
    def _put_message(self, req, account, queue, message):
241
232
        return attributes
242
233
 
243
234
    def _response(self, status=200, body=None, content_type=None):
244
 
        if isinstance(body, types.GeneratorType):
245
 
            try:
 
235
        try:
 
236
            if isinstance(body, types.GeneratorType):
246
237
                body = list(body)
247
 
            except burrow.backend.NotFound:
248
 
                body = None
249
 
                status = 404
 
238
            if isinstance(body, types.FunctionType):
 
239
                body = body()
 
240
        except burrow.backend.InvalidArguments:
 
241
            status = 400
 
242
            body = None
 
243
        except burrow.backend.NotFound:
 
244
            status = 404
 
245
            body = None
250
246
        if body == []:
251
247
            body = None
252
248
        if body is None:
258
254
                if isinstance(body, list) or isinstance(body, dict):
259
255
                    content_type = 'application/json'
260
256
                else:
261
 
                    content_type = "application/octet-stream"
 
257
                    content_type = 'application/octet-stream'
262
258
            if content_type == 'application/json':
263
259
                body = json.dumps(body, indent=2)
264
 
        return webob.Response(status=status, body=body,
265
 
            content_type=content_type)
 
260
        response = webob.Response(status=status)
 
261
        if body is not None:
 
262
            response.content_type = content_type
 
263
            if isinstance(body, unicode):
 
264
                response.unicode_body = body
 
265
            else:
 
266
                response.body = body
 
267
        return response
266
268
 
267
269
 
268
270
class WSGILog(object):