def test_expected_request_handler(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_handler(lambda request: JSON_STRING)
    assert requests.get(httpserver.url_for("/foobar")).json() == {'foo': 'bar'}
Ejemplo n.º 2
0
def test_link_preview(httpserver: HTTPServer):
    httpserver.expect_request("/preview1").respond_with_data(
        get_sample("twitter-card/with-image.html"),
        headers={"content-type": "text/html"},
    )
    httpserver.expect_request("/preview2").respond_with_data(
        get_sample("generic/h1-img.html"),
        headers={"content-type": "text/html"},
    )
    httpserver.expect_request("/preview-3.json").respond_with_data(
        '{}',
        headers={"content-type": "application/json"},
    )

    url = httpserver.url_for("/preview1")
    preview = link_preview(url)
    assert preview.title == "a title"
    assert preview.force_title == "a title"
    assert preview.description is None
    assert preview.image == "/img/heck.jpg"
    assert preview.absolute_image == "%s%s" % (url, preview.image)

    url = httpserver.url_for("/preview2")
    preview = link_preview(url)
    assert preview.title == "This title is from the first h1 tag."
    assert preview.description is None
    assert preview.image == "http://*****:*****@abc.com/the-bunny(720p)",
                           content="OK")
    assert preview.force_title == "abc.com/the-bunny(720p)"

    preview = link_preview("https://192.168.1.1", content="OK")
    assert preview.force_title == "192.168.1.1"

    preview = link_preview("https://192.168.1.1:9696", content="OK")
    assert preview.force_title == "192.168.1.1:9696"

    preview = link_preview(httpserver.url_for('/preview-3.json'))
    assert preview.title is None
    assert preview.description is None
    assert preview.image is None
    assert preview.absolute_image is None
    assert preview.force_title == "Preview 3"
def test_expected_request_json(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
    assert requests.get(httpserver.url_for("/foobar")).json() == {'foo': 'bar'}
Ejemplo n.º 4
0
def test_client(httpserver: HTTPServer):
    with HTTPServer(host='127.0.0.1', port=8000) as httpserver:
        httpserver.expect_request("/upload_snapshot").respond_with_data("uploaded data")
        upload_sample(('127.0.0.1', 8000), 'tests/data/test_file.mind.gz')
        assert requests.get(httpserver.url_for("/upload_snapshot")).text == "uploaded data"