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 java.util.Locale;
31  
32  import org.apache.hc.core5.http.HttpResponse;
33  import org.apache.hc.core5.http.HttpStatus;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  
37  public class HttpResponseWrapperTest {
38  
39      @Test
40      void testDefaultResponseConstructors() {
41          final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request");
42          final HttpResponseWrapper httpResponseWrapper1 = new HttpResponseWrapper(response1);
43  
44          Assertions.assertEquals(HttpStatus.SC_BAD_REQUEST, httpResponseWrapper1.getCode());
45  
46          final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever");
47          final HttpResponseWrapper httpResponseWrapper2 = new HttpResponseWrapper(response2);
48          Assertions.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, httpResponseWrapper2.getCode());
49          Assertions.assertEquals("whatever", httpResponseWrapper2.getReasonPhrase());
50  
51          httpResponseWrapper2.setReasonPhrase("another-whatever");
52          Assertions.assertEquals("another-whatever", httpResponseWrapper2.getReasonPhrase());
53      }
54  
55      @Test
56      void testSetResponseStatus() {
57          final HttpResponse response1 = new BasicHttpResponse(200, "OK");
58          final HttpResponseWrapper httpResponseWrapper1 = new HttpResponseWrapper(response1);
59  
60          Assertions.assertNotNull(httpResponseWrapper1.getCode());
61          Assertions.assertEquals(200, httpResponseWrapper1.getCode());
62  
63          final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request");
64          final HttpResponseWrapper httpResponseWrapper2 = new HttpResponseWrapper(response2);
65          Assertions.assertEquals(HttpStatus.SC_BAD_REQUEST, httpResponseWrapper2.getCode());
66  
67          final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever");
68          final HttpResponseWrapper httpResponseWrapper3 = new HttpResponseWrapper(response3);
69          Assertions.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, httpResponseWrapper3.getCode());
70          Assertions.assertEquals("whatever", httpResponseWrapper3.getReasonPhrase());
71  
72          final HttpResponse response4 = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
73          final HttpResponseWrapper httpResponseWrapper4 = new HttpResponseWrapper(response4);
74          Assertions.assertThrows(IllegalArgumentException.class, () -> httpResponseWrapper4.setCode(-23));
75      }
76  
77      @Test
78      void testLocale() {
79          final HttpResponse response = new BasicHttpResponse(200, "OK");
80          final HttpResponseWrapper httpResponseWrapper = new HttpResponseWrapper(response);
81          httpResponseWrapper.setLocale(Locale.US);
82          Assertions.assertEquals("US", httpResponseWrapper.getLocale().getCountry());
83      }
84  
85  }