Skip to content

Commit bfddf33

Browse files
author
Grzegorz Piwowarek
committed
Refactor LiveTest
1 parent d0a376e commit bfddf33

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

httpclient/src/test/java/org/baeldung/httpclient/base/HttpClientSandboxLiveTest.java

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@
99
import org.apache.http.impl.client.BasicCredentialsProvider;
1010
import org.apache.http.impl.client.CloseableHttpClient;
1111
import org.apache.http.impl.client.HttpClientBuilder;
12-
import org.junit.After;
1312
import org.junit.Test;
1413

1514
import java.io.IOException;
1615
import java.io.InputStream;
1716

1817
public 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

Comments
 (0)