ocket8888 commented on code in PR #4074:
URL: https://github.com/apache/trafficcontrol/pull/4074#discussion_r1012327712


##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/LocationsTest.java:
##########
@@ -139,4 +144,38 @@ public void itGetsCachesForALocation() throws Exception {
                        if (response != null) response.close();
                }
        }
+
+       @Test
+       public void itHandlesHeadRequests() throws Exception {
+               final List<String> paths = new ArrayList<String>();
+               paths.add("http://localhost:3333/crs/locations";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+
+               CloseableHttpResponse response = null;
+
+               try {
+                       final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/locations";);
+                       response = closeableHttpClient.execute(httpGet);
+
+                       ObjectMapper objectMapper = new ObjectMapper(new 
JsonFactory());
+                       JsonNode jsonNode = 
objectMapper.readTree(EntityUtils.toString(response.getEntity()));
+
+                       String location = 
jsonNode.get("locations").get(0).asText();
+                       paths.add("http://localhost:3333/crs/locations/"; + 
location + "/caches");
+               } catch (Exception e) {
+                       fail(e.getMessage());
+               } finally {
+                       if (response != null) response.close();
+               }
+
+               for (final String path : paths) {
+                       final HttpHead httpHead = new HttpHead(path);
+                       try {
+                               response = 
closeableHttpClient.execute(httpHead);
+                               
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));

Review Comment:
   Shouldn't you also check that it sets the Content-Length header?



##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/BufferedResponseTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.traffic_control.traffic_router.core.external;
+
+import org.apache.traffic_control.traffic_router.core.http.RouterFilter;
+import org.apache.traffic_control.traffic_router.core.util.ExternalTest;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.catalina.LifecycleException;
+import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@Category(ExternalTest.class)
+public class BufferedResponseTest {
+       final private String routerHttpPort = 
System.getProperty("routerHttpPort", "8888");
+       private CloseableHttpClient httpClient;
+
+       @Before
+       public void before() throws LifecycleException {
+               httpClient = HttpClientBuilder.create().build();
+       }
+
+       @After
+       public void after() throws Exception {
+               if (httpClient != null) httpClient.close();
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderFor404() throws IOException {
+               final String encodedUrl = 
URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff";,
 "utf-8");
+               final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/deliveryservices?url="; + encodedUrl);
+               CloseableHttpResponse response = null;
+
+               try {
+                       response = httpClient.execute(httpGet);
+                       assertThat(response.getStatusLine().getStatusCode(), 
equalTo(404));
+                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());

Review Comment:
   should use `org.springframework.http.HttpHeaders.TRANSFER_ENCODING` instead 
of string literal.



##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/BufferedResponseTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.traffic_control.traffic_router.core.external;
+
+import org.apache.traffic_control.traffic_router.core.http.RouterFilter;
+import org.apache.traffic_control.traffic_router.core.util.ExternalTest;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.catalina.LifecycleException;
+import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@Category(ExternalTest.class)
+public class BufferedResponseTest {
+       final private String routerHttpPort = 
System.getProperty("routerHttpPort", "8888");
+       private CloseableHttpClient httpClient;
+
+       @Before
+       public void before() throws LifecycleException {
+               httpClient = HttpClientBuilder.create().build();
+       }
+
+       @After
+       public void after() throws Exception {
+               if (httpClient != null) httpClient.close();
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderFor404() throws IOException {
+               final String encodedUrl = 
URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff";,
 "utf-8");
+               final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/deliveryservices?url="; + encodedUrl);
+               CloseableHttpResponse response = null;
+
+               try {
+                       response = httpClient.execute(httpGet);
+                       assertThat(response.getStatusLine().getStatusCode(), 
equalTo(404));
+                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                       assertThat(response.getFirstHeader("Content-Length"), 
notNullValue());
+               } finally {
+                       if (response != null) response.close();
+               }
+       }
+
+       @Test
+       public void itSetsTheSameContentLengthForHeadAndGet() throws 
IOException {
+               final List<String> paths = new ArrayList<>();
+               paths.add("http://localhost:3333/crs/stats";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+               
paths.add("http://localhost:3333/crs/consistenthash/deliveryservice?deliveryServiceId=csd-target-1&requestPath=/";);
+
+               for (final String path : paths) {
+                       final List<HttpRequestBase> requests = new 
ArrayList<>();
+                       requests.add(new HttpHead(path));
+                       requests.add(new HttpGet(path));
+
+                       final List<Integer> contentLengths = new ArrayList<>();
+
+                       for (final HttpRequestBase request : requests) {
+                               CloseableHttpResponse response = null;
+
+                               try {
+                                       response = httpClient.execute(request);
+                                       final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                                       assertThat(contentLengthHeader, 
notNullValue());
+                                       
contentLengths.add(Integer.parseInt(contentLengthHeader.getValue()));
+                               } finally {
+                                       if (response != null) response.close();
+                               }
+                       }
+
+                       assertThat(contentLengths.size(), equalTo(2));
+                       assertThat("Expected HEAD and GET requests for " + path 
+ "to have the same Content-Length", contentLengths.get(0), 
equalTo(contentLengths.get(1)));

Review Comment:
   same as above



##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/BufferedResponseTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.traffic_control.traffic_router.core.external;
+
+import org.apache.traffic_control.traffic_router.core.http.RouterFilter;
+import org.apache.traffic_control.traffic_router.core.util.ExternalTest;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.catalina.LifecycleException;
+import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@Category(ExternalTest.class)
+public class BufferedResponseTest {
+       final private String routerHttpPort = 
System.getProperty("routerHttpPort", "8888");
+       private CloseableHttpClient httpClient;
+
+       @Before
+       public void before() throws LifecycleException {
+               httpClient = HttpClientBuilder.create().build();
+       }
+
+       @After
+       public void after() throws Exception {
+               if (httpClient != null) httpClient.close();
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderFor404() throws IOException {
+               final String encodedUrl = 
URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff";,
 "utf-8");
+               final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/deliveryservices?url="; + encodedUrl);
+               CloseableHttpResponse response = null;
+
+               try {
+                       response = httpClient.execute(httpGet);
+                       assertThat(response.getStatusLine().getStatusCode(), 
equalTo(404));
+                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                       assertThat(response.getFirstHeader("Content-Length"), 
notNullValue());
+               } finally {
+                       if (response != null) response.close();
+               }
+       }
+
+       @Test
+       public void itSetsTheSameContentLengthForHeadAndGet() throws 
IOException {
+               final List<String> paths = new ArrayList<>();
+               paths.add("http://localhost:3333/crs/stats";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+               
paths.add("http://localhost:3333/crs/consistenthash/deliveryservice?deliveryServiceId=csd-target-1&requestPath=/";);
+
+               for (final String path : paths) {
+                       final List<HttpRequestBase> requests = new 
ArrayList<>();
+                       requests.add(new HttpHead(path));
+                       requests.add(new HttpGet(path));
+
+                       final List<Integer> contentLengths = new ArrayList<>();
+
+                       for (final HttpRequestBase request : requests) {
+                               CloseableHttpResponse response = null;
+
+                               try {
+                                       response = httpClient.execute(request);
+                                       final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());

Review Comment:
   same as the above 2



##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/BufferedResponseTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.traffic_control.traffic_router.core.external;
+
+import org.apache.traffic_control.traffic_router.core.http.RouterFilter;
+import org.apache.traffic_control.traffic_router.core.util.ExternalTest;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.catalina.LifecycleException;
+import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@Category(ExternalTest.class)
+public class BufferedResponseTest {
+       final private String routerHttpPort = 
System.getProperty("routerHttpPort", "8888");
+       private CloseableHttpClient httpClient;
+
+       @Before
+       public void before() throws LifecycleException {
+               httpClient = HttpClientBuilder.create().build();
+       }
+
+       @After
+       public void after() throws Exception {
+               if (httpClient != null) httpClient.close();
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderFor404() throws IOException {
+               final String encodedUrl = 
URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff";,
 "utf-8");
+               final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/deliveryservices?url="; + encodedUrl);
+               CloseableHttpResponse response = null;
+
+               try {
+                       response = httpClient.execute(httpGet);
+                       assertThat(response.getStatusLine().getStatusCode(), 
equalTo(404));
+                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                       assertThat(response.getFirstHeader("Content-Length"), 
notNullValue());
+               } finally {
+                       if (response != null) response.close();
+               }
+       }
+
+       @Test
+       public void itSetsTheSameContentLengthForHeadAndGet() throws 
IOException {
+               final List<String> paths = new ArrayList<>();
+               paths.add("http://localhost:3333/crs/stats";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+               
paths.add("http://localhost:3333/crs/consistenthash/deliveryservice?deliveryServiceId=csd-target-1&requestPath=/";);
+
+               for (final String path : paths) {
+                       final List<HttpRequestBase> requests = new 
ArrayList<>();
+                       requests.add(new HttpHead(path));
+                       requests.add(new HttpGet(path));
+
+                       final List<Integer> contentLengths = new ArrayList<>();
+
+                       for (final HttpRequestBase request : requests) {
+                               CloseableHttpResponse response = null;
+
+                               try {
+                                       response = httpClient.execute(request);
+                                       final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                                       assertThat(contentLengthHeader, 
notNullValue());
+                                       
contentLengths.add(Integer.parseInt(contentLengthHeader.getValue()));
+                               } finally {
+                                       if (response != null) response.close();
+                               }
+                       }
+
+                       assertThat(contentLengths.size(), equalTo(2));
+                       assertThat("Expected HEAD and GET requests for " + path 
+ "to have the same Content-Length", contentLengths.get(0), 
equalTo(contentLengths.get(1)));
+               }
+       }
+
+       @Test
+       public void itSetsAnAccurateContentLengthForGet() throws IOException {
+               final List<String> paths = new ArrayList<>();
+               paths.add("http://localhost:3333/crs/stats";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+               
paths.add("http://localhost:3333/crs/consistenthash/deliveryservice?deliveryServiceId=csd-target-1&requestPath=/";);
+
+               for (final String path : paths) {
+                       CloseableHttpResponse response = null;
+
+                       try {
+                               final HttpGet httpGet = new HttpGet(path);
+                               response = httpClient.execute(httpGet);
+
+                               final ObjectMapper objectMapper = new 
ObjectMapper(new JsonFactory());
+                               final String json = 
EntityUtils.toString(response.getEntity());
+                               final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                               /* If the content length is too low and cuts 
off the response
+                                * body, objectMapper.readTree(json) will 
likely throw a
+                                * JsonProcessingException.
+                                */
+                               objectMapper.readTree(json);
+                               
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                               assertThat(contentLengthHeader, notNullValue());
+                               
assertThat(Integer.parseInt(contentLengthHeader.getValue()), 
equalTo(json.length()));
+                       } finally {
+                               if (response != null) response.close();
+                       }
+               }
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderForDeliveryServiceSteering() 
throws IOException {
+               final String testHostName = 
"tr.client-steering-test-1.thecdn.example.com";
+               final RequestConfig config = 
RequestConfig.custom().setRedirectsEnabled(false).build();
+               final List<String> paths = new ArrayList<>();
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&format=json");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + 
RouterFilter.REDIRECT_QUERY_PARAM + "=false");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + 
RouterFilter.REDIRECT_QUERY_PARAM + "=true");
+
+
+               for (final String path : paths) {
+                       final List<HttpRequestBase> requests = new 
ArrayList<>();
+                       requests.add(new HttpHead("http://localhost:"; + 
routerHttpPort + path));
+                       requests.add(new HttpGet("http://localhost:"; + 
routerHttpPort + path));
+
+                       final List<Integer> contentLengths = new ArrayList<>();
+
+                       for (final HttpRequestBase request : requests) {
+                               CloseableHttpResponse response = null;
+                               request.setConfig(config);
+                               request.addHeader("Host", testHostName);
+
+                               try {
+                                       response = httpClient.execute(request);
+                                       final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                                       assertThat(contentLengthHeader, 
notNullValue());
+                                       
contentLengths.add(Integer.parseInt(contentLengthHeader.getValue()));
+                               } finally {
+                                       if (response != null) response.close();
+                               }
+                       }
+
+                       assertThat(contentLengths.size(), equalTo(2));
+                       assertThat("Expected HEAD and GET requests for " + 
testHostName + path + "to have the same Content-Length", contentLengths.get(0), 
equalTo(contentLengths.get(1)));

Review Comment:
   I think you're missing a space before "to", causing it to get tacked on to 
the request path which can make it a bit hard to read.



##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/BufferedResponseTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.traffic_control.traffic_router.core.external;
+
+import org.apache.traffic_control.traffic_router.core.http.RouterFilter;
+import org.apache.traffic_control.traffic_router.core.util.ExternalTest;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.catalina.LifecycleException;
+import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@Category(ExternalTest.class)
+public class BufferedResponseTest {
+       final private String routerHttpPort = 
System.getProperty("routerHttpPort", "8888");
+       private CloseableHttpClient httpClient;
+
+       @Before
+       public void before() throws LifecycleException {
+               httpClient = HttpClientBuilder.create().build();
+       }
+
+       @After
+       public void after() throws Exception {
+               if (httpClient != null) httpClient.close();
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderFor404() throws IOException {
+               final String encodedUrl = 
URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff";,
 "utf-8");
+               final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/deliveryservices?url="; + encodedUrl);
+               CloseableHttpResponse response = null;
+
+               try {
+                       response = httpClient.execute(httpGet);
+                       assertThat(response.getStatusLine().getStatusCode(), 
equalTo(404));
+                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());

Review Comment:
   `Assert.assertThat` is deprecated, deprecation message says to use 
`MatcherAssert.assertThat` instead.



##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/BufferedResponseTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.traffic_control.traffic_router.core.external;
+
+import org.apache.traffic_control.traffic_router.core.http.RouterFilter;
+import org.apache.traffic_control.traffic_router.core.util.ExternalTest;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.catalina.LifecycleException;
+import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@Category(ExternalTest.class)
+public class BufferedResponseTest {
+       final private String routerHttpPort = 
System.getProperty("routerHttpPort", "8888");
+       private CloseableHttpClient httpClient;
+
+       @Before
+       public void before() throws LifecycleException {
+               httpClient = HttpClientBuilder.create().build();
+       }
+
+       @After
+       public void after() throws Exception {
+               if (httpClient != null) httpClient.close();
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderFor404() throws IOException {
+               final String encodedUrl = 
URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff";,
 "utf-8");
+               final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/deliveryservices?url="; + encodedUrl);
+               CloseableHttpResponse response = null;
+
+               try {
+                       response = httpClient.execute(httpGet);
+                       assertThat(response.getStatusLine().getStatusCode(), 
equalTo(404));
+                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                       assertThat(response.getFirstHeader("Content-Length"), 
notNullValue());
+               } finally {
+                       if (response != null) response.close();
+               }
+       }
+
+       @Test
+       public void itSetsTheSameContentLengthForHeadAndGet() throws 
IOException {
+               final List<String> paths = new ArrayList<>();
+               paths.add("http://localhost:3333/crs/stats";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+               
paths.add("http://localhost:3333/crs/consistenthash/deliveryservice?deliveryServiceId=csd-target-1&requestPath=/";);
+
+               for (final String path : paths) {
+                       final List<HttpRequestBase> requests = new 
ArrayList<>();
+                       requests.add(new HttpHead(path));
+                       requests.add(new HttpGet(path));
+
+                       final List<Integer> contentLengths = new ArrayList<>();
+
+                       for (final HttpRequestBase request : requests) {
+                               CloseableHttpResponse response = null;
+
+                               try {
+                                       response = httpClient.execute(request);
+                                       final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                                       assertThat(contentLengthHeader, 
notNullValue());
+                                       
contentLengths.add(Integer.parseInt(contentLengthHeader.getValue()));
+                               } finally {
+                                       if (response != null) response.close();
+                               }
+                       }
+
+                       assertThat(contentLengths.size(), equalTo(2));
+                       assertThat("Expected HEAD and GET requests for " + path 
+ "to have the same Content-Length", contentLengths.get(0), 
equalTo(contentLengths.get(1)));
+               }
+       }
+
+       @Test
+       public void itSetsAnAccurateContentLengthForGet() throws IOException {
+               final List<String> paths = new ArrayList<>();
+               paths.add("http://localhost:3333/crs/stats";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+               
paths.add("http://localhost:3333/crs/consistenthash/deliveryservice?deliveryServiceId=csd-target-1&requestPath=/";);
+
+               for (final String path : paths) {
+                       CloseableHttpResponse response = null;
+
+                       try {
+                               final HttpGet httpGet = new HttpGet(path);
+                               response = httpClient.execute(httpGet);
+
+                               final ObjectMapper objectMapper = new 
ObjectMapper(new JsonFactory());
+                               final String json = 
EntityUtils.toString(response.getEntity());
+                               final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                               /* If the content length is too low and cuts 
off the response
+                                * body, objectMapper.readTree(json) will 
likely throw a
+                                * JsonProcessingException.
+                                */
+                               objectMapper.readTree(json);
+                               
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                               assertThat(contentLengthHeader, notNullValue());
+                               
assertThat(Integer.parseInt(contentLengthHeader.getValue()), 
equalTo(json.length()));
+                       } finally {
+                               if (response != null) response.close();
+                       }
+               }
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderForDeliveryServiceSteering() 
throws IOException {
+               final String testHostName = 
"tr.client-steering-test-1.thecdn.example.com";
+               final RequestConfig config = 
RequestConfig.custom().setRedirectsEnabled(false).build();
+               final List<String> paths = new ArrayList<>();
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&format=json");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + 
RouterFilter.REDIRECT_QUERY_PARAM + "=false");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + 
RouterFilter.REDIRECT_QUERY_PARAM + "=true");
+
+
+               for (final String path : paths) {
+                       final List<HttpRequestBase> requests = new 
ArrayList<>();
+                       requests.add(new HttpHead("http://localhost:"; + 
routerHttpPort + path));
+                       requests.add(new HttpGet("http://localhost:"; + 
routerHttpPort + path));
+
+                       final List<Integer> contentLengths = new ArrayList<>();
+
+                       for (final HttpRequestBase request : requests) {
+                               CloseableHttpResponse response = null;
+                               request.setConfig(config);
+                               request.addHeader("Host", testHostName);
+
+                               try {
+                                       response = httpClient.execute(request);
+                                       final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());

Review Comment:
   Same as above



##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/BufferedResponseTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.traffic_control.traffic_router.core.external;
+
+import org.apache.traffic_control.traffic_router.core.http.RouterFilter;
+import org.apache.traffic_control.traffic_router.core.util.ExternalTest;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.catalina.LifecycleException;
+import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@Category(ExternalTest.class)
+public class BufferedResponseTest {
+       final private String routerHttpPort = 
System.getProperty("routerHttpPort", "8888");
+       private CloseableHttpClient httpClient;
+
+       @Before
+       public void before() throws LifecycleException {
+               httpClient = HttpClientBuilder.create().build();
+       }
+
+       @After
+       public void after() throws Exception {
+               if (httpClient != null) httpClient.close();
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderFor404() throws IOException {
+               final String encodedUrl = 
URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff";,
 "utf-8");
+               final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/deliveryservices?url="; + encodedUrl);
+               CloseableHttpResponse response = null;
+
+               try {
+                       response = httpClient.execute(httpGet);
+                       assertThat(response.getStatusLine().getStatusCode(), 
equalTo(404));
+                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                       assertThat(response.getFirstHeader("Content-Length"), 
notNullValue());
+               } finally {
+                       if (response != null) response.close();
+               }
+       }
+
+       @Test
+       public void itSetsTheSameContentLengthForHeadAndGet() throws 
IOException {
+               final List<String> paths = new ArrayList<>();
+               paths.add("http://localhost:3333/crs/stats";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+               
paths.add("http://localhost:3333/crs/consistenthash/deliveryservice?deliveryServiceId=csd-target-1&requestPath=/";);
+
+               for (final String path : paths) {
+                       final List<HttpRequestBase> requests = new 
ArrayList<>();
+                       requests.add(new HttpHead(path));
+                       requests.add(new HttpGet(path));
+
+                       final List<Integer> contentLengths = new ArrayList<>();
+
+                       for (final HttpRequestBase request : requests) {
+                               CloseableHttpResponse response = null;
+
+                               try {
+                                       response = httpClient.execute(request);
+                                       final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                                       assertThat(contentLengthHeader, 
notNullValue());
+                                       
contentLengths.add(Integer.parseInt(contentLengthHeader.getValue()));
+                               } finally {
+                                       if (response != null) response.close();
+                               }
+                       }
+
+                       assertThat(contentLengths.size(), equalTo(2));
+                       assertThat("Expected HEAD and GET requests for " + path 
+ "to have the same Content-Length", contentLengths.get(0), 
equalTo(contentLengths.get(1)));
+               }
+       }
+
+       @Test
+       public void itSetsAnAccurateContentLengthForGet() throws IOException {
+               final List<String> paths = new ArrayList<>();
+               paths.add("http://localhost:3333/crs/stats";);
+               paths.add("http://localhost:3333/crs/locations/caches";);
+               
paths.add("http://localhost:3333/crs/consistenthash/deliveryservice?deliveryServiceId=csd-target-1&requestPath=/";);
+
+               for (final String path : paths) {
+                       CloseableHttpResponse response = null;
+
+                       try {
+                               final HttpGet httpGet = new HttpGet(path);
+                               response = httpClient.execute(httpGet);
+
+                               final ObjectMapper objectMapper = new 
ObjectMapper(new JsonFactory());
+                               final String json = 
EntityUtils.toString(response.getEntity());
+                               final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                               /* If the content length is too low and cuts 
off the response
+                                * body, objectMapper.readTree(json) will 
likely throw a
+                                * JsonProcessingException.
+                                */
+                               objectMapper.readTree(json);
+                               
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                               assertThat(contentLengthHeader, notNullValue());
+                               
assertThat(Integer.parseInt(contentLengthHeader.getValue()), 
equalTo(json.length()));
+                       } finally {
+                               if (response != null) response.close();
+                       }
+               }
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderForDeliveryServiceSteering() 
throws IOException {
+               final String testHostName = 
"tr.client-steering-test-1.thecdn.example.com";
+               final RequestConfig config = 
RequestConfig.custom().setRedirectsEnabled(false).build();
+               final List<String> paths = new ArrayList<>();
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&format=json");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + 
RouterFilter.REDIRECT_QUERY_PARAM + "=false");
+               
paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + 
RouterFilter.REDIRECT_QUERY_PARAM + "=true");
+
+
+               for (final String path : paths) {
+                       final List<HttpRequestBase> requests = new 
ArrayList<>();
+                       requests.add(new HttpHead("http://localhost:"; + 
routerHttpPort + path));
+                       requests.add(new HttpGet("http://localhost:"; + 
routerHttpPort + path));
+
+                       final List<Integer> contentLengths = new ArrayList<>();
+
+                       for (final HttpRequestBase request : requests) {
+                               CloseableHttpResponse response = null;
+                               request.setConfig(config);
+                               request.addHeader("Host", testHostName);
+
+                               try {
+                                       response = httpClient.execute(request);
+                                       final Header contentLengthHeader = 
response.getFirstHeader("Content-Length");
+
+                                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                                       assertThat(contentLengthHeader, 
notNullValue());
+                                       
contentLengths.add(Integer.parseInt(contentLengthHeader.getValue()));
+                               } finally {
+                                       if (response != null) response.close();
+                               }
+                       }
+
+                       assertThat(contentLengths.size(), equalTo(2));
+                       assertThat("Expected HEAD and GET requests for " + 
testHostName + path + "to have the same Content-Length", contentLengths.get(0), 
equalTo(contentLengths.get(1)));

Review Comment:
   if TR's response isn't as expected, this could throw an 
`IndexOutOfBoundsException`. Idk much about Java testing, but you have a `try` 
block to catch something above, so is that alright? It also doesn't seem like 
an `IOException` so I'm not sure why the compiler isn't mad



##########
traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/external/BufferedResponseTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.traffic_control.traffic_router.core.external;
+
+import org.apache.traffic_control.traffic_router.core.http.RouterFilter;
+import org.apache.traffic_control.traffic_router.core.util.ExternalTest;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.catalina.LifecycleException;
+import org.apache.http.Header;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@Category(ExternalTest.class)
+public class BufferedResponseTest {
+       final private String routerHttpPort = 
System.getProperty("routerHttpPort", "8888");
+       private CloseableHttpClient httpClient;
+
+       @Before
+       public void before() throws LifecycleException {
+               httpClient = HttpClientBuilder.create().build();
+       }
+
+       @After
+       public void after() throws Exception {
+               if (httpClient != null) httpClient.close();
+       }
+
+       @Test
+       public void itSetsContentLengthHeaderFor404() throws IOException {
+               final String encodedUrl = 
URLEncoder.encode("http://trafficrouter01.somedeliveryservice.somecdn.domain.foo/stuff";,
 "utf-8");
+               final HttpGet httpGet = new 
HttpGet("http://localhost:3333/crs/deliveryservices?url="; + encodedUrl);
+               CloseableHttpResponse response = null;
+
+               try {
+                       response = httpClient.execute(httpGet);
+                       assertThat(response.getStatusLine().getStatusCode(), 
equalTo(404));
+                       
assertThat(response.getFirstHeader("Transfer-Encoding"), nullValue());
+                       assertThat(response.getFirstHeader("Content-Length"), 
notNullValue());

Review Comment:
   should use `org.springframework.http.HttpHeaders.CONTENT_LENGTH` instead of 
string literal.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to