def test_structure_inject_headers(): expected_proxy = Proxy( "http://darwin.dog", inject_headers={"X-Clacks-Overhead": "GNU Terry Pratchett"}) proxy_structure = expected_proxy.as_structure() proxy = Proxy.from_structure(proxy_structure) assert proxy.inject_headers == expected_proxy.inject_headers
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"))))
def test_proxy_uses_path_predicate_generator(mock_server): proxy_imposter = Imposter( Stub(responses=Proxy( to="https://httpbin.org", mode=Proxy.Mode.ONCE, predicate_generators=[PredicateGenerator(path=True)], ))) with mock_server(proxy_imposter): response = requests.get(proxy_imposter.url / "status/418") assert_that( response, is_response().with_status_code(418).and_body( contains_string("teapot"))) response = requests.get(proxy_imposter.url / "status/200") assert_that(response, is_response().with_status_code(200)) recorded_stubs = proxy_imposter.playback() playback_impostor = Imposter(recorded_stubs) with mock_server(playback_impostor): response = requests.get(playback_impostor.url / "status/418") assert_that( response, is_response().with_status_code(418).and_body( contains_string("teapot"))) response = requests.get(playback_impostor.url / "status/200") assert_that(response, is_response().with_status_code(200))
def test_proxy_uses_query_predicate_generator_with_key(mock_server): proxy_imposter = Imposter( Stub(responses=Proxy( to="https://httpbin.org", mode=Proxy.Mode.ONCE, predicate_generators=[ PredicateGenerator(query={"foo": "whatever"}) ], ))) with mock_server(proxy_imposter): response = requests.get(proxy_imposter.url / "get", params={ "foo": "bar", "quxx": "buzz" }) assert_that( response, is_response().with_body( json_matching( has_entries(args=has_entries(foo="bar", quxx="buzz")))), ) response = requests.get(proxy_imposter.url / "get", params={ "foo": "baz", "quxx": "buxx" }) assert_that( response, is_response().with_body( json_matching(has_entries(args=has_entries(foo="baz")))), ) recorded_stubs = proxy_imposter.playback() playback_impostor = Imposter(recorded_stubs) with mock_server(playback_impostor): response = requests.get(playback_impostor.url / "get", params={ "foo": "bar", "quxx": "whatever" }) assert_that( response, is_response().with_body( json_matching( has_entries(args=has_entries(foo="bar", quxx="buzz")))), ) response = requests.get(playback_impostor.url / "get", params={ "foo": "baz", "quxx": "anything" }) assert_that( response, is_response().with_body( json_matching( has_entries(args=has_entries(foo="baz", quxx="buxx")))), )
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"))
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.
def test_decorate_proxy(mock_server): proxy_target = Imposter(Stub(responses=Response(body="Hello ${NAME}."))) mock_server.add_impostor(proxy_target) imposter = Imposter(Stub(responses=Proxy(to=proxy_target.url, decorate=JS))) with mock_server(imposter): response = requests.get(imposter.url) assert_that(response, is_response().with_body("Hello World."))
def test_proxy_without_stub(mock_server): imposter = Imposter(Proxy(to="http://example.com")) with mock_server(imposter): response = requests.get(imposter.url) assert_that( response, is_response().with_status_code(200).and_body( has_title("Example Domain")))
def test_proxy(mock_server): imposter = Imposter(Stub(responses=Proxy(to="http://example.com"))) with mock_server(imposter): response = requests.get(imposter.url) assert_that( response, is_response().with_status_code(200).and_body( has_title("Example Domain"))) assert_that(imposter, had_request().with_path("/").and_method("GET"))
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))
def test_decorate_proxy_binary(mock_server): proxy_target = Imposter( Stub( responses=Response( headers={"Content-Type": "application/octet-stream"}, body="Hello ${NAME}." ) ) ) mock_server.add_impostor(proxy_target) imposter = Imposter(Stub(responses=Proxy(to=proxy_target.url, decorate=JS_FOR_BINARY))) with mock_server(imposter): response = requests.get(imposter.url) assert_that(response, is_response().with_body("Hello World."))
def test_inject_headers(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, inject_headers={"X-Clacks-Overhead": "GNU Terry Pratchett"}, ))) server.add_imposters(proxy_imposter) requests.get(proxy_imposter.url / "test") assert_that( target_imposter, had_request().with_path("/test").and_headers( has_entry("X-Clacks-Overhead", "GNU Terry Pratchett")), )
def test_structure_proxy_port(): expected_imposter = Imposter(Stub(responses=Proxy("http://darwin.dog")), port=4546) imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.port == expected_imposter.port
def test_structure_wait(): expected_proxy = Proxy("http://darwin.dog", wait=200) proxy_structure = expected_proxy.as_structure() proxy = Proxy.from_structure(proxy_structure) assert proxy.wait == expected_proxy.wait
def test_structure_to(): expected_proxy = Proxy("http://darwin.dog") proxy_structure = expected_proxy.as_structure() proxy = Proxy.from_structure(proxy_structure) assert proxy.to == expected_proxy.to