Skip to content

Commit 52a3b0f

Browse files
committed
chore: convert line endings
1 parent d6d770a commit 52a3b0f

1 file changed

Lines changed: 156 additions & 156 deletions

File tree

Lines changed: 156 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,156 @@
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-
}
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

Comments
 (0)