additional examples
This commit is contained in:
parent
3dcf53244c
commit
c0cc59ee48
@ -0,0 +1,72 @@
|
||||
package org.baeldung.httpclient;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HttpClientAuthLiveTest {
|
||||
|
||||
private CloseableHttpClient instance;
|
||||
|
||||
private CloseableHttpResponse response;
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
instance = HttpClientBuilder.create().build();
|
||||
}
|
||||
|
||||
@After
|
||||
public final void after() throws IllegalStateException, IOException {
|
||||
if (response == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
final InputStream instream = entity.getContent();
|
||||
instream.close();
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
// tests
|
||||
|
||||
// simple request - response
|
||||
|
||||
@Test
|
||||
public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException {
|
||||
final CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
final AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
|
||||
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "user1Pass");
|
||||
|
||||
provider.setCredentials(scope, credentials);
|
||||
|
||||
instance = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
|
||||
|
||||
response = instance.execute(new HttpGet("http://localhost:8080/spring-security-mvc-basic-auth/homepage.html"));
|
||||
|
||||
final int statusCode = response.getStatusLine().getStatusCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.jackson.try1;
|
||||
package org.baeldung.jackson.sandbox;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
@ -13,7 +13,7 @@ import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JacksonTryUnitTest {
|
||||
public class JacksonPrettyPrintUnitTest {
|
||||
|
||||
@Test
|
||||
public final void whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
Loading…
x
Reference in New Issue
Block a user