Ejemplo n.º 1
0
def test_multiple_responses(mock_server):
    imposter = Imposter(
        Stub(responses=[Response(
            body="sausages"), Response(body="egg")]))

    with mock_server(imposter):
        r1 = requests.get(imposter.url)
        r2 = requests.get(imposter.url)
        r3 = requests.get(imposter.url)

        assert_that(r1, is_(response_with(body="sausages")))
        assert_that(r2, is_(response_with(body="egg")))
        assert_that(r3, is_(response_with(body="sausages")))
Ejemplo n.º 2
0
def test_json_payload(mock_server):
    # Given
    imposter = Imposter(
        Stub(Predicate(body={"foo": ["bar", "baz"]}),
             Response(body="sausages")))

    with mock_server(imposter):
        # When
        r1 = requests.get(imposter.url, json={"foo": ["bar", "baz"]})
        r2 = requests.get(imposter.url, json={"baz": ["bar", "foo"]})

        # Then
        assert_that(r1, is_(response_with(body="sausages")))
        assert_that(r2, not_(response_with(body="sausages")))
Ejemplo n.º 3
0
def test_jsonpath_copy(mock_server):
    imposter = Imposter(
        Stub(
            responses=Response(
                body="Have you read BOOK?", copy=Copy("body", "BOOK", UsingJsonpath("$..title"))
            )
        )
    )

    with mock_server(imposter):
        response = requests.post(
            imposter.url,
            json={
                "books": [
                    {
                        "book": {
                            "title": "Game of Thrones",
                            "summary": "Dragons and political intrigue",
                        }
                    },
                    {"book": {"title": "Harry Potter", "summary": "Dragons and a boy wizard"}},
                    {"book": {"title": "The Hobbit", "summary": "A dragon and short people"}},
                ]
            },
        )

        assert_that(response, is_(response_with(body="Have you read Game of Thrones?")))
Ejemplo n.º 4
0
def test_or_predicate_and_body(mock_server):
    imposter = Imposter(
        Stub(
            Predicate(body="foo") | Predicate(body="bar"),
            Response(body="oranges")))

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        r1 = requests.get(imposter.url, data="foo")
        r2 = requests.get(imposter.url, data="bar")
        r3 = requests.get(imposter.url, data="baz")

        assert_that(r1, is_(response_with(status_code=200, body="oranges")))
        assert_that(r2, is_(response_with(status_code=200, body="oranges")))
        assert_that(r3, not_(response_with(status_code=200, body="oranges")))
Ejemplo n.º 5
0
def test_lookup(mock_server):
    datasource_path = str(
        Path("tests") / "integration" / "behaviors" / "test_data" /
        "values.csv")
    imposter = Imposter(
        Stub(responses=Response(
            status_code="${row}['code']",
            body=
            "Hello ${row}['Name'], have you done your ${row}['jobs'] today?",
            headers={"X-Tree": "${row}['tree']"},
            lookup=Lookup(Key("path", UsingRegex("/(.*)$"), 1),
                          datasource_path, "Name", "${row}"),
        )))

    with mock_server(imposter):
        response = requests.get(imposter.url / "liquid")

        assert_that(
            response,
            is_(
                response_with(
                    status_code=400,
                    body="Hello liquid, have you done your farmer today?",
                    headers=has_entry("X-Tree", "mango"),
                )),
        )
Ejemplo n.º 6
0
def test_regex_copy(mock_server):
    imposter = Imposter(
        Stub(
            responses=Response(
                status_code="${code}",
                headers={"X-Test": "${header}"},
                body="Hello, ${name}!",
                copy=[
                    Copy("path", "${code}", UsingRegex("\\d+")),
                    Copy({"headers": "X-Request"}, "${header}", UsingRegex(".+")),
                    Copy({"query": "name"}, "${name}", UsingRegex("AL\\w+", ignore_case=True)),
                ],
            )
        )
    )

    with mock_server(imposter):
        response = requests.get(
            imposter.url / str(456), params={"name": "Alice"}, headers={"X-REQUEST": "Header value"}
        )

        assert_that(
            response,
            is_(
                response_with(
                    status_code=456,
                    body="Hello, Alice!",
                    headers=has_entry("X-Test", "Header value"),
                )
            ),
        )
Ejemplo n.º 7
0
def test_body(mock_server):
    imposter = Imposter(Stub(responses=Response(body="sausages")))

    with mock_server(imposter):
        response = requests.get(imposter.url)

        assert_that(response, is_(response_with(body="sausages")))
Ejemplo n.º 8
0
def test_status(mock_server):
    imposter = Imposter(Stub(responses=Response(status_code=204)))

    with mock_server(imposter):
        response = requests.get(imposter.url)

        assert_that(response, is_(response_with(status_code=204)))
Ejemplo n.º 9
0
def test_proxy_in_stub(mock_server):
    imposter = Imposter(Stub(responses=Proxy(to="http://example.com")))

    with mock_server(imposter):
        response = requests.get("{0}/".format(imposter.url))

        assert_that(response, is_(response_with(status_code=200, body=has_title("Example Domain"))))
Ejemplo n.º 10
0
def test_multiple_imposters(mock_server):
    imposters = [
        Imposter(Stub(Predicate(path="/test1"), Response("sausages"))),
        Imposter([
            Stub([Predicate(path="/test2")],
                 [Response("chips", status_code=201)])
        ]),
    ]

    with mock_server(imposters) as s:
        logger.debug("server: %s", s)
        r1 = requests.get("{0}/test1".format(imposters[0].url))
        r2 = requests.get("{0}/test2".format(imposters[1].url))

    assert_that(r1, response_with(status_code=200, body="sausages"))
    assert_that(r2, response_with(status_code=201, body="chips"))
Ejemplo n.º 11
0
def test_multiple_stubs(mock_server):
    imposter = Imposter(
        [
            Stub(Predicate(path="/test1"), Response(body="sausages")),
            Stub(Predicate(path="/test2"), Response(body="chips")),
        ],
        port=4567,
        name="bill",
    )

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)
        r1 = requests.get("{0}/test1".format(imposter.url))
        r2 = requests.get("{0}/test2".format(imposter.url))

    assert_that(r1, response_with(body="sausages"))
    assert_that(r2, response_with(body="chips"))
Ejemplo n.º 12
0
def test_default_imposter(mock_server):
    imposter = Imposter(Stub())

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)
        r = requests.get("{0}/".format(imposter.url))

    assert_that(r, response_with(status_code=200, body=""))
Ejemplo n.º 13
0
def test_headers_predicate(mock_server):
    # Given
    imposter = Imposter(
        Stub(Predicate(headers={"foo": "bar"}), Response(body="oranges")))

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        # When
        r1 = requests.get(imposter.url, headers={"foo": "bar"})
        r2 = requests.get(imposter.url, headers={"foo": "baz"})
        r3 = requests.get(imposter.url)

        # Then
        assert_that(r1, is_(response_with(body="oranges")))
        assert_that(r2, is_(response_with(body=not_("oranges"))))
        assert_that(r3, is_(response_with(body=not_("oranges"))))
Ejemplo n.º 14
0
def test_proxy(mock_server):
    imposter = Imposter(Proxy(to="http://example.com"))

    with mock_server(imposter) as server:
        response = requests.get("{0}/".format(imposter.url))

        assert_that(response, is_(response_with(status_code=200, body=has_title("Example Domain"))))
        assert_that(server, had_request(path="/", method="GET"))
Ejemplo n.º 15
0
def test_decorate(mock_server):
    imposter = Imposter(Stub(responses=Response(body="The time is ${TIME}.", decorate=JS)))

    with mock_server(imposter):
        response = requests.get(imposter.url)

        assert_that(
            response, is_(response_with(body=matches_regexp(r"The time is \d\d:\d\d:\d\d\.")))
        )
Ejemplo n.º 16
0
def test_repeat(mock_server):
    # Given
    imposter = Imposter(
        Stub(Predicate(), [Response(body="oranges", repeat=2), Response(body="apples")])
    )

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        # When
        r1 = requests.get(imposter.url)
        r2 = requests.get(imposter.url)
        r3 = requests.get(imposter.url)

        # Then
        assert_that(r1, is_(response_with(body="oranges")))
        assert_that(r2, is_(response_with(body="oranges")))
        assert_that(r3, is_(response_with(body="apples")))
Ejemplo n.º 17
0
def test_binary_mode(mock_server):
    imposter = Imposter(
        Stub(responses=Response(mode=Response.Mode.BINARY,
                                body=b"c2F1c2FnZXM=")))

    with mock_server(imposter):
        response = requests.get(imposter.url)

        assert_that(response, is_(response_with(content=b"sausages")))
Ejemplo n.º 18
0
def test_xml_payload(mock_server):
    # Given
    imposter = Imposter(
        Stub(
            Predicate(xpath="//foo",
                      body="bar",
                      operator=Predicate.Operator.EQUALS),
            Response(body="sausages"),
        ))

    with mock_server(imposter):
        # When
        r1 = requests.get(imposter.url,
                          data=et2string(data2xml({"foo": "bar"})))
        r2 = requests.get(imposter.url,
                          data=et2string(data2xml({"foo": "baz"})))

        # Then
        assert_that(r1, is_(response_with(body="sausages")))
        assert_that(r2, is_(response_with(body=not_("sausages"))))
Ejemplo n.º 19
0
def test_json_response(mock_server):
    # Given
    imposter = Imposter(
        Stub(Predicate(), Response(body={"foo": ["bar", "baz"]})))

    with mock_server(imposter):
        # When
        r = requests.get(imposter.url)

        # Then
        assert_that(
            r, is_(response_with(body=json_matching({"foo": ["bar", "baz"]}))))
Ejemplo n.º 20
0
def test_xpath_copy(mock_server):
    imposter = Imposter(
        Stub(
            responses=Response(
                body="Have you read BOOK?", copy=Copy("body", "BOOK", UsingXpath("(//title)[2]"))
            )
        )
    )

    with mock_server(imposter):
        response = requests.post(imposter.url, data=BOOKS_XML)

        assert_that(response, is_(response_with(body="Have you read Harry Potter?")))
Ejemplo n.º 21
0
def test_xml_response(mock_server):
    # Given
    imposter = Imposter(
        Stub(Predicate(), Response(body=data2xml({"foo": {
            "bar": "baz"
        }}))))

    with mock_server(imposter):
        # When
        r = requests.get(imposter.url)

        # Then
        assert_that(r, is_(response_with(body="<foo><bar>baz</bar></foo>")))
Ejemplo n.º 22
0
def test_shell_transform(mock_server):
    imposter = Imposter(
        Stub(
            responses=Response(
                body="Hello ${name}!", shell_transform=('python -c "{0}" '.format(SCRIPT))
            )
        )
    )

    with mock_server(imposter):
        response = requests.post(imposter.url, json={"person": {"name": "Alice"}})

        assert_that(response, is_(response_with(body=json_matching({"person": {"name": "ALICE"}}))))
Ejemplo n.º 23
0
def test_and_predicate_and_query_strings(mock_server):
    imposter = Imposter(
        Stub(
            Predicate(query={"foo": "bar"})
            & Predicate(query={"dinner": "chips"}),
            Response(body="black pudding"),
        ))

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        r1 = requests.get("{0}/".format(imposter.url),
                          params={
                              "dinner": "chips",
                              "foo": "bar"
                          })
        r2 = requests.get("{0}/".format(imposter.url),
                          params={"dinner": "chips"})

        assert_that(r1,
                    is_(response_with(status_code=200, body="black pudding")))
        assert_that(r2,
                    not_(response_with(status_code=200, body="black pudding")))
Ejemplo n.º 24
0
def test_allow_multiple_servers_on_different_ports():
    # Given
    try:
        server1 = MountebankServer(port=2526)
        server2 = MountebankServer(port=2527)
        imposter1 = Imposter(
            Stub(Predicate(path="/test"), Response(body="sausages")))
        imposter2 = Imposter(
            Stub(Predicate(path="/test"), Response(body="bacon")))

        with server1(imposter1), server2(imposter2):

            response1 = requests.get("{0}/test".format(imposter1.url))
            response2 = requests.get("{0}/test".format(imposter2.url))

            assert_that(response1,
                        is_(response_with(status_code=200, body="sausages")))
            assert_that(response2,
                        is_(response_with(status_code=200, body="bacon")))

    finally:
        server1.close()
        server2.close()
Ejemplo n.º 25
0
def test_wait(mock_server):
    # Given
    imposter = Imposter(Stub(Predicate(), Response(body="oranges", wait=500)))

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        # When
        with Timer() as t:
            r = requests.get(imposter.url)

        # Then
        assert_that(r, is_(response_with(body="oranges")))
        assert_that(t.elapsed, close_to(0.5, 0.1))
Ejemplo n.º 26
0
def test_headers(mock_server):
    imposter = Imposter(
        Stub(responses=Response(
            headers={"X-Clacks-Overhead": "GNU Terry Pratchett"})))

    with mock_server(imposter):
        response = requests.get(imposter.url)

        assert_that(
            response,
            is_(
                response_with(headers=has_entry("X-Clacks-Overhead",
                                                "GNU Terry Pratchett"))),
        )
Ejemplo n.º 27
0
def test_methods(mock_server):
    # Given
    imposter = Imposter([
        Stub(Predicate(method=Predicate.Method.GET), Response(body="get")),
        Stub(Predicate(method=Predicate.Method.PUT), Response(body="put")),
        Stub(Predicate(method=Predicate.Method.POST), Response(body="post")),
        Stub(Predicate(method=Predicate.Method.DELETE),
             Response(body="delete")),
    ])

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        # When
        delete = requests.delete(imposter.url)
        post = requests.post(imposter.url)
        put = requests.put(imposter.url)
        get = requests.get(imposter.url)

        # Then
        assert_that(delete, is_(response_with(body="delete")))
        assert_that(post, is_(response_with(body="post")))
        assert_that(put, is_(response_with(body="put")))
        assert_that(get, is_(response_with(body="get")))
Ejemplo n.º 28
0
def test_request_to_mock_server(mock_server):
    # Start mock server with required behavior
    imposter = Imposter(
        Stub(Predicate(path="/test"), Response(body="sausages")))

    with mock_server(imposter) as server:
        # Make request to mock server
        response = requests.get("{0}/test".format(imposter.url))

        assert_that(
            "We got the expected response",
            response,
            is_(response_with(status_code=200, body="sausages")),
        )
        assert_that("The mock server recorded the request", server,
                    had_request(path="/test", method="GET"))
Ejemplo n.º 29
0
def test_xpath_copy_namespaced(mock_server):
    imposter = Imposter(
        Stub(
            responses=Response(
                body="Have you read BOOK?",
                copy=Copy(
                    "body",
                    "BOOK",
                    UsingXpath(
                        "//isbn:title", ns={"isbn": "http://schemas.isbn.org/ns/1999/basic.dtd"}
                    ),
                ),
            )
        )
    )

    with mock_server(imposter):
        response = requests.post(imposter.url, data=BOOKS_XML_NAMESPACED)

        assert_that(response, is_(response_with(body="Have you read Game of Thrones?")))