def test_structure_inject(): expected_imposter = Imposter( Stub(responses=InjectionResponse(inject="function (request) {\n}")), port=4546) imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.port == expected_imposter.port
def query_all_imposters(self) -> Iterator[Imposter]: """Yield all imposters running on the server, including those defined elsewhere.""" server_info = requests.get(self.server_url) imposters = server_info.json()["imposters"] for imposter in imposters: yield Imposter.from_structure( requests.get(imposter["_links"]["self"]["href"]).json())
def test_imposter_structure_roundtrip(): # Given expected = ImposterBuilder().build() structure = expected.as_structure() # When actual = Imposter.from_structure(structure) # Then assert_that(actual, instance_of(Imposter)) assert_that(actual, has_identical_properties_to(expected))
def query_all_imposters(self) -> Sequence[Imposter]: """Yield all imposters running on the server, including those defined elsewhere.""" server_info = requests.get(self.server_url) imposters_structure = server_info.json()["imposters"] all_imposters: MutableSequence[Imposter] = [] for imposter_structure in imposters_structure: impostor_url = imposter_structure["_links"]["self"]["href"] imposter = Imposter.from_structure(requests.get(impostor_url).json()) imposter.host = self.host imposter.server_url = self.server_url all_imposters.append(imposter) return sorted(all_imposters, key=attrgetter("port"))
def test_imposter_complex_predicates(predicate): # Given expected = Imposter(Stub(predicate().build())) structure = expected.as_structure() # When actual = Imposter.from_structure(structure) # Then assert_that(actual, instance_of(Imposter)) assert_that( actual, has_identical_properties_to(expected, ignoring=["configuration_url"]))
def test_imposter_structure_without_default_response_roundtrip(): # Given expected = ImposterBuilder().with_default_response(None).build() structure = expected.as_structure() # When actual = Imposter.from_structure(structure) # Then assert_that(actual, instance_of(Imposter)) assert_that( actual, has_identical_properties_to(expected, ignoring=["configuration_url"]))
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_structure_no_record_requests(): expected_imposter = Imposter(Stub()) imposter_structure = expected_imposter.as_structure() del imposter_structure["recordRequests"] imposter = Imposter.from_structure(imposter_structure) assert imposter.record_requests == expected_imposter.record_requests
def test_structure_record_requests(): expected_imposter = Imposter(Stub(), record_requests=False) imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.record_requests == expected_imposter.record_requests
def test_structure_name(): expected_imposter = Imposter(Stub(), name="darwin") imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.name == expected_imposter.name
def test_structure_no_protocol(): expected_imposter = Imposter(Stub()) imposter_structure = expected_imposter.as_structure() del imposter_structure["protocol"] imposter = Imposter.from_structure(imposter_structure) assert imposter.protocol == expected_imposter.protocol
def test_structure_protocol(): expected_imposter = Imposter(Stub(), protocol="http") imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.protocol == expected_imposter.protocol
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_response_port(): expected_imposter = Imposter(Stub(responses=Response()), port=4546) imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.port == expected_imposter.port
def test_structure_no_port(): expected_imposter = Imposter(Stub()) imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.port == expected_imposter.port
def test_structure_port(): expected_imposter = ImposterBuilder().with_port(4546).build() imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.port == 4546
def test_structure_protocol(): expected_imposter = ImposterBuilder().with_protocol("http").build() imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.protocol == Imposter.Protocol.HTTP
def test_structure_record_requests(): expected_imposter = ImposterBuilder().with_record_requests(False).build() imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.record_requests is False
def test_structure_name(): expected_imposter = ImposterBuilder().with_name("darwin").build() imposter_structure = expected_imposter.as_structure() imposter = Imposter.from_structure(imposter_structure) assert imposter.name == "darwin"