/*
* Weblounge: Web Content Management System
* Copyright (c) 2003 - 2011 The Weblounge Team
* http://entwinemedia.com/weblounge
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ch.entwine.weblounge.test.harness.content;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import ch.entwine.weblounge.common.impl.testing.IntegrationTestBase;
import ch.entwine.weblounge.common.impl.util.TestUtils;
import ch.entwine.weblounge.common.url.UrlUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
/**
* Integration test for the loading of site resources.
*/
public class StaticResourcesTest extends IntegrationTestBase {
/** The logger */
private static final Logger logger = LoggerFactory.getLogger(StaticResourcesTest.class);
/** The image content type */
private static final String CONTENT_TYPE_IMAGE = "image/jpeg";
/** Path to the image */
private static final String IMAGE_PATH = "/weblounge-sites/weblounge-test/web/images/porsche.jpg";
/**
* Creates a new instance of the <code>SiteResources</code> test.
*/
public StaticResourcesTest() {
super("Static Resources Test", WEBLOUNGE_CONTENT_TEST_GROUP);
}
/**
* {@inheritDoc}
*
* @see ch.entwine.weblounge.testing.kernel.IntegrationTest#execute(java.lang.String)
*/
@Override
public void execute(String serverUrl) throws Exception {
logger.info("Testing loading of an image");
String requestUrl = UrlUtils.concat(serverUrl, IMAGE_PATH);
// Value of the ETag header from the first response */
String eTagValue = null;
// Send and the request and examine the response
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet request = new HttpGet(requestUrl);
logger.info("Sending request to {}", requestUrl);
HttpResponse response = TestUtils.request(httpClient, request, null);
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
String contentType = response.getEntity().getContentType().getValue();
assertEquals(CONTENT_TYPE_IMAGE, contentType.split(";")[0]);
// Read the content
response.getEntity().consumeContent();
// Read and store ETag
Header eTagHeader = response.getFirstHeader("ETag");
assertNotNull(eTagHeader);
assertNotNull(eTagHeader.getValue());
eTagValue = eTagHeader.getValue();
} finally {
httpClient.getConnectionManager().shutdown();
}
// Test ETag support
httpClient = new DefaultHttpClient();
try {
HttpGet request = new HttpGet(requestUrl);
request.addHeader("If-None-Match", eTagValue);
logger.info("Sending 'If-None-Match' request to {}", requestUrl);
HttpResponse response = TestUtils.request(httpClient, request, null);
assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatusLine().getStatusCode());
assertNull(response.getEntity());
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}