Ejemplo n.º 1
0
def test_urlmatch_with_extra_args_to_responder(defer):
    node = Node()
    defer(node.stop)

    responder = make_responder(lambda req, extra: req.write(str(extra)))
    rnd_data = bytes(random.random())
    responders = [(r'', responder.using(extra=rnd_data))]
    http_server = node.spawn(HttpServer.using(address=('localhost', 0), responders=responders))
    _, port = actor_exec(node, lambda: http_server.ask('get-addr'))

    eq_(rnd_data, requests.get('http://localhost:%d' % (port,)).text)
Ejemplo n.º 2
0
def test_urlmatch_with_extra_args_to_responder(defer):
    node = Node()
    defer(node.stop)

    responder = make_responder(lambda req, extra: req.write(str(extra)))
    rnd_data = bytes(random.random())
    responders = [(r'', responder.using(extra=rnd_data))]
    http_server = node.spawn(
        HttpServer.using(address=('localhost', 0), responders=responders))
    _, port = actor_exec(node, lambda: http_server.ask('get-addr'))

    eq_(rnd_data, requests.get('http://localhost:%d' % (port, )).text)
Ejemplo n.º 3
0
def test_stop(defer):
    node = Node()
    defer(node.stop)

    responders = []
    http_server = node.spawn(HttpServer.using(address=('localhost', 0), responders=responders))
    _, port = actor_exec(node, lambda: http_server.ask('get-addr'))

    eq_(404, requests.get('http://localhost:%d' % (port,)).status_code)

    http_server.stop()

    with assert_raises(requests.ConnectionError):
        requests.get('http://localhost:%d' % (port,))
Ejemplo n.º 4
0
def test_stop(defer):
    node = Node()
    defer(node.stop)

    responders = []
    http_server = node.spawn(
        HttpServer.using(address=('localhost', 0), responders=responders))
    _, port = actor_exec(node, lambda: http_server.ask('get-addr'))

    eq_(404, requests.get('http://localhost:%d' % (port, )).status_code)

    http_server.stop()

    with assert_raises(requests.ConnectionError):
        requests.get('http://localhost:%d' % (port, ))
Ejemplo n.º 5
0
def test_basic(defer):
    node = Node()
    defer(node.stop)

    responders = []
    http_server = node.spawn(HttpServer.using(address=('localhost', 0), responders=responders))
    _, port = actor_exec(node, lambda: http_server.ask('get-addr'))

    eq_(404, requests.get('http://localhost:%d' % (port,)).status_code)

    rnd_response = str(random.random())
    responders[:] = [
        (r'^/$', make_responder(lambda request: request.write(rnd_response))),
    ]
    eq_(200, requests.get('http://localhost:%d' % (port,)).status_code)
Ejemplo n.º 6
0
def test_basic(defer):
    node = Node()
    defer(node.stop)

    responders = []
    http_server = node.spawn(
        HttpServer.using(address=('localhost', 0), responders=responders))
    _, port = actor_exec(node, lambda: http_server.ask('get-addr'))

    eq_(404, requests.get('http://localhost:%d' % (port, )).status_code)

    rnd_response = str(random.random())
    responders[:] = [
        (r'^/$', make_responder(lambda request: request.write(rnd_response))),
    ]
    eq_(200, requests.get('http://localhost:%d' % (port, )).status_code)
Ejemplo n.º 7
0
def test_file_upload(defer):
    node = Node()
    defer(node.stop)

    def handle_file(request):
        request.write(request.files['file'].read())

    responders = [(r'^/handle-file$', make_responder(handle_file))]
    http_server = node.spawn(HttpServer.using(address=('localhost', 0), responders=responders))
    _, port = actor_exec(node, lambda: http_server.ask('get-addr'))

    rnd_data = bytes(random.random())
    with tempfile.NamedTemporaryFile() as f:
        f.write(rnd_data)
        f.flush()
        req = requests.post('http://localhost:%d/handle-file' % (port,), files={'file': open(f.name, 'rb')})
    eq_(200, req.status_code)
    eq_(rnd_data, req.text)
Ejemplo n.º 8
0
def test_file_upload(defer):
    node = Node()
    defer(node.stop)

    def handle_file(request):
        request.write(request.files['file'].read())

    responders = [(r'^/handle-file$', make_responder(handle_file))]
    http_server = node.spawn(
        HttpServer.using(address=('localhost', 0), responders=responders))
    _, port = actor_exec(node, lambda: http_server.ask('get-addr'))

    rnd_data = bytes(random.random())
    with tempfile.NamedTemporaryFile() as f:
        f.write(rnd_data)
        f.flush()
        req = requests.post('http://localhost:%d/handle-file' % (port, ),
                            files={'file': open(f.name, 'rb')})
    eq_(200, req.status_code)
    eq_(rnd_data, req.text)