/**
* OLAT - Online Learning and Training
* http://www.olat.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),
* University of Zurich, Switzerland.
*
*/
package org.olat.core.gui.render;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Test;
/**
* Description:
* downloads the entry page from olatng.uzh.ch and checks if the response
* headers are sent with proper cache control entries like:
* Cache-Control: max-age=31536000, public
* This allows browsers to cache static resources and minimize traffic which returns 304 (not modified).
* If the tests fails make sure that http.conf from apache has the following entry
*
*
* Initial Date: 17.01.2011
*
* @author guido
*/
public class TestRenderStaticURLCacheHeaders {
private String uri = "https://olatng.uzh.ch/";
@Test
public void cacheHeaders() throws IOException, MalformedURLException {
URL url = new URL(uri + "olat/dmz/");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
assertEquals(200, uc.getResponseCode());
assertEquals(true, uc.getContentType().startsWith("text/html"));
BufferedReader in = new BufferedReader(new InputStreamReader(uc
.getInputStream()));
String inputLine;
// grab the first url that points to an static resource to check headers
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains("src=\"/olat/raw/")) {
//System.out.println("Found line: " + inputLine);
break;
}
}
in.close();
String jsUrl = inputLine.substring(inputLine.indexOf("src=\"") + 6,
inputLine.lastIndexOf("\""));
assertTrue(jsUrl.startsWith("olat/"));
url = new URL(uri + jsUrl);
uc = (HttpURLConnection) url.openConnection();
assertEquals(200, uc.getResponseCode());
// the cached elements in olat e.g. js, css ... should have an cache
// lifetime of one year
assertEquals("max-age=31536000, public", uc.getHeaderField("Cache-Control"));
}
}