1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-11-15 16:57:09 +00:00
plantuml-server/src/test/java/net/sourceforge/plantuml/servlet/TestImage.java

85 lines
3.6 KiB
Java
Raw Normal View History

2011-02-23 14:51:38 +00:00
package net.sourceforge.plantuml.servlet;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
2011-03-10 15:17:30 +00:00
import java.util.Date;
import java.util.Locale;
2011-02-23 14:51:38 +00:00
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
public class TestImage extends WebappTestCase {
2011-02-23 14:51:38 +00:00
/**
* Verifies the generation of the version image from an encoded URL
*/
public void testVersionImage() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "png/" + TestUtils.VERSION);
2013-07-10 15:07:24 +00:00
WebResponse response = conversation.getResource(request);
2011-02-23 14:51:38 +00:00
// Analyze response
// Verifies the Content-Type header
2013-07-10 15:07:24 +00:00
assertEquals("Response content type is not PNG", "image/png", response.getContentType());
// Get the image and verify its size
2011-02-23 14:51:38 +00:00
InputStream responseStream = response.getInputStream();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
2013-07-10 15:07:24 +00:00
while ((n = responseStream.read(buf)) != -1) {
imageStream.write(buf, 0, n);
2011-02-23 14:51:38 +00:00
}
imageStream.close();
byte[] inMemoryImage = imageStream.toByteArray();
int diagramLen = inMemoryImage.length;
2013-07-10 15:07:24 +00:00
assertTrue(diagramLen > 10000);
assertTrue(diagramLen < 20000);
2013-07-01 20:40:14 +00:00
responseStream.close();
2011-02-23 14:51:38 +00:00
}
2011-03-10 15:17:30 +00:00
/**
* Verifies that the HTTP header of a diagram incites the browser to cache it.
*/
public void testDiagramHttpHeader() throws Exception {
WebConversation conversation = new WebConversation();
// Bob -> Alice : hello
WebRequest request = new GetMethodWebRequest(getServerUrl() + "png/" + TestUtils.SEQBOB);
2013-07-10 15:07:24 +00:00
WebResponse response = conversation.getResource(request);
2011-03-10 15:17:30 +00:00
// Analyze response
// Verifies the Content-Type header
2013-07-10 15:07:24 +00:00
assertEquals("Response content type is not PNG", "image/png", response.getContentType());
2011-03-10 15:17:30 +00:00
// Verifies the availability of the Expires entry in the response header
2013-07-10 15:07:24 +00:00
assertNotNull(response.getHeaderField("Expires"));
2011-03-10 15:17:30 +00:00
// Verifies the availability of the Last-Modified entry in the response header
2013-07-10 15:07:24 +00:00
assertNotNull(response.getHeaderField("Last-Modified"));
2011-03-10 15:17:30 +00:00
// Verifies the Last-Modified value is in the past
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZ", Locale.ENGLISH);
2013-07-10 15:07:24 +00:00
Date lastModified = format.parse(response.getHeaderField("Last-Modified"));
assertTrue("Last-Modified is not in the past", lastModified.before(new Date()));
// Consume the response
InputStream responseStream = response.getInputStream();
2013-07-10 15:07:24 +00:00
while (responseStream.read() != -1) {
// Do nothing
}
2011-03-10 15:17:30 +00:00
}
/**
* Verifies that the HTTP header of a diagram incites the browser to cache it.
*/
public void testOldImgURL() throws Exception {
WebConversation conversation = new WebConversation();
// Bob -> Alice : hello
WebRequest request = new GetMethodWebRequest(getServerUrl() + "img/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not PNG", "image/png", response.getContentType());
// Consume the response
InputStream responseStream = response.getInputStream();
while (responseStream.read() != -1) {
// Do nothing
}
}
2011-02-23 14:51:38 +00:00
}