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.client5.http.impl.classic;
29  
30  import org.apache.hc.client5.http.classic.ExecRuntime;
31  import org.apache.hc.core5.function.Supplier;
32  import org.apache.hc.core5.http.ClassicHttpResponse;
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.HttpEntity;
35  import org.apache.hc.core5.http.impl.io.ChunkedInputStream;
36  import org.apache.hc.core5.http.impl.io.SessionInputBufferImpl;
37  import org.apache.hc.core5.http.io.SessionInputBuffer;
38  import org.junit.Assert;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.mockito.ArgumentCaptor;
42  import org.mockito.Mock;
43  import org.mockito.Mockito;
44  import org.mockito.MockitoAnnotations;
45  
46  import java.io.ByteArrayInputStream;
47  import java.io.InputStream;
48  import java.util.List;
49  
50  public class TestResponseEntityProxy {
51  
52      @Mock
53      private ClassicHttpResponse response;
54      @Mock
55      private ExecRuntime execRuntime;
56      @Mock
57      private HttpEntity entity;
58  
59      @Before
60      public void setUp() {
61          MockitoAnnotations.initMocks(this);
62          Mockito.when(entity.isStreaming()).thenReturn(Boolean.TRUE);
63          Mockito.when(response.getEntity()).thenReturn(entity);
64      }
65  
66      @Test
67      public void testGetTrailersWithNoChunkedInputStream() throws Exception {
68          final ByteArrayInputStream inputStream = new ByteArrayInputStream("Test payload".getBytes());
69          Mockito.when(entity.getContent()).thenReturn(inputStream);
70          final ArgumentCaptor<HttpEntity> httpEntityArgumentCaptor = ArgumentCaptor.forClass(HttpEntity.class);
71  
72          ResponseEntityProxy.enhance(response, execRuntime);
73  
74          Mockito.verify(response).setEntity(httpEntityArgumentCaptor.capture());
75          final HttpEntity wrappedEntity = httpEntityArgumentCaptor.getValue();
76  
77          final InputStream is = wrappedEntity.getContent();
78          while (is.read() != -1) {} // read until the end
79          final Supplier<List<? extends Header>> trailers = wrappedEntity.getTrailers();
80  
81          Assert.assertTrue(trailers.get().isEmpty());
82      }
83  
84      @Test
85      public void testGetTrailersWithChunkedInputStream() throws Exception {
86          final SessionInputBuffer sessionInputBuffer = new SessionInputBufferImpl(100);
87          final ByteArrayInputStream inputStream = new ByteArrayInputStream("0\r\nX-Test-Trailer-Header: test\r\n".getBytes());
88          final ChunkedInputStream chunkedInputStream = new ChunkedInputStream(sessionInputBuffer, inputStream);
89  
90          Mockito.when(entity.getContent()).thenReturn(chunkedInputStream);
91          final ArgumentCaptor<HttpEntity> httpEntityArgumentCaptor = ArgumentCaptor.forClass(HttpEntity.class);
92  
93          ResponseEntityProxy.enhance(response, execRuntime);
94  
95          Mockito.verify(response).setEntity(httpEntityArgumentCaptor.capture());
96          final HttpEntity wrappedEntity = httpEntityArgumentCaptor.getValue();
97  
98          final InputStream is = wrappedEntity.getContent();
99          while (is.read() != -1) {} // consume the stream so it can reach to trailers and parse
100         final Supplier<List<? extends Header>> trailers = wrappedEntity.getTrailers();
101         final List<? extends Header> headers = trailers.get();
102 
103         Assert.assertEquals(1, headers.size());
104         final Header header = headers.get(0);
105         Assert.assertEquals("X-Test-Trailer-Header", header.getName());
106         Assert.assertEquals("test", header.getValue());
107     }
108 }