File: test_routing.py

package info (click to toggle)
dask.distributed 2022.12.1%2Bds.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,164 kB
  • sloc: python: 81,938; javascript: 1,549; makefile: 228; sh: 100
file content (41 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import annotations

import pytest
from tornado import web
from tornado.httpclient import AsyncHTTPClient, HTTPClientError

from distributed.http.routing import RoutingApplication
from distributed.utils_test import gen_test


class OneHandler(web.RequestHandler):
    def get(self):
        self.write("one")


class TwoHandler(web.RequestHandler):
    def get(self):
        self.write("two")


@gen_test()
async def test_basic():
    application = RoutingApplication([(r"/one", OneHandler)])
    two = web.Application([(r"/two", TwoHandler)])
    server = application.listen(1234)

    client = AsyncHTTPClient("http://localhost:1234")
    response = await client.fetch("http://localhost:1234/one")
    assert response.body.decode() == "one"

    with pytest.raises(HTTPClientError):
        response = await client.fetch("http://localhost:1234/two")

    application.applications.append(two)

    response = await client.fetch("http://localhost:1234/two")
    assert response.body.decode() == "two"

    application.add_handlers(".*", [(r"/three", OneHandler, {})])
    response = await client.fetch("http://localhost:1234/three")
    assert response.body.decode() == "one"