1 package fi.iki.elonen.integration; 2 3 /* 4 * #%L 5 * NanoHttpd-Core 6 * %% 7 * Copyright (C) 2012 - 2015 nanohttpd 8 * %% 9 * Redistribution and use in source and binary forms, with or without modification, 10 * are permitted provided that the following conditions are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright notice, this 13 * list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the nanohttpd nor the names of its contributors 20 * may be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 * OF THE POSSIBILITY OF SUCH DAMAGE. 33 * #L% 34 */ 35 36 import static org.hamcrest.CoreMatchers.is; 37 import static org.hamcrest.CoreMatchers.nullValue; 38 import static org.junit.Assert.assertEquals; 39 import static org.junit.Assert.assertNotNull; 40 import static org.junit.Assert.assertNull; 41 import static org.junit.Assert.assertThat; 42 43 import java.io.ByteArrayInputStream; 44 import java.io.IOException; 45 import java.io.InputStream; 46 47 import org.apache.http.Header; 48 import org.apache.http.HttpResponse; 49 import org.apache.http.client.methods.HttpGet; 50 import org.apache.http.impl.client.DecompressingHttpClient; 51 import org.apache.http.util.EntityUtils; 52 import org.junit.Test; 53 54 import fi.iki.elonen.NanoHTTPD; 55 56 public class GZipIntegrationTest extends IntegrationTestBase<GZipIntegrationTest.TestServer> { 57 58 public static class TestServer extends NanoHTTPD { 59 60 public Response response; 61 62 public TestServer() { 63 super(8192); 64 } 65 66 @Override 67 public Response serve(IHTTPSession session) { 68 return response; 69 } 70 71 @Override 72 protected boolean useGzipWhenAccepted(Response r) { 73 return true; 74 } 75 } 76 77 @Override 78 public TestServer createTestServer() { 79 return new TestServer(); 80 } 81 82 @Test 83 public void contentEncodingShouldBeAddedToFixedLengthResponses() throws IOException { 84 testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test"); 85 HttpGet request = new HttpGet("http://localhost:8192/"); 86 request.addHeader("Accept-encoding", "gzip"); 87 HttpResponse response = httpclient.execute(request); 88 Header contentEncoding = response.getFirstHeader("content-encoding"); 89 assertNotNull("Content-Encoding should be set", contentEncoding); 90 assertEquals("gzip", contentEncoding.getValue()); 91 } 92 93 @Test 94 public void contentEncodingShouldBeAddedToChunkedResponses() throws IOException { 95 InputStream data = new ByteArrayInputStream("This is a test".getBytes("UTF-8")); 96 testServer.response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, "text/plain", data); 97 HttpGet request = new HttpGet("http://localhost:8192/"); 98 request.addHeader("Accept-encoding", "gzip"); 99 HttpResponse response = httpclient.execute(request); 100 Header contentEncoding = response.getFirstHeader("content-encoding"); 101 assertNotNull("Content-Encoding should be set", contentEncoding); 102 assertEquals("gzip", contentEncoding.getValue()); 103 } 104 105 @Test 106 public void shouldFindCorrectAcceptEncodingAmongMany() throws IOException { 107 testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test"); 108 HttpGet request = new HttpGet("http://localhost:8192/"); 109 request.addHeader("Accept-encoding", "deflate,gzip"); 110 HttpResponse response = httpclient.execute(request); 111 Header contentEncoding = response.getFirstHeader("content-encoding"); 112 assertNotNull("Content-Encoding should be set", contentEncoding); 113 assertEquals("gzip", contentEncoding.getValue()); 114 } 115 116 @Test 117 public void contentLengthShouldBeRemovedFromZippedResponses() throws IOException { 118 testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test"); 119 HttpGet request = new HttpGet("http://localhost:8192/"); 120 request.addHeader("Accept-encoding", "gzip"); 121 HttpResponse response = httpclient.execute(request); 122 Header contentLength = response.getFirstHeader("content-length"); 123 assertNull("Content-Length should not be set when gzipping response", contentLength); 124 } 125 126 @Test 127 public void fixedLengthContentIsEncodedProperly() throws IOException { 128 testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test"); 129 HttpGet request = new HttpGet("http://localhost:8192/"); 130 request.addHeader("Accept-encoding", "gzip"); 131 HttpResponse response = new DecompressingHttpClient(httpclient).execute(request); 132 assertEquals("This is a test", EntityUtils.toString(response.getEntity())); 133 } 134 135 @Test 136 public void chunkedContentIsEncodedProperly() throws IOException { 137 InputStream data = new ByteArrayInputStream("This is a test".getBytes("UTF-8")); 138 testServer.response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, "text/plain", data); 139 HttpGet request = new HttpGet("http://localhost:8192/"); 140 request.addHeader("Accept-encoding", "gzip"); 141 HttpResponse response = new DecompressingHttpClient(httpclient).execute(request); 142 assertEquals("This is a test", EntityUtils.toString(response.getEntity())); 143 } 144 145 @Test 146 public void noGzipWithoutAcceptEncoding() throws IOException { 147 testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test"); 148 HttpGet request = new HttpGet("http://localhost:8192/"); 149 HttpResponse response = httpclient.execute(request); 150 Header contentEncoding = response.getFirstHeader("content-encoding"); 151 assertThat(contentEncoding, is(nullValue())); 152 assertEquals("This is a test", EntityUtils.toString(response.getEntity())); 153 } 154 155 @Test 156 public void contentShouldNotBeGzippedIfContentLengthIsAddedManually() throws IOException { 157 testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test"); 158 testServer.response.addHeader("Content-Length", "" + ("This is a test".getBytes("UTF-8").length)); 159 HttpGet request = new HttpGet("http://localhost:8192/"); 160 request.addHeader("Accept-encoding", "gzip"); 161 HttpResponse response = httpclient.execute(request); 162 Header contentEncoding = response.getFirstHeader("content-encoding"); 163 assertNull("Content-Encoding should not be set when manually setting content-length", contentEncoding); 164 assertEquals("This is a test", EntityUtils.toString(response.getEntity())); 165 166 } 167 168 } 169