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  package org.apache.hc.core5.http.io.support;
28  
29  import java.io.ByteArrayOutputStream;
30  import java.io.IOException;
31  import java.net.Socket;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.apache.hc.core5.http.ClassicHttpResponse;
35  import org.apache.hc.core5.http.ContentType;
36  import org.apache.hc.core5.http.Header;
37  import org.apache.hc.core5.http.HttpException;
38  import org.apache.hc.core5.http.HttpVersion;
39  import org.apache.hc.core5.http.config.Http1Config;
40  import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
41  import org.apache.hc.core5.http.impl.io.DefaultBHttpServerConnection;
42  import org.apache.hc.core5.http.impl.io.DefaultHttpRequestParserFactory;
43  import org.apache.hc.core5.http.impl.io.DefaultHttpResponseWriterFactory;
44  import org.apache.hc.core5.http.io.entity.StringEntity;
45  import org.apache.hc.core5.http.message.BasicHeader;
46  import org.junit.jupiter.api.Assertions;
47  import org.junit.jupiter.api.BeforeEach;
48  import org.junit.jupiter.api.Test;
49  import org.mockito.Mock;
50  import org.mockito.Mockito;
51  import org.mockito.MockitoAnnotations;
52  
53  class ClassicResponseBuilderTest {
54  
55      @Mock
56      Socket socket;
57  
58      DefaultBHttpServerConnection conn;
59  
60      ByteArrayOutputStream outStream;
61  
62      @BeforeEach
63      public void prepareMocks() throws IOException {
64          MockitoAnnotations.openMocks(this);
65          conn = new DefaultBHttpServerConnection("http", Http1Config.DEFAULT,
66                  null, null,
67                  DefaultContentLengthStrategy.INSTANCE,
68                  DefaultContentLengthStrategy.INSTANCE,
69                  DefaultHttpRequestParserFactory.INSTANCE,
70                  DefaultHttpResponseWriterFactory.INSTANCE);
71          outStream = new ByteArrayOutputStream();
72          Mockito.when(socket.getOutputStream()).thenReturn(outStream);
73          conn.bind(socket);
74          Assertions.assertEquals(0, conn.getEndpointDetails().getResponseCount());
75      }
76  
77      @Test
78      void create() throws IOException, HttpException {
79  
80          final ClassicHttpResponse response = ClassicResponseBuilder.create(200)
81                  .setHeader("X-Test-Filter", "active")
82                  .addHeader("header1", "blah")
83                  .setHeader(new BasicHeader("header2", "blah"))
84                  .addHeader(new BasicHeader("header3", "blah"))
85                  .setVersion(HttpVersion.HTTP_1_1)
86                  .setEntity("<html><body><h1>Access OK</h1></body></html>", ContentType.TEXT_HTML)
87                  .setEntity("Another entity")
88                  .build();
89  
90          response.addHeader("User-Agent", "test");
91  
92          conn.sendResponseHeader(response);
93          conn.flush();
94  
95          Assertions.assertEquals(1, conn.getEndpointDetails().getResponseCount());
96          final String s = new String(outStream.toByteArray(), StandardCharsets.US_ASCII);
97          Assertions.assertEquals("HTTP/1.1 200 OK\r\nX-Test-Filter: active\r\nheader1: blah\r\nheader2: blah\r\nheader3: blah\r\nUser-Agent: test\r\n\r\n", s);
98          Assertions.assertNotNull(response.getEntity());
99      }
100 
101     @Test
102     void remove() throws IOException, HttpException {
103         final Header header = new BasicHeader("header2", "blah");
104         final ClassicHttpResponse response = ClassicResponseBuilder.create(200)
105                 .setEntity(new StringEntity("123", ContentType.TEXT_PLAIN))
106                 .setHeader("X-Test-Filter", "active")
107                 .addHeader("header1", "blah")
108                 .setHeader(header)
109                 .addHeader(new BasicHeader("header3", "blah"))
110                 .setVersion(HttpVersion.HTTP_1_1)
111                 .setEntity("<html><body><h1>Access OK</h1></body></html>", ContentType.TEXT_HTML)
112                 .setEntity("Another entity")
113                 .removeHeader(header)
114                 .build();
115 
116         response.removeHeaders("X-Test-Filter");
117 
118         conn.sendResponseHeader(response);
119         conn.flush();
120 
121         Assertions.assertEquals(1, conn.getEndpointDetails().getResponseCount());
122         final String s = new String(outStream.toByteArray(), StandardCharsets.US_ASCII);
123         Assertions.assertEquals("HTTP/1.1 200 OK\r\nheader1: blah\r\nheader3: blah\r\n\r\n", s);
124         Assertions.assertNotNull(response.getEntity());
125 
126     }
127 
128     @Test
129     void copy() throws IOException, HttpException {
130         final Header header = new BasicHeader("header2", "blah");
131         final ClassicHttpResponse response = ClassicResponseBuilder.create(200)
132                 .setEntity(new StringEntity("123", ContentType.TEXT_PLAIN))
133                 .addHeader("header1", "blah")
134                 .setHeader(header)
135                 .addHeader(new BasicHeader("header3", "blah"))
136                 .setVersion(HttpVersion.HTTP_1_1)
137                 .setEntity("<html><body><h1>Access OK</h1></body></html>", ContentType.TEXT_HTML)
138                 .setEntity("Another entity")
139                 .removeHeader(header)
140                 .build();
141 
142         final ClassicResponseBuilder classicResponseBuilder = ClassicResponseBuilder.copy(response);
143         final ClassicHttpResponse response2 = classicResponseBuilder.build();
144         conn.sendResponseHeader(response2);
145         conn.flush();
146 
147         Assertions.assertEquals(1, conn.getEndpointDetails().getResponseCount());
148         final String s = new String(outStream.toByteArray(), StandardCharsets.US_ASCII);
149         Assertions.assertEquals("HTTP/1.1 200 OK\r\nheader1: blah\r\nheader3: blah\r\n\r\n", s);
150         Assertions.assertNotNull(response.getEntity());
151         Assertions.assertNotNull(classicResponseBuilder.toString());
152     }
153 }