fixed content-disposition tests

This commit is contained in:
Adrian Cole 2010-09-18 18:51:13 -07:00
parent a8852e54f9
commit 50a44b91c4
4 changed files with 61 additions and 11 deletions

View File

@ -203,6 +203,10 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, request.getPayload().getContentType()); connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, request.getPayload().getContentType());
if (request.getPayload().getContentDisposition() != null) if (request.getPayload().getContentDisposition() != null)
connection.setRequestProperty("Content-Disposition", request.getPayload().getContentDisposition()); connection.setRequestProperty("Content-Disposition", request.getPayload().getContentDisposition());
if (request.getPayload().getContentEncoding() != null)
connection.setRequestProperty("Content-Encoding", request.getPayload().getContentEncoding());
if (request.getPayload().getContentLanguage() != null)
connection.setRequestProperty("Content-Language", request.getPayload().getContentLanguage());
if (chunked) { if (chunked) {
connection.setChunkedStreamingMode(8196); connection.setChunkedStreamingMode(8196);
} else { } else {

View File

@ -241,6 +241,26 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
assertEquals(headers.get("x-Content-Disposition"), Collections.singleton("attachment; filename=photo.jpg")); assertEquals(headers.get("x-Content-Disposition"), Collections.singleton("attachment; filename=photo.jpg"));
payload.release(); payload.release();
} }
@Test(invocationCount = 5, timeOut = 5000)
public void testPostContentEncoding() throws ExecutionException, InterruptedException, TimeoutException,
IOException {
Payload payload = Payloads.newStringPayload("foo");
payload.setContentEncoding("gzip");
Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
assertEquals(headers.get("x-Content-Encoding"), Collections.singleton("gzip"));
payload.release();
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPostContentLanguage() throws ExecutionException, InterruptedException, TimeoutException,
IOException {
Payload payload = Payloads.newStringPayload("foo");
payload.setContentLanguage("mi, en");
Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
assertEquals(headers.get("x-Content-Language"), Collections.singleton("mi, en"));
payload.release();
}
@Test(invocationCount = 5, timeOut = 5000) @Test(invocationCount = 5, timeOut = 5000)
public void testPut() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException { public void testPut() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {

View File

@ -72,10 +72,13 @@ public abstract class BaseRestClientTest {
bind(TransformingHttpCommandExecutorService.class).toInstance(mock); bind(TransformingHttpCommandExecutorService.class).toInstance(mock);
} }
} }
protected void assertPayloadEquals(HttpRequest request, String toMatch, String contentType, boolean contentMD5) { protected void assertPayloadEquals(HttpRequest request, String toMatch, String contentType, boolean contentMD5) {
assertPayloadEquals(request, toMatch, contentType, null, contentMD5); assertPayloadEquals(request, toMatch, contentType, null, null, null, contentMD5);
} }
protected void assertPayloadEquals(HttpRequest request, String toMatch, String contentType, String contentDispositon, boolean contentMD5) {
protected void assertPayloadEquals(HttpRequest request, String toMatch, String contentType,
String contentDispositon, String contentEncoding, String contentLanguage, boolean contentMD5) {
if (request.getPayload() == null) { if (request.getPayload() == null) {
assertNull(toMatch); assertNull(toMatch);
} else { } else {
@ -88,28 +91,31 @@ public abstract class BaseRestClientTest {
assertEquals(payload, toMatch); assertEquals(payload, toMatch);
Long length = new Long(payload.getBytes().length); Long length = new Long(payload.getBytes().length);
try { try {
assertContentHeadersEqual(request, contentType, contentDispositon, length, contentMD5 ? CryptoStreams assertContentHeadersEqual(request, contentType, contentDispositon, contentEncoding, contentLanguage,
.md5(request.getPayload()) : null); length, contentMD5 ? CryptoStreams.md5(request.getPayload()) : null);
} catch (IOException e) { } catch (IOException e) {
propagate(e); propagate(e);
} }
} }
} }
protected void assertContentHeadersEqual(HttpRequest request, String contentType, String contentDispositon, Long length, byte[] contentMD5) { protected void assertContentHeadersEqual(HttpRequest request, String contentType, String contentDispositon,
String contentEncoding, String contentLanguage, Long length, byte[] contentMD5) {
if (request.getFirstHeaderOrNull(TRANSFER_ENCODING) == null) { if (request.getFirstHeaderOrNull(TRANSFER_ENCODING) == null) {
assertEquals(request.getPayload().getContentLength(), length); assertEquals(request.getPayload().getContentLength(), length);
} else { } else {
assertEquals(request.getFirstHeaderOrNull(TRANSFER_ENCODING), "chunked"); assertEquals(request.getFirstHeaderOrNull(TRANSFER_ENCODING), "chunked");
assert request.getPayload().getContentLength() == null assert request.getPayload().getContentLength() == null
|| request.getPayload().getContentLength().equals(length); || request.getPayload().getContentLength().equals(length);
} }
assertEquals(request.getPayload().getContentType(), contentType); assertEquals(request.getPayload().getContentType(), contentType);
assertEquals(request.getPayload().getContentDisposition(), contentDispositon); assertEquals(request.getPayload().getContentDisposition(), contentDispositon);
assertEquals(request.getPayload().getContentEncoding(), contentEncoding);
assertEquals(request.getPayload().getContentLanguage(), contentLanguage);
assertEquals(request.getPayload().getContentMD5(), contentMD5); assertEquals(request.getPayload().getContentMD5(), contentMD5);
} }
//FIXME Shouldn't be assertPayloadHeadersEqual? // FIXME Shouldn't be assertPayloadHeadersEqual?
protected void assertNonPayloadHeadersEqual(HttpRequest request, String toMatch) { protected void assertNonPayloadHeadersEqual(HttpRequest request, String toMatch) {
assertEquals(sortAndConcatHeadersIntoString(request.getHeaders()), toMatch); assertEquals(sortAndConcatHeadersIntoString(request.getHeaders()), toMatch);
} }
@ -121,10 +127,10 @@ public abstract class BaseRestClientTest {
protected void assertExceptionParserClassEquals(Method method, @Nullable Class<?> parserClass) { protected void assertExceptionParserClassEquals(Method method, @Nullable Class<?> parserClass) {
if (parserClass == null) if (parserClass == null)
assertEquals(RestAnnotationProcessor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(injector, assertEquals(RestAnnotationProcessor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(injector,
method).getClass(), MapHttp4xxCodesToExceptions.class); method).getClass(), MapHttp4xxCodesToExceptions.class);
else else
assertEquals(RestAnnotationProcessor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(injector, assertEquals(RestAnnotationProcessor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(injector,
method).getClass(), parserClass); method).getClass(), parserClass);
} }
protected void assertSaxResponseParserClassEquals(Method method, @Nullable Class<?> parserClass) { protected void assertSaxResponseParserClassEquals(Method method, @Nullable Class<?> parserClass) {
@ -133,7 +139,7 @@ public abstract class BaseRestClientTest {
protected void assertResponseParserClassEquals(Method method, HttpRequest request, @Nullable Class<?> parserClass) { protected void assertResponseParserClassEquals(Method method, HttpRequest request, @Nullable Class<?> parserClass) {
assertEquals(RestAnnotationProcessor.createResponseParser(parserFactory, injector, method, request).getClass(), assertEquals(RestAnnotationProcessor.createResponseParser(parserFactory, injector, method, request).getClass(),
parserClass); parserClass);
} }
} }

View File

@ -1501,9 +1501,29 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
HttpRequest request = factory(TestQuery.class).createRequest(method, payload); HttpRequest request = factory(TestQuery.class).createRequest(method, payload);
assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
assertNonPayloadHeadersEqual(request, ""); assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, "whoops", "application/unknown", "attachment; filename=photo.jpg", false); assertPayloadEquals(request, "whoops", "application/unknown", "attachment; filename=photo.jpg", null, null, false);
} }
public void testPutPayloadContentEncoding() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestTransformers.class.getMethod("put", Payload.class);
Payload payload = newStringPayload("whoops");
payload.setContentEncoding("gzip");
HttpRequest request = factory(TestQuery.class).createRequest(method, payload);
assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, "whoops", "application/unknown", null, "gzip", null, false);
}
public void testPutPayloadContentLanguage() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestTransformers.class.getMethod("put", Payload.class);
Payload payload = newStringPayload("whoops");
payload.setContentLanguage("en");
HttpRequest request = factory(TestQuery.class).createRequest(method, payload);
assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, "whoops", "application/unknown", null, null, "en", false);
}
public void testPutPayloadWithGeneratedMD5AndNoContentType() throws SecurityException, NoSuchMethodException, public void testPutPayloadWithGeneratedMD5AndNoContentType() throws SecurityException, NoSuchMethodException,
IOException { IOException {
Payload payload = newStringPayload("whoops"); Payload payload = newStringPayload("whoops");