View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.message;
29  
30  import org.apache.hc.core5.http.message.StatusLine.StatusClass;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  /**
35   * Unit tests for {@link org.apache.hc.core5.http.message.StatusLine}.
36   */
37  public class TestBasicStatusLine {
38  
39      @Test
40      public void testGetStatusClass() {
41          StatusLine statusLine = new StatusLine(new BasicHttpResponse(100, "Continue"));
42          Assert.assertEquals(StatusClass.INFORMATIONAL, statusLine.getStatusClass());
43  
44          statusLine = new StatusLine(new BasicHttpResponse(200, "OK"));
45          Assert.assertEquals(StatusClass.SUCCESSFUL, statusLine.getStatusClass());
46  
47          statusLine = new StatusLine(new BasicHttpResponse(302, "Found"));
48          Assert.assertEquals(StatusClass.REDIRECTION, statusLine.getStatusClass());
49  
50          statusLine = new StatusLine(new BasicHttpResponse(409, "Conflict"));
51          Assert.assertEquals(StatusClass.CLIENT_ERROR, statusLine.getStatusClass());
52  
53          statusLine = new StatusLine(new BasicHttpResponse(502, "Bad Gateway"));
54          Assert.assertEquals(StatusClass.SERVER_ERROR, statusLine.getStatusClass());
55  
56          statusLine = new StatusLine(new BasicHttpResponse(999, "Not a status"));
57          Assert.assertEquals(StatusClass.OTHER, statusLine.getStatusClass());
58      }
59  
60      @Test
61      public void testGetStatusShorthand() {
62          StatusLine statusLine = new StatusLine(new BasicHttpResponse(100, "Continue"));
63          Assert.assertTrue(statusLine.isInformational());
64          Assert.assertFalse(statusLine.isSuccessful());
65          Assert.assertFalse(statusLine.isError());
66  
67          statusLine = new StatusLine(new BasicHttpResponse(200, "OK"));
68          Assert.assertTrue(statusLine.isSuccessful());
69          Assert.assertFalse(statusLine.isRedirection());
70          Assert.assertFalse(statusLine.isError());
71  
72          statusLine = new StatusLine(new BasicHttpResponse(302, "Found"));
73          Assert.assertTrue(statusLine.isRedirection());
74          Assert.assertFalse(statusLine.isClientError());
75          Assert.assertFalse(statusLine.isError());
76  
77          statusLine = new StatusLine(new BasicHttpResponse(409, "Conflict"));
78          Assert.assertTrue(statusLine.isClientError());
79          Assert.assertTrue(statusLine.isError());
80          Assert.assertFalse(statusLine.isServerError());
81  
82          statusLine = new StatusLine(new BasicHttpResponse(502, "Bad Gateway"));
83          Assert.assertTrue(statusLine.isServerError());
84          Assert.assertTrue(statusLine.isError());
85          Assert.assertFalse(statusLine.isSuccessful());
86      }
87  }