Apuntes
mock servers
Contenido del apunte (3)
Página 1
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = TestSpringBootApplication.class,properties = {"enable=security:true"}) @EnableWebSecurity @Log public class TestRestTemplate { //TODO: write to log public static final String TEST_CASE_ID = "PlanckUtils " + "RestTemplateWithCredentials"; private String user = "foo"; private String pass = "bar"; private UsersConfig mockUsersConfig; private RestTemplateWithCredentials internalRestTemplate; MockRestServiceServer mockServer; @Test public void testRestTemplate() { mockUsersConfig = new UsersConfig(); mockUsersConfig.setUserUser(user); mockUsersConfig.setUserPassword(pass); internalRestTemplate = new RestTemplateWithCredentials(mockUsersConfig); mockServer = MockRestServiceServer.bindTo(internalRestTemplate).build(); String authString = user + ":" + pass; mockServer.expect(requestTo("http://google.com")) .andExpect(method(HttpMethod.GET)) .andExpect(header("Authorization", "Basic " + Base64.encode(authString.getBytes()))) .andRespond(withSuccess()); String httpResult = internalRestTemplate.getForObject("http://google.com", String.class); } }
Página 2
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = TestSpringBootApplication.class,properties = {"enable=security:true"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @Log public class TestRestTemplateWithCredentials { public static final String TEST_CASE_ID = "PlanckUtils " + "RestTemplateWithCredentials"; private ClientAndProxy proxy; private ClientAndServer mockServer; @LocalServerPort private int port; private String user = "foo"; private String pass = "bar"; private UsersConfig mockUsersConfig; private RestTemplateWithCredentials internalRestTemplate; @Test public void testRestTemplate() { mockUsersConfig = new UsersConfig(); mockUsersConfig.setUserUser(user); mockUsersConfig.setUserPassword(pass); internalRestTemplate = new RestTemplateWithCredentials(mockUsersConfig); String authString = user + ":" + pass; new MockServerClient("localhost", port).verify( request() .withMethod("GET") .withPath("http://google.com") .withHeader("Authorization", "Basic" + Base64.encode(authString.getBytes()))); String httpResult = internalRestTemplate.getForObject("http://google.com", String.class); } }
Página 3
create mock server which would know how to send and recieve requests from host, in specific port.
Información
- Descripción
- Apunte sobre mock servers, creado por Moran Abelev el 31/08/2017.
- Creado por
- Moran Abelev
- Visitas
- 0
- Valoración