|
1 | | -/* |
2 | | - * Copyright 2019 Google LLC |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | - * in compliance with the License. You may obtain a copy of the License at |
6 | | - * |
7 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
8 | | - * |
9 | | - * Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | - * or implied. See the License for the specific language governing permissions and limitations under |
12 | | - * the License. |
13 | | - */ |
14 | | - |
15 | | -package com.google.api.client.http.apache.v5; |
16 | | - |
17 | | -import static org.junit.Assert.assertEquals; |
18 | | -import static org.junit.Assert.assertFalse; |
19 | | -import static org.junit.Assert.assertTrue; |
20 | | - |
21 | | -import com.google.api.client.http.ByteArrayContent; |
22 | | -import com.google.api.client.http.HttpContent; |
23 | | -import com.google.api.client.http.InputStreamContent; |
24 | | -import com.google.api.client.http.LowLevelHttpResponse; |
25 | | -import java.io.ByteArrayInputStream; |
26 | | -import java.io.InputStream; |
27 | | -import java.nio.charset.StandardCharsets; |
28 | | -import java.util.Arrays; |
29 | | -import java.util.concurrent.atomic.AtomicInteger; |
30 | | -import org.apache.hc.client5.http.classic.methods.HttpPost; |
31 | | -import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; |
32 | | -import org.apache.hc.core5.http.ClassicHttpRequest; |
33 | | -import org.apache.hc.core5.http.ClassicHttpResponse; |
34 | | -import org.apache.hc.core5.http.ContentType; |
35 | | -import org.apache.hc.core5.http.HttpEntity; |
36 | | -import org.apache.hc.core5.http.HttpHost; |
37 | | -import org.apache.hc.core5.http.io.entity.BasicHttpEntity; |
38 | | -import org.apache.hc.core5.http.protocol.HttpContext; |
39 | | -import org.junit.Test; |
40 | | - |
41 | | -public class Apache5HttpRequestTest { |
42 | | - @Test |
43 | | - public void testContentLengthSet() throws Exception { |
44 | | - HttpUriRequestBase base = new HttpPost("http://www.google.com"); |
45 | | - Apache5HttpRequest request = |
46 | | - new Apache5HttpRequest( |
47 | | - new MockHttpClient() { |
48 | | - @Override |
49 | | - public ClassicHttpResponse executeOpen( |
50 | | - HttpHost target, ClassicHttpRequest request, HttpContext context) { |
51 | | - return new MockClassicHttpResponse(); |
52 | | - } |
53 | | - }, |
54 | | - base); |
55 | | - HttpContent content = |
56 | | - new ByteArrayContent("text/plain", "sample".getBytes(StandardCharsets.UTF_8)); |
57 | | - request.setStreamingContent(content); |
58 | | - request.setContentLength(content.getLength()); |
59 | | - request.execute(); |
60 | | - |
61 | | - assertFalse(base.getEntity().isChunked()); |
62 | | - assertEquals(6, base.getEntity().getContentLength()); |
63 | | - } |
64 | | - |
65 | | - @Test |
66 | | - public void testChunked() throws Exception { |
67 | | - byte[] buf = new byte[300]; |
68 | | - Arrays.fill(buf, (byte) ' '); |
69 | | - HttpUriRequestBase base = new HttpPost("http://www.google.com"); |
70 | | - Apache5HttpRequest request = |
71 | | - new Apache5HttpRequest( |
72 | | - new MockHttpClient() { |
73 | | - @Override |
74 | | - public ClassicHttpResponse executeOpen( |
75 | | - HttpHost target, ClassicHttpRequest request, HttpContext context) { |
76 | | - return new MockClassicHttpResponse(); |
77 | | - } |
78 | | - }, |
79 | | - base); |
80 | | - HttpContent content = new InputStreamContent("text/plain", new ByteArrayInputStream(buf)); |
81 | | - request.setStreamingContent(content); |
82 | | - request.execute(); |
83 | | - |
84 | | - assertTrue(base.getEntity().isChunked()); |
85 | | - assertEquals(-1, base.getEntity().getContentLength()); |
86 | | - } |
87 | | - |
88 | | - @Test |
89 | | - public void testExecute_closeContent_closesResponse() throws Exception { |
90 | | - HttpUriRequestBase base = new HttpPost("http://www.google.com"); |
91 | | - final InputStream responseContentStream = new ByteArrayInputStream(new byte[] {1, 2, 3}); |
92 | | - BasicHttpEntity testEntity = |
93 | | - new BasicHttpEntity(responseContentStream, ContentType.DEFAULT_BINARY); |
94 | | - AtomicInteger closedResponseCounter = new AtomicInteger(0); |
95 | | - ClassicHttpResponse classicResponse = |
96 | | - new MockClassicHttpResponse() { |
97 | | - @Override |
98 | | - public HttpEntity getEntity() { |
99 | | - return testEntity; |
100 | | - } |
101 | | - |
102 | | - @Override |
103 | | - public void close() { |
104 | | - closedResponseCounter.incrementAndGet(); |
105 | | - } |
106 | | - }; |
107 | | - |
108 | | - Apache5HttpRequest request = |
109 | | - new Apache5HttpRequest( |
110 | | - new MockHttpClient() { |
111 | | - @Override |
112 | | - public ClassicHttpResponse executeOpen( |
113 | | - HttpHost target, ClassicHttpRequest request, HttpContext context) { |
114 | | - return classicResponse; |
115 | | - } |
116 | | - }, |
117 | | - base); |
118 | | - LowLevelHttpResponse response = request.execute(); |
119 | | - assertTrue(response instanceof Apache5HttpResponse); |
120 | | - |
121 | | - // we confirm that the classic response we prepared in this test is the same as the content's |
122 | | - // response |
123 | | - assertTrue(response.getContent() instanceof Apache5ResponseContent); |
124 | | - assertEquals(classicResponse, ((Apache5ResponseContent) response.getContent()).getResponse()); |
125 | | - |
126 | | - // we close the response's content stream and confirm the response is also closed |
127 | | - assertEquals(0, closedResponseCounter.get()); |
128 | | - response.getContent().close(); |
129 | | - assertEquals(1, closedResponseCounter.get()); |
130 | | - } |
131 | | -} |
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.api.client.http.apache.v5; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +import static org.junit.Assert.assertFalse; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | + |
| 21 | +import com.google.api.client.http.ByteArrayContent; |
| 22 | +import com.google.api.client.http.HttpContent; |
| 23 | +import com.google.api.client.http.InputStreamContent; |
| 24 | +import com.google.api.client.http.LowLevelHttpResponse; |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
| 31 | +import org.apache.hc.client5.http.classic.methods.HttpPost; |
| 32 | +import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; |
| 33 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 34 | +import org.apache.hc.core5.http.ClassicHttpRequest; |
| 35 | +import org.apache.hc.core5.http.ClassicHttpResponse; |
| 36 | +import org.apache.hc.core5.http.ContentType; |
| 37 | +import org.apache.hc.core5.http.HttpEntity; |
| 38 | +import org.apache.hc.core5.http.HttpHost; |
| 39 | +import org.apache.hc.core5.http.io.entity.BasicHttpEntity; |
| 40 | +import org.apache.hc.core5.http.protocol.HttpContext; |
| 41 | +import org.apache.hc.core5.util.Timeout; |
| 42 | +import org.junit.Test; |
| 43 | + |
| 44 | +public class Apache5HttpRequestTest { |
| 45 | + @Test |
| 46 | + public void testContentLengthSet() throws Exception { |
| 47 | + HttpUriRequestBase base = new HttpPost("http://www.google.com"); |
| 48 | + Apache5HttpRequest request = |
| 49 | + new Apache5HttpRequest( |
| 50 | + new MockHttpClient() { |
| 51 | + @Override |
| 52 | + public ClassicHttpResponse executeOpen( |
| 53 | + HttpHost target, ClassicHttpRequest request, HttpContext context) { |
| 54 | + return new MockClassicHttpResponse(); |
| 55 | + } |
| 56 | + }, |
| 57 | + base); |
| 58 | + HttpContent content = |
| 59 | + new ByteArrayContent("text/plain", "sample".getBytes(StandardCharsets.UTF_8)); |
| 60 | + request.setStreamingContent(content); |
| 61 | + request.setContentLength(content.getLength()); |
| 62 | + request.execute(); |
| 63 | + |
| 64 | + assertFalse(base.getEntity().isChunked()); |
| 65 | + assertEquals(6, base.getEntity().getContentLength()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testChunked() throws Exception { |
| 70 | + byte[] buf = new byte[300]; |
| 71 | + Arrays.fill(buf, (byte) ' '); |
| 72 | + HttpUriRequestBase base = new HttpPost("http://www.google.com"); |
| 73 | + Apache5HttpRequest request = |
| 74 | + new Apache5HttpRequest( |
| 75 | + new MockHttpClient() { |
| 76 | + @Override |
| 77 | + public ClassicHttpResponse executeOpen( |
| 78 | + HttpHost target, ClassicHttpRequest request, HttpContext context) { |
| 79 | + return new MockClassicHttpResponse(); |
| 80 | + } |
| 81 | + }, |
| 82 | + base); |
| 83 | + HttpContent content = new InputStreamContent("text/plain", new ByteArrayInputStream(buf)); |
| 84 | + request.setStreamingContent(content); |
| 85 | + request.execute(); |
| 86 | + |
| 87 | + assertTrue(base.getEntity().isChunked()); |
| 88 | + assertEquals(-1, base.getEntity().getContentLength()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testExecute_closeContent_closesResponse() throws Exception { |
| 93 | + HttpUriRequestBase base = new HttpPost("http://www.google.com"); |
| 94 | + final InputStream responseContentStream = new ByteArrayInputStream(new byte[] {1, 2, 3}); |
| 95 | + BasicHttpEntity testEntity = |
| 96 | + new BasicHttpEntity(responseContentStream, ContentType.DEFAULT_BINARY); |
| 97 | + AtomicInteger closedResponseCounter = new AtomicInteger(0); |
| 98 | + ClassicHttpResponse classicResponse = |
| 99 | + new MockClassicHttpResponse() { |
| 100 | + @Override |
| 101 | + public HttpEntity getEntity() { |
| 102 | + return testEntity; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void close() { |
| 107 | + closedResponseCounter.incrementAndGet(); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + Apache5HttpRequest request = |
| 112 | + new Apache5HttpRequest( |
| 113 | + new MockHttpClient() { |
| 114 | + @Override |
| 115 | + public ClassicHttpResponse executeOpen( |
| 116 | + HttpHost target, ClassicHttpRequest request, HttpContext context) { |
| 117 | + return classicResponse; |
| 118 | + } |
| 119 | + }, |
| 120 | + base); |
| 121 | + LowLevelHttpResponse response = request.execute(); |
| 122 | + assertTrue(response instanceof Apache5HttpResponse); |
| 123 | + |
| 124 | + // we confirm that the classic response we prepared in this test is the same as the content's |
| 125 | + // response |
| 126 | + assertTrue(response.getContent() instanceof Apache5ResponseContent); |
| 127 | + assertEquals(classicResponse, ((Apache5ResponseContent) response.getContent()).getResponse()); |
| 128 | + |
| 129 | + // we close the response's content stream and confirm the response is also closed |
| 130 | + assertEquals(0, closedResponseCounter.get()); |
| 131 | + response.getContent().close(); |
| 132 | + assertEquals(1, closedResponseCounter.get()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testSetTimeout() throws Exception { |
| 137 | + HttpUriRequestBase base = new HttpPost("http://www.google.com"); |
| 138 | + Apache5HttpRequest request = |
| 139 | + new Apache5HttpRequest( |
| 140 | + new MockHttpClient() { |
| 141 | + @Override |
| 142 | + public ClassicHttpResponse executeOpen( |
| 143 | + HttpHost target, ClassicHttpRequest request, HttpContext context) { |
| 144 | + return new MockClassicHttpResponse(); |
| 145 | + } |
| 146 | + }, |
| 147 | + base); |
| 148 | + request.setTimeout(100, 200); |
| 149 | + request.execute(); |
| 150 | + |
| 151 | + RequestConfig config = base.getConfig(); |
| 152 | + assertEquals(Timeout.of(100, TimeUnit.MILLISECONDS), config.getConnectTimeout()); |
| 153 | + assertEquals(Timeout.of(200, TimeUnit.MILLISECONDS), config.getResponseTimeout()); |
| 154 | + assertEquals(Timeout.of(100, TimeUnit.MILLISECONDS), config.getConnectionRequestTimeout()); |
| 155 | + } |
| 156 | +} |
0 commit comments