MessageHeaderAccessor properly removes header even in case of null value

Issue: SPR-14468
(cherry picked from commit 4ea5f07)
This commit is contained in:
Juergen Hoeller 2016-07-15 00:15:46 +02:00
parent 44152ce401
commit 4be5541c0e
2 changed files with 31 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 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.
@ -306,12 +306,17 @@ public class MessageHeaderAccessor {
throw new IllegalArgumentException("'" + name + "' header is read-only");
}
verifyType(name, value);
if (!ObjectUtils.nullSafeEquals(value, getHeader(name))) {
this.modified = true;
if (value != null) {
if (value != null) {
// Modify header if necessary
if (!ObjectUtils.nullSafeEquals(value, getHeader(name))) {
this.modified = true;
this.headers.getRawHeaders().put(name, value);
}
else {
}
else {
// Remove header if available
if (this.headers.containsKey(name)) {
this.modified = true;
this.headers.getRawHeaders().remove(name);
}
}

View File

@ -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.
@ -18,6 +18,7 @@ package org.springframework.messaging.support;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -40,6 +41,7 @@ import static org.junit.Assert.*;
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @author Juergen Hoeller
*/
public class MessageHeaderAccessorTests {
@ -89,6 +91,24 @@ public class MessageHeaderAccessorTests {
assertEquals("baz", actual.get("bar"));
}
@Test
public void testRemoveHeader() {
Message<?> message = new GenericMessage<>("payload", Collections.singletonMap("foo", "bar"));
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
accessor.removeHeader("foo");
Map<String, Object> headers = accessor.toMap();
assertFalse(headers.containsKey("foo"));
}
@Test
public void testRemoveHeaderEvenIfNull() {
Message<?> message = new GenericMessage<>("payload", Collections.singletonMap("foo", null));
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
accessor.removeHeader("foo");
Map<String, Object> headers = accessor.toMap();
assertFalse(headers.containsKey("foo"));
}
@Test
public void removeHeaders() {
Map<String, Object> map = new HashMap<>();
@ -153,7 +173,6 @@ public class MessageHeaderAccessorTests {
@Test
public void toMap() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setHeader("foo", "bar1");
@ -380,7 +399,6 @@ public class MessageHeaderAccessorTests {
}
public static class TestMessageHeaderAccessor extends MessageHeaderAccessor {
private TestMessageHeaderAccessor() {