BufferedImage converter writes Content-Type again
Issue: SPR-13906
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -21,10 +21,11 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MockHttpOutputMessage implements HttpOutputMessage {
|
||||
|
||||
@@ -34,19 +35,31 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
|
||||
|
||||
private boolean headersWritten = false;
|
||||
|
||||
private final HttpHeaders writtenHeaders = new HttpHeaders();
|
||||
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a copy of the actual headers written at the time of the call to
|
||||
* getBody, i.e. ignoring any further changes that may have been made to
|
||||
* the underlying headers, e.g. via a previously obtained instance.
|
||||
*/
|
||||
public HttpHeaders getWrittenHeaders() {
|
||||
return writtenHeaders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getBody() throws IOException {
|
||||
this.headersWritten = true;
|
||||
writeHeaders();
|
||||
return body;
|
||||
}
|
||||
|
||||
public byte[] getBodyAsBytes() {
|
||||
this.headersWritten = true;
|
||||
writeHeaders();
|
||||
return body.toByteArray();
|
||||
}
|
||||
|
||||
@@ -54,4 +67,13 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
|
||||
byte[] bytes = getBodyAsBytes();
|
||||
return new String(bytes, charset);
|
||||
}
|
||||
|
||||
private void writeHeaders() {
|
||||
if (this.headersWritten) {
|
||||
return;
|
||||
}
|
||||
this.headersWritten = true;
|
||||
this.writtenHeaders.putAll(this.headers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -33,6 +33,11 @@ import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for BufferedImageHttpMessageConverter.
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class BufferedImageHttpMessageConverterTests {
|
||||
|
||||
private BufferedImageHttpMessageConverter converter;
|
||||
@@ -73,7 +78,7 @@ public class BufferedImageHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MediaType contentType = new MediaType("image", "png");
|
||||
converter.write(body, contentType, outputMessage);
|
||||
assertEquals("Invalid content type", contentType, outputMessage.getHeaders().getContentType());
|
||||
assertEquals("Invalid content type", contentType, outputMessage.getWrittenHeaders().getContentType());
|
||||
assertTrue("Invalid size", outputMessage.getBodyAsBytes().length > 0);
|
||||
BufferedImage result = ImageIO.read(new ByteArrayInputStream(outputMessage.getBodyAsBytes()));
|
||||
assertEquals("Invalid height", 500, result.getHeight());
|
||||
@@ -88,7 +93,7 @@ public class BufferedImageHttpMessageConverterTests {
|
||||
BufferedImage body = ImageIO.read(logo.getFile());
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(body, new MediaType("*", "*"), outputMessage);
|
||||
assertEquals("Invalid content type", contentType, outputMessage.getHeaders().getContentType());
|
||||
assertEquals("Invalid content type", contentType, outputMessage.getWrittenHeaders().getContentType());
|
||||
assertTrue("Invalid size", outputMessage.getBodyAsBytes().length > 0);
|
||||
BufferedImage result = ImageIO.read(new ByteArrayInputStream(outputMessage.getBodyAsBytes()));
|
||||
assertEquals("Invalid height", 500, result.getHeight());
|
||||
|
||||
Reference in New Issue
Block a user