HTTPCLIENT-1032: additional unit test coverage for HttpCacheEntry
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1044636 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f5a01c6cb2
commit
47a66573e9
|
@ -27,112 +27,279 @@
|
||||||
package org.apache.http.client.cache;
|
package org.apache.http.client.cache;
|
||||||
|
|
||||||
import static org.easymock.classextension.EasyMock.*;
|
import static org.easymock.classextension.EasyMock.*;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
import org.apache.http.HttpVersion;
|
import org.apache.http.HttpVersion;
|
||||||
import org.apache.http.StatusLine;
|
import org.apache.http.StatusLine;
|
||||||
import org.apache.http.client.cache.HttpCacheEntry;
|
import org.apache.http.client.cache.HttpCacheEntry;
|
||||||
|
import org.apache.http.impl.cookie.DateUtils;
|
||||||
import org.apache.http.message.BasicHeader;
|
import org.apache.http.message.BasicHeader;
|
||||||
import org.apache.http.message.BasicStatusLine;
|
import org.apache.http.message.BasicStatusLine;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class TestHttpCacheEntry {
|
public class TestHttpCacheEntry {
|
||||||
|
|
||||||
private Date now;
|
private Date now;
|
||||||
private Date elevenSecondsAgo;
|
private Date elevenSecondsAgo;
|
||||||
private Date nineSecondsAgo;
|
private Date nineSecondsAgo;
|
||||||
private Resource mockResource;
|
private Resource mockResource;
|
||||||
private StatusLine statusLine;
|
private StatusLine statusLine;
|
||||||
|
private HttpCacheEntry entry;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
now = new Date();
|
now = new Date();
|
||||||
elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
|
elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
|
||||||
nineSecondsAgo = new Date(now.getTime() - 9 * 1000L);
|
nineSecondsAgo = new Date(now.getTime() - 9 * 1000L);
|
||||||
statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1,
|
statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1,
|
||||||
HttpStatus.SC_OK, "OK");
|
HttpStatus.SC_OK, "OK");
|
||||||
mockResource = createMock(Resource.class);
|
mockResource = createMock(Resource.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpCacheEntry makeEntry(Header[] headers) {
|
private HttpCacheEntry makeEntry(Header[] headers) {
|
||||||
return new HttpCacheEntry(elevenSecondsAgo, nineSecondsAgo,
|
return new HttpCacheEntry(elevenSecondsAgo, nineSecondsAgo,
|
||||||
statusLine, headers, mockResource, null);
|
statusLine, headers, mockResource, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetHeadersReturnsCorrectHeaders() {
|
public void testGetHeadersReturnsCorrectHeaders() {
|
||||||
Header[] headers = { new BasicHeader("foo", "fooValue"),
|
Header[] headers = { new BasicHeader("foo", "fooValue"),
|
||||||
new BasicHeader("bar", "barValue1"),
|
new BasicHeader("bar", "barValue1"),
|
||||||
new BasicHeader("bar", "barValue2")
|
new BasicHeader("bar", "barValue2")
|
||||||
};
|
};
|
||||||
HttpCacheEntry entry = makeEntry(headers);
|
entry = makeEntry(headers);
|
||||||
Assert.assertEquals(2, entry.getHeaders("bar").length);
|
assertEquals(2, entry.getHeaders("bar").length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFirstHeaderReturnsCorrectHeader() {
|
public void testGetFirstHeaderReturnsCorrectHeader() {
|
||||||
Header[] headers = { new BasicHeader("foo", "fooValue"),
|
Header[] headers = { new BasicHeader("foo", "fooValue"),
|
||||||
new BasicHeader("bar", "barValue1"),
|
new BasicHeader("bar", "barValue1"),
|
||||||
new BasicHeader("bar", "barValue2")
|
new BasicHeader("bar", "barValue2")
|
||||||
};
|
};
|
||||||
HttpCacheEntry entry = makeEntry(headers);
|
entry = makeEntry(headers);
|
||||||
Assert.assertEquals("barValue1", entry.getFirstHeader("bar").getValue());
|
assertEquals("barValue1", entry.getFirstHeader("bar").getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetHeadersReturnsEmptyArrayIfNoneMatch() {
|
public void testGetHeadersReturnsEmptyArrayIfNoneMatch() {
|
||||||
Header[] headers = { new BasicHeader("foo", "fooValue"),
|
Header[] headers = { new BasicHeader("foo", "fooValue"),
|
||||||
new BasicHeader("bar", "barValue1"),
|
new BasicHeader("bar", "barValue1"),
|
||||||
new BasicHeader("bar", "barValue2")
|
new BasicHeader("bar", "barValue2")
|
||||||
};
|
};
|
||||||
HttpCacheEntry entry = makeEntry(headers);
|
entry = makeEntry(headers);
|
||||||
|
assertEquals(0, entry.getHeaders("baz").length);
|
||||||
|
}
|
||||||
|
|
||||||
Assert.assertEquals(0, entry.getHeaders("baz").length);
|
@Test
|
||||||
}
|
public void testGetFirstHeaderReturnsNullIfNoneMatch() {
|
||||||
|
Header[] headers = { new BasicHeader("foo", "fooValue"),
|
||||||
|
new BasicHeader("bar", "barValue1"),
|
||||||
|
new BasicHeader("bar", "barValue2")
|
||||||
|
};
|
||||||
|
entry = makeEntry(headers);
|
||||||
|
assertEquals(null, entry.getFirstHeader("quux"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFirstHeaderReturnsNullIfNoneMatch() {
|
public void testCacheEntryWithNoVaryHeaderDoesNotHaveVariants() {
|
||||||
Header[] headers = { new BasicHeader("foo", "fooValue"),
|
Header[] headers = new Header[0];
|
||||||
new BasicHeader("bar", "barValue1"),
|
entry = makeEntry(headers);
|
||||||
new BasicHeader("bar", "barValue2")
|
assertFalse(entry.hasVariants());
|
||||||
};
|
}
|
||||||
HttpCacheEntry entry = makeEntry(headers);
|
|
||||||
|
|
||||||
Assert.assertEquals(null, entry.getFirstHeader("quux"));
|
@Test
|
||||||
}
|
public void testCacheEntryWithOneVaryHeaderHasVariants() {
|
||||||
|
Header[] headers = { new BasicHeader("Vary", "User-Agent") };
|
||||||
|
entry = makeEntry(headers);
|
||||||
|
assertTrue(entry.hasVariants());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCacheEntryWithNoVaryHeaderDoesNotHaveVariants() {
|
public void testCacheEntryWithMultipleVaryHeadersHasVariants() {
|
||||||
Header[] headers = new Header[0];
|
Header[] headers = { new BasicHeader("Vary", "User-Agent"),
|
||||||
HttpCacheEntry entry = makeEntry(headers);
|
new BasicHeader("Vary", "Accept-Encoding")
|
||||||
Assert.assertFalse(entry.hasVariants());
|
};
|
||||||
}
|
entry = makeEntry(headers);
|
||||||
|
assertTrue(entry.hasVariants());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCacheEntryWithOneVaryHeaderHasVariants() {
|
public void testCacheEntryWithVaryStarHasVariants(){
|
||||||
Header[] headers = { new BasicHeader("Vary", "User-Agent") };
|
Header[] headers = { new BasicHeader("Vary", "*") };
|
||||||
HttpCacheEntry entry = makeEntry(headers);
|
entry = makeEntry(headers);
|
||||||
Assert.assertTrue(entry.hasVariants());
|
assertTrue(entry.hasVariants());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCacheEntryWithMultipleVaryHeadersHasVariants() {
|
public void mustProvideRequestDate() {
|
||||||
Header[] headers = { new BasicHeader("Vary", "User-Agent"),
|
try {
|
||||||
new BasicHeader("Vary", "Accept-Encoding")
|
new HttpCacheEntry(null, new Date(), statusLine,
|
||||||
};
|
new Header[]{}, mockResource, null);
|
||||||
HttpCacheEntry entry = makeEntry(headers);
|
fail("Should have thrown exception");
|
||||||
Assert.assertTrue(entry.hasVariants());
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCacheEntryWithVaryStarHasVariants(){
|
public void mustProvideResponseDate() {
|
||||||
Header[] headers = { new BasicHeader("Vary", "*") };
|
try {
|
||||||
HttpCacheEntry entry = makeEntry(headers);
|
new HttpCacheEntry(new Date(), null, statusLine,
|
||||||
Assert.assertTrue(entry.hasVariants());
|
new Header[]{}, mockResource, null);
|
||||||
}
|
fail("Should have thrown exception");
|
||||||
|
} catch (IllegalArgumentException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustProvideStatusLine() {
|
||||||
|
try {
|
||||||
|
new HttpCacheEntry(new Date(), new Date(), null,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
fail("Should have thrown exception");
|
||||||
|
} catch (IllegalArgumentException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustProvideResponseHeaders() {
|
||||||
|
try {
|
||||||
|
new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
null, mockResource, null);
|
||||||
|
fail("Should have thrown exception");
|
||||||
|
} catch (IllegalArgumentException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustProvideResource() {
|
||||||
|
try {
|
||||||
|
new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, null, null);
|
||||||
|
fail("Should have thrown exception");
|
||||||
|
} catch (IllegalArgumentException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canProvideVariantSet() {
|
||||||
|
new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, new HashSet<String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canRetrieveOriginalStatusLine() {
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
assertSame(statusLine, entry.getStatusLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void protocolVersionComesFromOriginalStatusLine() {
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
assertSame(statusLine.getProtocolVersion(),
|
||||||
|
entry.getProtocolVersion());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void reasonPhraseComesFromOriginalStatusLine() {
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
assertSame(statusLine.getReasonPhrase(), entry.getReasonPhrase());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void statusCodeComesFromOriginalStatusLine() {
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
assertEquals(statusLine.getStatusCode(), entry.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canGetOriginalRequestDate() {
|
||||||
|
final Date requestDate = new Date();
|
||||||
|
entry = new HttpCacheEntry(requestDate, new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
assertSame(requestDate, entry.getRequestDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canGetOriginalResponseDate() {
|
||||||
|
final Date responseDate = new Date();
|
||||||
|
entry = new HttpCacheEntry(new Date(), responseDate, statusLine,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
assertSame(responseDate, entry.getResponseDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canGetOriginalResource() {
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
assertSame(mockResource, entry.getResource());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canGetOriginalHeaders() {
|
||||||
|
Header[] headers = {
|
||||||
|
new BasicHeader("Server", "MockServer/1.0"),
|
||||||
|
new BasicHeader("Date", DateUtils.formatDate(now))
|
||||||
|
};
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
headers, mockResource, null);
|
||||||
|
Header[] result = entry.getAllHeaders();
|
||||||
|
assertEquals(headers.length, result.length);
|
||||||
|
for(int i=0; i<headers.length; i++) {
|
||||||
|
assertEquals(headers[i], result[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canRetrieveOriginalVariantSet() {
|
||||||
|
Set<String> variants = new HashSet<String>();
|
||||||
|
variants.add("variant1");
|
||||||
|
variants.add("variant2");
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, variants);
|
||||||
|
Set<String> result = entry.getVariantURIs();
|
||||||
|
assertEquals(variants.size(), result.size());
|
||||||
|
for(String variant : variants) {
|
||||||
|
assertTrue(result.contains(variant));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void variantSetIsNotModifiable() {
|
||||||
|
Set<String> variants = new HashSet<String>();
|
||||||
|
variants.add("variant1");
|
||||||
|
variants.add("variant2");
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, variants);
|
||||||
|
Set<String> result = entry.getVariantURIs();
|
||||||
|
try {
|
||||||
|
result.remove("variant1");
|
||||||
|
fail("Should have thrown exception");
|
||||||
|
} catch (UnsupportedOperationException expected) {
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
result.add("variant3");
|
||||||
|
fail("Should have thrown exception");
|
||||||
|
} catch (UnsupportedOperationException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canConvertToString() {
|
||||||
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
new Header[]{}, mockResource, null);
|
||||||
|
assertNotNull(entry.toString());
|
||||||
|
assertFalse("".equals(entry.toString()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue