def test_respond_201(): # a 201 response with additional response headers created_resp = mock.Mock() headers = {'Foo': '1', 'Bar': '2'} falcon_support.respond(created_resp, status=falcon.HTTP_CREATED, headers=headers, body="Expected") assert_that(created_resp.body, equal_to("Expected")) assert_that(created_resp.set_header.call_count, equal_to(2)) assert_that(created_resp.status, equal_to(falcon.HTTP_CREATED))
def on_get(req, resp, by_id): resource_id = as_int(by_id) if not resource_id: raise falcon.HTTPBadRequest('Invalid ID', 'Expected integer identifier') result = Prices.get(req.context['session'], resource_id) if result: respond(resp, body=result.json()) else: respond(resp, status=falcon.HTTP_404)
def on_post(req, resp, by_id): raw_body = req.stream.read() if not raw_body: raise falcon.HTTPBadRequest('A valid JSON document is required.', '') try: body = json.loads(raw_body.decode('utf-8')) except UnicodeDecodeError: msg = 'Non-UTF8 characters found in the request body' raise falcon.HTTPBadRequest(msg, '') except ValueError as e: msg = 'Could not parse the body as Json: {0}. Ignoring.'.format(e) raise falcon.HTTPBadRequest(msg, '') submitted = Prices.parse(by_id, body) Prices.record(req.context['session'], submitted) resp.add_link('/prices/%s' % by_id, 'self') respond(resp)
def test_respond_304(): # a 304 response status blocks body publishing not_modified_resp = mock.Mock() falcon_support.respond(not_modified_resp, status=falcon.HTTP_NOT_MODIFIED) not_modified_resp.body.assert_not_called() assert_that(not_modified_resp.status, equal_to(falcon.HTTP_NOT_MODIFIED))
def test_respond_204(): # a 204 response status blocks body publishing no_content_resp = mock.Mock() falcon_support.respond(no_content_resp, status=falcon.HTTP_NO_CONTENT) no_content_resp.body.assert_not_called() assert_that(no_content_resp.status, equal_to(falcon.HTTP_NO_CONTENT))
def test_respond_head(): # a HEAD request blocks body publishing head_resp = mock.Mock() falcon_support.respond(head_resp, method='HEAD', body="Not Expected") head_resp.body.assert_not_called() assert_that(head_resp.status, equal_to(falcon.HTTP_OK))
def test_respond_get(): # a GET request get_resp = mock.Mock() falcon_support.respond(get_resp, body="Expected") assert_that(get_resp.body, equal_to("Expected")) assert_that(get_resp.status, equal_to(falcon.HTTP_OK))
def on_get(req, resp): result = Prices.list(req.context['session']) found = [row.dict() for row in result] respond(resp, body=json.dumps(found))