Esempio n. 1
0
def test_wait(mock_server):
    imposter = Imposter(Stub(responses=Response(wait=100)))

    with mock_server(imposter), Timer() as timer:
        requests.get(imposter.url)

        assert_that(timer.elapsed, between(0.1, 0.25))
Esempio n. 2
0
def test_response_matcher_builder():
    # Given
    stub_response = MOCK_RESPONSE
    matcher = (is_response().with_status_code(200).and_body(
        "sausages").and_content(b"content").and_json(has_entries(
            a="b")).and_headers(has_entries(key="value")).and_cookies(
                has_entries(name="value")).and_elapsed(
                    between(timedelta(seconds=1),
                            timedelta(minutes=1))).and_history(
                                contains_exactly(
                                    is_response().with_url(
                                        is_url().with_path("/path1")),
                                    is_response().with_url(
                                        is_url().with_path("/path2")),
                                )).and_url(is_url().with_path(
                                    "/path0")).and_encoding("utf-8"))
    mismatcher = is_response().with_body("kale").and_status_code(404)

    # When

    # Then
    assert_that(stub_response, matcher)
    assert_that(stub_response, not_(mismatcher))
    assert_that(
        matcher,
        has_string(
            "response with "
            "status_code: <200> "
            "body: 'sausages' "
            "content: <b'content'> "
            "json: a dictionary containing {'a': 'b'} "
            "headers: a dictionary containing {'key': 'value'} "
            "cookies: a dictionary containing {'name': 'value'} "
            "elapsed: (a value greater than or equal to <0:00:01> and a value less than or equal to <0:01:00>) "
            "history: a sequence containing "
            "[response with url: URL with path: '/path1', response with url: URL with path: '/path2'] "
            "url: URL with path: '/path0' "
            "encoding: 'utf-8'"),
    )
    assert_that(
        mismatcher,
        mismatches_with(
            stub_response,
            contains_string(
                "was response with status code: was <200> body: was 'sausages'"
            ),
        ),
    )
    assert_that(
        matcher,
        matches_with(
            stub_response,
            contains_string(
                "was response with status code: was <200> body: was 'sausages'"
            ),
        ),
    )
Esempio n. 3
0
def test_wait_function(mock_server):
    imposter = Imposter(
        Stub(responses=Response(wait="function() { return Math.floor(Math.random() * 50) + 100; }"))
    )

    with mock_server(imposter), Timer() as timer:
        requests.get(imposter.url)

        assert_that(timer.elapsed, between(0.1, 0.5))
Esempio n. 4
0
def test_proxy_delay(mock_server):
    imposter = Imposter(Stub(responses=Proxy(to="http://example.com", wait=500)))

    with mock_server(imposter), Timer() as t:
        requests.get("{0}/".format(imposter.url))

    assert_that(
        t.elapsed, between(0.5, 0.9)
    )  # Slightly longer than the wait time, to give example.com and the 'net time to work.
Esempio n. 5
0
def redirects_to(url_matcher: Union[str, Matcher]) -> Matcher[Response]:
    """Is a response a redirect to a URL matching the suplplied matcher? Matches :requests.models.Response:.
    :param url_matcher: Expected URL.
    """
    return described_as(
        str(StringDescription().append_text(
            "redirects to ").append_description_of(url_matcher)),
        is_response().with_status_code(between(300, 399)).and_headers(
            has_entry("Location", url_matcher)),
    )
Esempio n. 6
0
def test_between():
    # Given
    r = 2

    # When

    # Then
    assert_that(r, between(1, 3))
    assert_that(r, not_(between(4, 6)))
    assert_that(
        between(1, 3),
        has_string(
            "(a value greater than or equal to <1> and a value less than or equal to <3>)"
        ),
    )
    assert_that(
        between(1, 3, lower_inclusive=False, upper_inclusive=False),
        has_string("(a value greater than <1> and a value less than <3>)"),
    )
    assert_that(between(4, 6), mismatches_with(3, contains_string("was <3>")))
Esempio n. 7
0
def test_proxy_delay(mock_server):
    target_imposter = Imposter(Stub(Predicate(path="/test")))
    with mock_server(target_imposter) as server:
        proxy_imposter = Imposter(
            Stub(responses=Proxy(to=target_imposter.url, wait=100)))
        server.add_imposters(proxy_imposter)

        with Timer() as timer:
            requests.get(proxy_imposter.url / "test")

            assert_that(timer.elapsed, between(0.1, 0.2))
Esempio n. 8
0
def test_response_elapsed():
    # Given

    # When
    actual = requests.get("https://httpbin.org/delay/0.5")

    # Then
    assert_that(
        actual,
        is_response().with_status_code(200).and_elapsed(
            between(timedelta(seconds=0.5), timedelta(seconds=1.5))),
    )
Esempio n. 9
0
def test_between_dates():
    # Given
    date = datetime.date(1968, 7, 21)

    # When

    # Then
    assert_that(
        date, between(datetime.date(1968, 7, 20), datetime.date(1968, 7, 22)))
    assert_that(
        date,
        not_(between(datetime.date(1968, 7, 22), datetime.date(1968, 7, 24))))
    assert_that(
        between(datetime.date(1968, 7, 20), datetime.date(1968, 7, 22)),
        has_string(
            "(a value greater than or equal to <1968-07-20> and a value less than or equal to <1968-07-22>)"
        ),
    )
    assert_that(
        between(datetime.date(1968, 7, 22), datetime.date(1968, 7, 24)),
        mismatches_with(date, contains_string("was <1968-07-21>")),
    )
Esempio n. 10
0
def test_between_exclusive():
    assert_that(3, not_(between(1, 3, upper_inclusive=False)))
    assert_that(3, between(1, 3))
    assert_that(1, not_(between(1, 3, lower_inclusive=False)))
    assert_that(1, between(1, 3))
Esempio n. 11
0
def is_weekday() -> Matcher[date]:
    """Match if date is a weekday."""
    matcher = HasWeekday(between(0, 4))
    return described_as("A weekday", matcher)