Home | History | Annotate | Download | only in http
      1 /*
      2  * Copyright (C) 2012 Square, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.squareup.okhttp.internal.http;
     17 
     18 import com.squareup.okhttp.Protocol;
     19 import java.io.IOException;
     20 import java.net.ProtocolException;
     21 import org.junit.Test;
     22 
     23 import static org.junit.Assert.assertEquals;
     24 import static org.junit.Assert.fail;
     25 
     26 public final class StatusLineTest {
     27   @Test public void parse() throws IOException {
     28     String message = "Temporary Redirect";
     29     int version = 1;
     30     int code = 200;
     31     StatusLine statusLine = StatusLine.parse("HTTP/1." + version + " " + code + " " + message);
     32     assertEquals(message, statusLine.message);
     33     assertEquals(Protocol.HTTP_1_1, statusLine.protocol);
     34     assertEquals(code, statusLine.code);
     35   }
     36 
     37   @Test public void emptyMessage() throws IOException {
     38     int version = 1;
     39     int code = 503;
     40     StatusLine statusLine = StatusLine.parse("HTTP/1." + version + " " + code + " ");
     41     assertEquals("", statusLine.message);
     42     assertEquals(Protocol.HTTP_1_1, statusLine.protocol);
     43     assertEquals(code, statusLine.code);
     44   }
     45 
     46   /**
     47    * This is not defined in the protocol but some servers won't add the leading
     48    * empty space when the message is empty.
     49    * http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
     50    */
     51   @Test public void emptyMessageAndNoLeadingSpace() throws IOException {
     52     int version = 1;
     53     int code = 503;
     54     StatusLine statusLine = StatusLine.parse("HTTP/1." + version + " " + code);
     55     assertEquals("", statusLine.message);
     56     assertEquals(Protocol.HTTP_1_1, statusLine.protocol);
     57     assertEquals(code, statusLine.code);
     58   }
     59 
     60   // https://github.com/square/okhttp/issues/386
     61   @Test public void shoutcast() throws IOException {
     62     StatusLine statusLine = StatusLine.parse("ICY 200 OK");
     63     assertEquals("OK", statusLine.message);
     64     assertEquals(Protocol.HTTP_1_0, statusLine.protocol);
     65     assertEquals(200, statusLine.code);
     66   }
     67 
     68   @Test public void missingProtocol() throws IOException {
     69     assertInvalid("");
     70     assertInvalid(" ");
     71     assertInvalid("200 OK");
     72     assertInvalid(" 200 OK");
     73   }
     74 
     75   @Test public void protocolVersions() throws IOException {
     76     assertInvalid("HTTP/2.0 200 OK");
     77     assertInvalid("HTTP/2.1 200 OK");
     78     assertInvalid("HTTP/-.1 200 OK");
     79     assertInvalid("HTTP/1.- 200 OK");
     80     assertInvalid("HTTP/0.1 200 OK");
     81     assertInvalid("HTTP/101 200 OK");
     82     assertInvalid("HTTP/1.1_200 OK");
     83   }
     84 
     85   @Test public void nonThreeDigitCode() throws IOException {
     86     assertInvalid("HTTP/1.1  OK");
     87     assertInvalid("HTTP/1.1 2 OK");
     88     assertInvalid("HTTP/1.1 20 OK");
     89     assertInvalid("HTTP/1.1 2000 OK");
     90     assertInvalid("HTTP/1.1 two OK");
     91     assertInvalid("HTTP/1.1 2");
     92     assertInvalid("HTTP/1.1 2000");
     93     assertInvalid("HTTP/1.1 two");
     94   }
     95 
     96   @Test public void truncated() throws IOException {
     97     assertInvalid("");
     98     assertInvalid("H");
     99     assertInvalid("HTTP/1");
    100     assertInvalid("HTTP/1.");
    101     assertInvalid("HTTP/1.1");
    102     assertInvalid("HTTP/1.1 ");
    103     assertInvalid("HTTP/1.1 2");
    104     assertInvalid("HTTP/1.1 20");
    105   }
    106 
    107   @Test public void wrongMessageDelimiter() throws IOException {
    108     assertInvalid("HTTP/1.1 200_");
    109   }
    110 
    111   private void assertInvalid(String statusLine) throws IOException {
    112     try {
    113       StatusLine.parse(statusLine);
    114       fail();
    115     } catch (ProtocolException expected) {
    116     }
    117   }
    118 }
    119