99import org .apache .http .impl .client .BasicCredentialsProvider ;
1010import org .apache .http .impl .client .CloseableHttpClient ;
1111import org .apache .http .impl .client .HttpClientBuilder ;
12- import org .junit .After ;
1312import org .junit .Test ;
1413
1514import java .io .IOException ;
1615import java .io .InputStream ;
1716
1817public class HttpClientSandboxLiveTest {
1918
20- private CloseableHttpClient client ;
19+ @ Test
20+ public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode () throws IOException {
21+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
22+ final AuthScope authscp = new AuthScope ("localhost" , 8080 );
23+ credentialsProvider .setCredentials (authscp , new UsernamePasswordCredentials ("user1" , "user1Pass" ));
2124
22- private CloseableHttpResponse response ;
25+ CloseableHttpClient client = HttpClientBuilder . create (). setDefaultCredentialsProvider ( credentialsProvider ). build () ;
2326
24- @ After
25- public final void after () throws IllegalStateException , IOException {
26- if (response == null ) {
27- return ;
28- }
27+ final HttpGet httpGet = new HttpGet ("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1" );
28+ CloseableHttpResponse response = client .execute (httpGet );
29+
30+ System .out .println (response .getStatusLine ());
2931
3032 try {
3133 final HttpEntity entity = response .getEntity ();
@@ -38,22 +40,4 @@ public final void after() throws IllegalStateException, IOException {
3840 response .close ();
3941 }
4042 }
41-
42- // tests
43-
44- // simple request - response
45-
46- @ Test
47- public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode () throws IOException {
48- final CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
49- final AuthScope authscp = new AuthScope ("localhost" , 8080 );
50- credentialsProvider .setCredentials (authscp , new UsernamePasswordCredentials ("user1" , "user1Pass" ));
51-
52- client = HttpClientBuilder .create ().setDefaultCredentialsProvider (credentialsProvider ).build ();
53-
54- final HttpGet httpGet = new HttpGet ("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1" );
55- response = client .execute (httpGet );
56-
57- System .out .println (response .getStatusLine ());
58- }
5943}
0 commit comments