GoConqr has a new look! Let us know what you think.

Notes

mock servers

Moran Abelev English 3 pages

Content of the notes (3)

Page 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); } }

Page 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); } }

Page 3

create mock server which would know how to send and recieve requests from host, in specific port.

Information

Description
Note on mock servers, created by Moran Abelev on 31/08/2017.
Created by
Moran Abelev
Views
0
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?