/*
* Copyright 2002-2014 the original author or authors.
*
* 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.springframework.http.client;
import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.zip.GZIPOutputStream;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.FileCopyUtils;
import android.test.suitebuilder.annotation.MediumTest;
public abstract class HttpComponentsAbstractHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
@Override
public void testHttpMethods() throws Exception {
super.testHttpMethods();
assertHttpMethod("patch", HttpMethod.PATCH);
}
@MediumTest
@Override
public void testGetAcceptEncodingGzip() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/gzip"), HttpMethod.GET);
assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod());
request.getHeaders().add("Accept-Encoding", "gzip");
ClientHttpResponse response = request.execute();
try {
assertNotNull(response.getStatusText());
assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
// HttpClient seamlessly handles gzip encoded responses
assertFalse("Header found", response.getHeaders().containsKey("Content-Encoding"));
final byte[] body = "gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip gzip "
.getBytes("UTF-8");
byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
assertTrue("Invalid body", Arrays.equals(body, result));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(body.length);
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
FileCopyUtils.copy(body, gzipOutputStream);
long contentLength = response.getHeaders().getContentLength();
// the content-length header is not being set in a gzip'd response
assertEquals("Invalid content-length", -1, contentLength);
} finally {
response.close();
}
}
}