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;
29  
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  /**
34   * Simple tests for various HTTP exception classes.
35   */
36  public class TestHttpExceptions {
37  
38      private static final String CLEAN_MESSAGE = "[0x00]Hello[0x06][0x07][0x08][0x09][0x0a][0x0b][0x0c][0x0d][0x0e][0x0f]World";
39      private static final String nonPrintableMessage = String.valueOf(
40              new char[] { 1, 'H', 'e', 'l', 'l', 'o', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'W', 'o', 'r', 'l', 'd' });
41  
42      @Test
43      public void testConstructor() {
44          final Throwable cause = new Exception();
45          new HttpException();
46          new HttpException("Oppsie");
47          new HttpException("Oppsie", cause);
48          new ProtocolException();
49          new ProtocolException("Oppsie");
50          new ProtocolException("Oppsie", cause);
51          new NoHttpResponseException("Oppsie");
52          new ConnectionClosedException("Oppsie");
53          new MethodNotSupportedException("Oppsie");
54          new MethodNotSupportedException("Oppsie", cause);
55          new UnsupportedHttpVersionException();
56          new UnsupportedHttpVersionException("Oppsie");
57      }
58  
59      @Test
60      public void testNonPrintableCharactersInConnectionClosedException() {
61          Assert.assertEquals(CLEAN_MESSAGE, new ConnectionClosedException(nonPrintableMessage).getMessage());
62      }
63  
64      @Test
65      public void testNonPrintableCharactersInHttpException() {
66          Assert.assertEquals(CLEAN_MESSAGE, new HttpException(nonPrintableMessage).getMessage());
67      }
68  
69      @Test
70      public void testNonPrintableCharactersInMethodNotSupportedException() {
71          Assert.assertEquals(CLEAN_MESSAGE, new MethodNotSupportedException(nonPrintableMessage).getMessage());
72      }
73  
74      @Test
75      public void testNonPrintableCharactersInNoHttpResponseException() {
76          Assert.assertEquals(CLEAN_MESSAGE, new NoHttpResponseException(nonPrintableMessage).getMessage());
77      }
78  
79      @Test
80      public void testNonPrintableCharactersInProtocolException() {
81          Assert.assertEquals(CLEAN_MESSAGE, new ProtocolException(nonPrintableMessage).getMessage());
82      }
83  
84      @Test
85      public void testNonPrintableCharactersInUnsupportedHttpVersionException() {
86          Assert.assertEquals(CLEAN_MESSAGE, new UnsupportedHttpVersionException(nonPrintableMessage).getMessage());
87      }
88  
89  }