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_encode_binary(): # Given somebin = b"dfgddzfbv" # When actual = json.dumps({"somebin": somebin}, cls=ExtendedJSONEncoder) # Then assert_that(actual, json_matching(has_entries(somebin="dfgddzfbv")))
def test_encode_datetime(): # Given somedatetime = datetime.datetime(1968, 7, 21, 4, 4, 0) # When actual = json.dumps({"somedatetime": somedatetime}, cls=ExtendedJSONEncoder) # Then assert_that(actual, json_matching(has_entries(somedatetime="1968-07-21T04:04:00")))
def test_json_body(mock_server): imposter = Imposter(Stub(responses=Response(body={"a": "b", "c": "d"}))) with mock_server(imposter): response = requests.get(imposter.url) assert_that( response, is_response().with_body(json_matching(has_entries(a="b", c="d"))))
def test_encode_binary(): # Given somebin = b"dfgddzfbv" # When actual = json.dumps({"somebin": somebin}, cls=ExtendedJSONEncoder) # Then assert_that(actual, json_matching(has_entries(somebin="dfgddzfbv")))
def test_encode_date_and_builtins(): # Given somestring = "sausages" someint = 99 somedate = datetime.date(1968, 7, 21) # When actual = json.dumps({"somedate": somedate, "somestring": somestring, "someint": someint}, cls=ExtendedJSONEncoder) # Then assert_that(actual, json_matching(has_entries(somestring=somestring, someint=someint, somedate="1968-07-21")))
def test_encode_datetime(): # Given somedatetime = datetime.datetime(1968, 7, 21, 4, 4, 0) # When actual = json.dumps({"somedatetime": somedatetime}, cls=ExtendedJSONEncoder) # Then assert_that(actual, json_matching(has_entries(somedatetime="1968-07-21T04:04:00")))
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"]}))))
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"}})))
def test_enum(): # Given class Colour(Enum): RED = 1 GREEN = 2 BLUE = 3 someenumvalue = Colour.GREEN # When actual = json.dumps({"someenumvalue": someenumvalue}, cls=ExtendedJSONEncoder) # Then assert_that(actual, json_matching(has_entries(someenumvalue="Colour.GREEN")))
def test_enum(): # Given class Colour(Enum): RED = 1 GREEN = 2 BLUE = 3 someenumvalue = Colour.GREEN # When actual = json.dumps({"someenumvalue": someenumvalue}, cls=ExtendedJSONEncoder) # Then assert_that(actual, json_matching(has_entries(someenumvalue="Colour.GREEN")))
def test_build_imposter_from_structure_on_disk(mock_server): # Given structure_path = Path("tests") / "integration" / "test_data" / "impostor_structure.json" # When with structure_path.open() as f: structure = json.load(f) imposter = Imposter.from_structure(structure["imposters"][0]) with mock_server(imposter): response = requests.get(f"{imposter.url}/tutorial") # Then assert_that( response, is_response().with_status_code(200).and_body(json_matching(has_entries(message="success"))), )
def test_encode_date_and_builtins(): # Given somestring = "sausages" someint = 99 somedate = datetime.date(1968, 7, 21) # When actual = json.dumps( { "somedate": somedate, "somestring": somestring, "someint": someint }, cls=ExtendedJSONEncoder, ) # Then assert_that( actual, json_matching( has_entries(somestring=somestring, someint=someint, somedate="1968-07-21")), )
def test_json_matching(): # Given j = json.dumps([1, 2, 3]) # When # Then assert_that(j, json_matching([1, 2, 3])) assert_that(j, json_matching(contains_exactly(1, 2, 3))) assert_that(j, not_(json_matching([1, 2, 5]))) assert_that(json_matching([1, 2, 3]), has_string("JSON structure matching <[1, 2, 3]>")) assert_that( json_matching([]), mismatches_with( "WTF is this?", matches_regexp(r"Got invalid JSON ['<]WTF is this\?['>]")), ) assert_that(json_matching([]), mismatches_with("[1]", "was <[1]>"))