Home | History | Annotate | Download | only in elonen
      1 package fi.iki.elonen;
      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 java.io.ByteArrayOutputStream;
     37 import java.io.InputStream;
     38 
     39 import org.junit.Test;
     40 
     41 public class HttpDeleteRequestTest extends HttpServerTest {
     42 
     43     @Test
     44     public void testDeleteRequestThatDoesntSendBackResponseBody_EmptyString() throws Exception {
     45         this.testServer.response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, "");
     46 
     47         ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
     48 
     49         String[] expected = {
     50             "HTTP/1.1 204 No Content",
     51             "Content-Type: text/html",
     52             "Date: .*",
     53             "Connection: keep-alive",
     54             "Content-Length: 0",
     55             ""
     56         };
     57 
     58         assertResponse(outputStream, expected);
     59     }
     60 
     61     @Test
     62     public void testDeleteRequestThatDoesntSendBackResponseBody_NullInputStream() throws Exception {
     63         this.testServer.response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, (InputStream) null);
     64 
     65         ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
     66 
     67         String[] expected = {
     68             "HTTP/1.1 204 No Content",
     69             "Content-Type: text/html",
     70             "Date: .*",
     71             "Connection: keep-alive",
     72             "Content-Length: 0",
     73             ""
     74         };
     75 
     76         assertResponse(outputStream, expected);
     77     }
     78 
     79     @Test
     80     public void testDeleteRequestThatDoesntSendBackResponseBody_NullString() throws Exception {
     81         this.testServer.response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, (String) null);
     82 
     83         ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
     84 
     85         String[] expected = {
     86             "HTTP/1.1 204 No Content",
     87             "Content-Type: text/html",
     88             "Date: .*",
     89             "Connection: keep-alive",
     90             "Content-Length: 0",
     91             ""
     92         };
     93 
     94         assertResponse(outputStream, expected);
     95     }
     96 
     97     @Test
     98     public void testDeleteRequestThatSendsBackResponseBody_Accepted() throws Exception {
     99         this.testServer.response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.ACCEPTED, "application/xml", "<body />");
    100 
    101         ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
    102 
    103         String[] expected = {
    104             "HTTP/1.1 202 Accepted",
    105             "Content-Type: application/xml",
    106             "Date: .*",
    107             "Connection: keep-alive",
    108             "Content-Length: 8",
    109             "",
    110             "<body />"
    111         };
    112 
    113         assertResponse(outputStream, expected);
    114     }
    115 
    116     @Test
    117     public void testDeleteRequestThatSendsBackResponseBody_Success() throws Exception {
    118         this.testServer.response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "application/xml", "<body />");
    119 
    120         ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1");
    121 
    122         String[] expected = {
    123             "HTTP/1.1 200 OK",
    124             "Content-Type: application/xml",
    125             "Date: .*",
    126             "Connection: keep-alive",
    127             "Content-Length: 8",
    128             "",
    129             "<body />"
    130         };
    131 
    132         assertResponse(outputStream, expected);
    133     }
    134 }
    135