Lines 16-24
Link Here
|
16 |
*/ |
16 |
*/ |
17 |
package org.apache.catalina.servlets; |
17 |
package org.apache.catalina.servlets; |
18 |
|
18 |
|
|
|
19 |
import static org.apache.catalina.startup.SimpleHttpClient.CRLF; |
20 |
import static org.junit.Assert.assertEquals; |
21 |
import static org.junit.Assert.assertFalse; |
22 |
import static org.junit.Assert.assertTrue; |
23 |
import static org.junit.Assert.fail; |
24 |
|
25 |
import java.io.ByteArrayOutputStream; |
19 |
import java.io.File; |
26 |
import java.io.File; |
20 |
import java.io.FileOutputStream; |
27 |
import java.io.FileOutputStream; |
21 |
import java.io.IOException; |
28 |
import java.io.IOException; |
|
|
29 |
import java.io.OutputStream; |
22 |
import java.io.OutputStreamWriter; |
30 |
import java.io.OutputStreamWriter; |
23 |
import java.io.Writer; |
31 |
import java.io.Writer; |
24 |
import java.text.SimpleDateFormat; |
32 |
import java.text.SimpleDateFormat; |
Lines 27-48
Link Here
|
27 |
import java.util.Locale; |
35 |
import java.util.Locale; |
28 |
import java.util.Map; |
36 |
import java.util.Map; |
29 |
import java.util.TimeZone; |
37 |
import java.util.TimeZone; |
|
|
38 |
import java.util.zip.GZIPOutputStream; |
30 |
|
39 |
|
31 |
import javax.servlet.http.HttpServletResponse; |
40 |
import javax.servlet.http.HttpServletResponse; |
32 |
|
41 |
|
33 |
import static org.junit.Assert.assertEquals; |
|
|
34 |
import static org.junit.Assert.assertFalse; |
35 |
import static org.junit.Assert.assertTrue; |
36 |
import static org.junit.Assert.fail; |
37 |
|
38 |
import org.junit.Test; |
39 |
|
40 |
import static org.apache.catalina.startup.SimpleHttpClient.CRLF; |
41 |
|
42 |
import org.apache.catalina.startup.SimpleHttpClient; |
42 |
import org.apache.catalina.startup.SimpleHttpClient; |
43 |
import org.apache.catalina.startup.Tomcat; |
43 |
import org.apache.catalina.startup.Tomcat; |
44 |
import org.apache.catalina.startup.TomcatBaseTest; |
44 |
import org.apache.catalina.startup.TomcatBaseTest; |
45 |
import org.apache.tomcat.util.buf.ByteChunk; |
45 |
import org.apache.tomcat.util.buf.ByteChunk; |
|
|
46 |
import org.junit.Test; |
46 |
|
47 |
|
47 |
public class TestDefaultServlet extends TomcatBaseTest { |
48 |
public class TestDefaultServlet extends TomcatBaseTest { |
48 |
|
49 |
|
Lines 85-91
Link Here
|
85 |
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc); |
86 |
assertEquals(HttpServletResponse.SC_NOT_FOUND, rc); |
86 |
|
87 |
|
87 |
} |
88 |
} |
|
|
89 |
|
90 |
/** |
91 |
* Verify serving of gzipped resources from context root. |
92 |
*/ |
93 |
@Test |
94 |
public void testGzippedFile() throws Exception { |
95 |
File appDir = new File(getTemporaryDirectory(), "MyApp"); |
96 |
File webInf = new File(appDir, "WEB-INF"); |
97 |
addDeleteOnTearDown(appDir); |
98 |
if (!webInf.mkdirs() && !webInf.isDirectory()) { |
99 |
fail("Unable to create directory [" + webInf + "]"); |
100 |
} |
101 |
|
102 |
Writer w = new OutputStreamWriter(new FileOutputStream(new File(appDir, |
103 |
"index.html")), "UTF-8"); |
104 |
try { |
105 |
w.write("<h1>Hello</h1>"); |
106 |
w.flush(); |
107 |
} finally { |
108 |
w.close(); |
109 |
} |
110 |
OutputStream o = new GZIPOutputStream(new FileOutputStream(new File( |
111 |
appDir, "index.html.gz"))); |
112 |
try { |
113 |
o.write("<h1>Hello</h1>".getBytes("UTF-8")); |
114 |
o.flush(); |
115 |
} finally { |
116 |
o.close(); |
117 |
} |
118 |
|
119 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
120 |
GZIPOutputStream gzipstream = new GZIPOutputStream(bos); |
121 |
gzipstream.write("<h1>Hello</h1>".getBytes("UTF-8")); |
122 |
gzipstream.flush(); |
123 |
gzipstream.close(); |
124 |
String gzipped = new String(bos.toByteArray(), "ISO-8859-1"); // encoding used by SimpleHttpClient |
125 |
|
126 |
Tomcat tomcat = getTomcatInstance(); |
127 |
String contextPath = "/MyApp"; |
128 |
tomcat.addWebapp(null, contextPath, appDir.getAbsolutePath()); |
129 |
tomcat.start(); |
130 |
|
131 |
TestGzipClient gzipClient = |
132 |
new TestGzipClient(tomcat.getConnector().getLocalPort()); |
88 |
|
133 |
|
|
|
134 |
gzipClient.reset(); |
135 |
gzipClient.setRequest(new String[] { |
136 |
"GET /MyApp/index.html HTTP/1.1" + CRLF + |
137 |
"Host: localhost" + CRLF + |
138 |
"Connection: Close" + CRLF + |
139 |
"Accept-Encoding: gzip" + CRLF + CRLF }); |
140 |
gzipClient.connect(); |
141 |
gzipClient.processRequest(); |
142 |
assertTrue(gzipClient.isResponse200()); |
143 |
assertEquals(gzipped, gzipClient.getResponseBody()); |
144 |
List<String> responseHeaders = gzipClient.getResponseHeaders(); |
145 |
assertTrue(responseHeaders.contains("Content-Type: text/html")); |
146 |
assertTrue(responseHeaders.contains("Content-Encoding: gzip")); |
147 |
assertTrue(responseHeaders.contains("Content-Length: " + gzipped.length())); |
148 |
|
149 |
gzipClient.reset(); |
150 |
gzipClient.setRequest(new String[] { |
151 |
"GET /MyApp/index.html HTTP/1.1" + CRLF + |
152 |
"Host: localhost" + CRLF + |
153 |
"Connection: Close" + CRLF+ CRLF }); |
154 |
gzipClient.connect(); |
155 |
gzipClient.processRequest(); |
156 |
assertTrue(gzipClient.isResponse200()); |
157 |
assertEquals("<h1>Hello</h1>", gzipClient.getResponseBody()); |
158 |
responseHeaders = gzipClient.getResponseHeaders(); |
159 |
assertTrue(responseHeaders.contains("Content-Type: text/html")); |
160 |
assertFalse(responseHeaders.contains("Content-Encoding: gzip")); |
161 |
assertTrue(responseHeaders.contains("Content-Length: " |
162 |
+ "<h1>Hello</h1>".length())); |
163 |
} |
164 |
|
89 |
/** |
165 |
/** |
90 |
* Test https://issues.apache.org/bugzilla/show_bug.cgi?id=50026 |
166 |
* Test https://issues.apache.org/bugzilla/show_bug.cgi?id=50026 |
91 |
* Verify serving of resources from context root with subpath mapping. |
167 |
* Verify serving of resources from context root with subpath mapping. |
Lines 304-307
Link Here
|
304 |
return true; |
380 |
return true; |
305 |
} |
381 |
} |
306 |
} |
382 |
} |
|
|
383 |
|
384 |
private static class TestGzipClient extends SimpleHttpClient { |
385 |
|
386 |
public TestGzipClient(int port) { |
387 |
setPort(port); |
388 |
} |
389 |
|
390 |
@Override |
391 |
public boolean isResponseBodyOK() { |
392 |
return true; |
393 |
} |
394 |
} |
307 |
} |
395 |
} |