|
| 1 | +package org.baeldung.client; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.apache.http.HttpResponse; |
| 6 | +import org.apache.http.auth.AuthScope; |
| 7 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 8 | +import org.apache.http.client.ClientProtocolException; |
| 9 | +import org.apache.http.client.methods.HttpGet; |
| 10 | +import org.apache.http.impl.client.DefaultHttpClient; |
| 11 | +import org.baeldung.client.spring.ClientConfig; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 16 | +import org.springframework.test.context.ContextConfiguration; |
| 17 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 18 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 19 | +import org.springframework.web.client.RestTemplate; |
| 20 | + |
| 21 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 22 | +@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class) |
| 23 | +public class RawClientLiveTest { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private RestTemplate restTemplate; |
| 27 | + |
| 28 | + // tests |
| 29 | + |
| 30 | + @Test |
| 31 | + public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException { |
| 32 | + final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory(); |
| 33 | + final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient(); |
| 34 | + httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user", "userPass")); |
| 35 | + |
| 36 | + final HttpResponse response = httpClient.execute(new HttpGet("http://localhost:8080/spring-security-rest-template/api/foos/1")); |
| 37 | + final int statusCode = response.getStatusLine().getStatusCode(); |
| 38 | + System.out.println(statusCode); |
| 39 | + } |
| 40 | +} |
0 commit comments