Skip to content

Commit 79ded1c

Browse files
committed
MockHttpServletResponse.setIntHeader supports 'Content-Length' header as well
Issue: SPR-13752 (cherry picked from commit a4f5c46)
1 parent fad0c95 commit 79ded1c

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -506,11 +506,12 @@ private void addHeaderValue(String name, Object value) {
506506

507507
private boolean setSpecialHeader(String name, Object value) {
508508
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
509-
setContentType((String) value);
509+
setContentType(value.toString());
510510
return true;
511511
}
512512
else if (CONTENT_LENGTH_HEADER.equalsIgnoreCase(name)) {
513-
setContentLength(Integer.parseInt((String) value));
513+
setContentLength(value instanceof Number ? ((Number) value).intValue() :
514+
Integer.parseInt(value.toString()));
514515
return true;
515516
}
516517
else {

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -122,6 +122,13 @@ public void contentLengthHeader() {
122122
assertEquals("66", response.getHeader("Content-Length"));
123123
}
124124

125+
@Test
126+
public void contentLengthIntHeader() {
127+
response.addIntHeader("Content-Length", 66);
128+
assertEquals(66, response.getContentLength());
129+
assertEquals("66", response.getHeader("Content-Length"));
130+
}
131+
125132
@Test
126133
public void httpHeaderNameCasingIsPreserved() throws Exception {
127134
final String headerName = "Header1";
@@ -221,20 +228,14 @@ public void locationHeaderUpdatesGetRedirectedUrl() {
221228
assertEquals(redirectUrl, response.getRedirectedUrl());
222229
}
223230

224-
/**
225-
* SPR-10414
226-
*/
227-
@Test
231+
@Test // SPR-10414
228232
public void modifyStatusAfterSendError() throws IOException {
229233
response.sendError(HttpServletResponse.SC_NOT_FOUND);
230234
response.setStatus(HttpServletResponse.SC_OK);
231235
assertEquals(response.getStatus(),HttpServletResponse.SC_NOT_FOUND);
232236
}
233237

234-
/**
235-
* SPR-10414
236-
*/
237-
@Test
238+
@Test // SPR-10414
238239
@SuppressWarnings("deprecation")
239240
public void modifyStatusMessageAfterSendError() throws IOException {
240241
response.sendError(HttpServletResponse.SC_NOT_FOUND);

spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
7171

7272
private boolean charset = false;
7373

74-
private final ByteArrayOutputStream content = new ByteArrayOutputStream();
74+
private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
7575

7676
private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content);
7777

@@ -189,8 +189,8 @@ public byte[] getContentAsByteArray() {
189189

190190
public String getContentAsString() throws UnsupportedEncodingException {
191191
flushBuffer();
192-
return (this.characterEncoding != null) ?
193-
this.content.toString(this.characterEncoding) : this.content.toString();
192+
return (this.characterEncoding != null ?
193+
this.content.toString(this.characterEncoding) : this.content.toString());
194194
}
195195

196196
@Override
@@ -218,8 +218,7 @@ public void setContentType(String contentType) {
218218
if (contentType != null) {
219219
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
220220
if (charsetIndex != -1) {
221-
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
222-
this.characterEncoding = encoding;
221+
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
223222
this.charset = true;
224223
}
225224
updateContentTypeHeader();
@@ -416,11 +415,13 @@ public String encodeRedirectURL(String url) {
416415
}
417416

418417
@Override
418+
@Deprecated
419419
public String encodeUrl(String url) {
420420
return encodeURL(url);
421421
}
422422

423423
@Override
424+
@Deprecated
424425
public String encodeRedirectUrl(String url) {
425426
return encodeRedirectURL(url);
426427
}
@@ -505,11 +506,12 @@ private void addHeaderValue(String name, Object value) {
505506

506507
private boolean setSpecialHeader(String name, Object value) {
507508
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
508-
setContentType((String) value);
509+
setContentType(value.toString());
509510
return true;
510511
}
511512
else if (CONTENT_LENGTH_HEADER.equalsIgnoreCase(name)) {
512-
setContentLength(Integer.parseInt((String) value));
513+
setContentLength(value instanceof Number ? ((Number) value).intValue() :
514+
Integer.parseInt(value.toString()));
513515
return true;
514516
}
515517
else {
@@ -534,14 +536,15 @@ private void doAddHeaderValue(String name, Object value, boolean replace) {
534536

535537
@Override
536538
public void setStatus(int status) {
537-
if(!this.isCommitted()) {
539+
if (!this.isCommitted()) {
538540
this.status = status;
539541
}
540542
}
541543

542544
@Override
545+
@Deprecated
543546
public void setStatus(int status, String errorMessage) {
544-
if(!this.isCommitted()) {
547+
if (!this.isCommitted()) {
545548
this.status = status;
546549
this.errorMessage = errorMessage;
547550
}

0 commit comments

Comments
 (0)