Пример #1
0
async def app(scope, receive, send):
    time_start = timeit.default_timer()
    assert scope["type"] == "http"
    headers = read_headers(scope["headers"])
    try:
        url = urls.URL(path=scope["path"])
        view = router.urls[url.name]
        body = await read_body(receive, headers["content-type"])
        response = view(method=scope["method"], url=url,
                        body=body).handle_request()
        await response.send(asgi_send=send)
        time_end = timeit.default_timer()
        print(time_end - time_start)
        # TODO: Log the time to database.
    except errors.HttpNotFound:
        await send({
            'type': 'http.response.start',
            'status': HTTPStatus.NOT_FOUND,
            "headers": []
        })
        await send({'type': 'http.response.body'})
    except Exception as e:
        print('error', e)
        await send({
            'type': 'http.response.start',
            'status': HTTPStatus.INTERNAL_SERVER_ERROR,
            "headers": []
        })
        await send({'type': 'http.response.body'})
Пример #2
0
 def test_resource_list(self):
     """URLs like /v1/cars should resolve."""
     url = urls.URL("/v1/tomatoes")
     self.assertEqual(url.version, 1)
     self.assertEqual(url.name, "tomatoes")
     self.assertIsNone(url.id)
     self.assertEqual(url.params, {})
Пример #3
0
 def test_resource(self):
     """URLs like /v1/cars/1564rl should resolve."""
     url = urls.URL("/v1/tomatoes/9a908d35-01c8-4734-ac62-3337e5b1c7a2")
     self.assertEqual(url.version, 1)
     self.assertEqual(url.name, "tomatoes")
     self.assertEqual(url.id, "9a908d35-01c8-4734-ac62-3337e5b1c7a2")
     self.assertEqual(url.params, {})
Пример #4
0
 def test_resource_list_with_query_params(self):
     """URLs like /v1/cars?year=2020 should resolve."""
     url = urls.URL("/v1/tomatoes?expired=true",
                    param_fields={"expired": fields.BooleanField()})
     self.assertEqual(url.version, 1)
     self.assertEqual(url.name, "tomatoes")
     self.assertIsNone(url.id)
     self.assertEqual(url.params, {"expired": True})
Пример #5
0
 def test_integer_ids(self):
     """Integer as resource ids should not pass."""
     with self.assertRaises(errors.HttpNotFound):
         urls.URL("/v1/tomatoes/23")