17162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawkepackage fi.iki.elonen;
27162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
38b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie/*
48b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * #%L
58b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * NanoHttpd-Core
68b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * %%
78b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * Copyright (C) 2012 - 2015 nanohttpd
88b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * %%
98b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * Redistribution and use in source and binary forms, with or without modification,
108b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * are permitted provided that the following conditions are met:
119e1ec7bff40d70d31953a04dd448665aaf549395ritchie *
128b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 1. Redistributions of source code must retain the above copyright notice, this
138b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    list of conditions and the following disclaimer.
149e1ec7bff40d70d31953a04dd448665aaf549395ritchie *
158b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 2. Redistributions in binary form must reproduce the above copyright notice,
168b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    this list of conditions and the following disclaimer in the documentation
178b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    and/or other materials provided with the distribution.
189e1ec7bff40d70d31953a04dd448665aaf549395ritchie *
198b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 3. Neither the name of the nanohttpd nor the names of its contributors
208b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    may be used to endorse or promote products derived from this software without
218b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie *    specific prior written permission.
229e1ec7bff40d70d31953a04dd448665aaf549395ritchie *
238b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
248b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
258b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
268b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
278b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
288b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
298b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
308b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
318b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
328b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * OF THE POSSIBILITY OF SUCH DAMAGE.
338b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * #L%
348b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie */
358b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie
367162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawkeimport java.io.ByteArrayOutputStream;
377162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawkeimport java.io.InputStream;
387162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
395e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport org.junit.Test;
407162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
417162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawkepublic class HttpDeleteRequestTest extends HttpServerTest {
427162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
437162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    @Test
447162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    public void testDeleteRequestThatDoesntSendBackResponseBody_EmptyString() throws Exception {
45504cf71fb77ee34a9b37a632664c58f89ab1984eJarno Elonen        this.testServer.response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, "");
467162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
475e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
487162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
497162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        String[] expected = {
509058464950a9734da0a7ff2dc47f3081bbb5117critchie            "HTTP/1.1 204 No Content",
519058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Type: text/html",
529058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Date: .*",
539058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Connection: keep-alive",
549058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Length: 0",
559058464950a9734da0a7ff2dc47f3081bbb5117critchie            ""
567162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        };
577162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
587162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        assertResponse(outputStream, expected);
597162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    }
607162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
617162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    @Test
625e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    public void testDeleteRequestThatDoesntSendBackResponseBody_NullInputStream() throws Exception {
63504cf71fb77ee34a9b37a632664c58f89ab1984eJarno Elonen        this.testServer.response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, (InputStream) null);
647162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
655e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
667162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
677162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        String[] expected = {
689058464950a9734da0a7ff2dc47f3081bbb5117critchie            "HTTP/1.1 204 No Content",
699058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Type: text/html",
709058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Date: .*",
719058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Connection: keep-alive",
729058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Length: 0",
739058464950a9734da0a7ff2dc47f3081bbb5117critchie            ""
747162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        };
757162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
767162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        assertResponse(outputStream, expected);
777162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    }
787162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
797162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    @Test
805e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    public void testDeleteRequestThatDoesntSendBackResponseBody_NullString() throws Exception {
81504cf71fb77ee34a9b37a632664c58f89ab1984eJarno Elonen        this.testServer.response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, (String) null);
827162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
835e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
847162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
857162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        String[] expected = {
869058464950a9734da0a7ff2dc47f3081bbb5117critchie            "HTTP/1.1 204 No Content",
879058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Type: text/html",
889058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Date: .*",
899058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Connection: keep-alive",
909058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Length: 0",
919058464950a9734da0a7ff2dc47f3081bbb5117critchie            ""
927162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        };
937162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
947162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        assertResponse(outputStream, expected);
957162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    }
967162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
977162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    @Test
985e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    public void testDeleteRequestThatSendsBackResponseBody_Accepted() throws Exception {
99504cf71fb77ee34a9b37a632664c58f89ab1984eJarno Elonen        this.testServer.response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.ACCEPTED, "application/xml", "<body />");
1007162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
1015e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
1027162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
1037162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        String[] expected = {
1045e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            "HTTP/1.1 202 Accepted",
1059058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Type: application/xml",
1069058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Date: .*",
1079058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Connection: keep-alive",
1089058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Length: 8",
1099058464950a9734da0a7ff2dc47f3081bbb5117critchie            "",
1109058464950a9734da0a7ff2dc47f3081bbb5117critchie            "<body />"
1117162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        };
1127162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
1137162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        assertResponse(outputStream, expected);
1147162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    }
1157162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
1167162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    @Test
1175e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie    public void testDeleteRequestThatSendsBackResponseBody_Success() throws Exception {
118504cf71fb77ee34a9b37a632664c58f89ab1984eJarno Elonen        this.testServer.response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "application/xml", "<body />");
1197162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
1205e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie        ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
1217162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
1227162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        String[] expected = {
1235e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie            "HTTP/1.1 200 OK",
1249058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Type: application/xml",
1259058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Date: .*",
1269058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Connection: keep-alive",
1279058464950a9734da0a7ff2dc47f3081bbb5117critchie            "Content-Length: 8",
1289058464950a9734da0a7ff2dc47f3081bbb5117critchie            "",
1299058464950a9734da0a7ff2dc47f3081bbb5117critchie            "<body />"
1307162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        };
1317162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke
1327162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke        assertResponse(outputStream, expected);
1337162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke    }
1347162954f3d9927a455a9f6e806c8cecf5327dcc6Paul Hawke}
135