|
| 1 | +package org.baeldung.httpclient; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.equalTo; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | + |
| 9 | +import org.apache.http.HttpEntity; |
| 10 | +import org.apache.http.client.ClientProtocolException; |
| 11 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 12 | +import org.apache.http.client.methods.HttpGet; |
| 13 | +import org.apache.http.client.protocol.HttpClientContext; |
| 14 | +import org.apache.http.impl.client.BasicCookieStore; |
| 15 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 16 | +import org.apache.http.impl.client.DefaultHttpClient; |
| 17 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 18 | +import org.apache.http.impl.cookie.BasicClientCookie; |
| 19 | +import org.apache.http.protocol.BasicHttpContext; |
| 20 | +import org.apache.http.protocol.HttpContext; |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +public class HttpClientCookieLiveTest { |
| 26 | + |
| 27 | + private CloseableHttpClient instance; |
| 28 | + |
| 29 | + private CloseableHttpResponse response; |
| 30 | + |
| 31 | + @Before |
| 32 | + public final void before() { |
| 33 | + instance = HttpClientBuilder.create().build(); |
| 34 | + } |
| 35 | + |
| 36 | + @After |
| 37 | + public final void after() throws IllegalStateException, IOException { |
| 38 | + if (response == null) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + final HttpEntity entity = response.getEntity(); |
| 44 | + if (entity != null) { |
| 45 | + final InputStream instream = entity.getContent(); |
| 46 | + instream.close(); |
| 47 | + } |
| 48 | + } finally { |
| 49 | + response.close(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // tests |
| 54 | + |
| 55 | + @Test |
| 56 | + public final void whenSettingCookiesOnARequest_thenCorrect() throws ClientProtocolException, IOException { |
| 57 | + instance = HttpClientBuilder.create().build(); |
| 58 | + final HttpGet request = new HttpGet("http://www.github.com"); |
| 59 | + request.setHeader("Cookie", "JSESSIONID=1234"); |
| 60 | + |
| 61 | + response = instance.execute(request); |
| 62 | + |
| 63 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 64 | + } |
| 65 | + |
| 66 | + @SuppressWarnings("deprecation") |
| 67 | + @Test |
| 68 | + public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws ClientProtocolException, IOException { |
| 69 | + final BasicCookieStore cookieStore = new BasicCookieStore(); |
| 70 | + final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234"); |
| 71 | + cookie.setDomain(".github.com"); |
| 72 | + cookieStore.addCookie(cookie); |
| 73 | + final DefaultHttpClient client = new DefaultHttpClient(); |
| 74 | + client.setCookieStore(cookieStore); |
| 75 | + |
| 76 | + final HttpGet request = new HttpGet("http://www.github.com"); |
| 77 | + |
| 78 | + response = client.execute(request); |
| 79 | + |
| 80 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() throws ClientProtocolException, IOException { |
| 85 | + final BasicCookieStore cookieStore = new BasicCookieStore(); |
| 86 | + final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234"); |
| 87 | + cookie.setDomain(".github.com"); |
| 88 | + cookieStore.addCookie(cookie); |
| 89 | + instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build(); |
| 90 | + |
| 91 | + final HttpGet request = new HttpGet("http://www.github.com"); |
| 92 | + |
| 93 | + response = instance.execute(request); |
| 94 | + |
| 95 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws ClientProtocolException, IOException { |
| 100 | + final BasicCookieStore cookieStore = new BasicCookieStore(); |
| 101 | + final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234"); |
| 102 | + cookie.setDomain(".github.com"); |
| 103 | + cookieStore.addCookie(cookie); |
| 104 | + instance = HttpClientBuilder.create().build(); |
| 105 | + |
| 106 | + final HttpGet request = new HttpGet("http://www.github.com"); |
| 107 | + |
| 108 | + final HttpContext localContext = new BasicHttpContext(); |
| 109 | + localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); |
| 110 | + response = instance.execute(request, localContext); |
| 111 | + |
| 112 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments