MessageHeaderAccessor properly removes header even in case of null value
Issue: SPR-14468
(cherry picked from commit 4ea5f07
)
This commit is contained in:
parent
44152ce401
commit
4be5541c0e
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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");
|
throw new IllegalArgumentException("'" + name + "' header is read-only");
|
||||||
}
|
}
|
||||||
verifyType(name, value);
|
verifyType(name, value);
|
||||||
|
if (value != null) {
|
||||||
|
// Modify header if necessary
|
||||||
if (!ObjectUtils.nullSafeEquals(value, getHeader(name))) {
|
if (!ObjectUtils.nullSafeEquals(value, getHeader(name))) {
|
||||||
this.modified = true;
|
this.modified = true;
|
||||||
if (value != null) {
|
|
||||||
this.headers.getRawHeaders().put(name, value);
|
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);
|
this.headers.getRawHeaders().remove(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.nio.charset.Charset;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -40,6 +41,7 @@ import static org.junit.Assert.*;
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
|
* @author Juergen Hoeller
|
||||||
*/
|
*/
|
||||||
public class MessageHeaderAccessorTests {
|
public class MessageHeaderAccessorTests {
|
||||||
|
|
||||||
|
@ -89,6 +91,24 @@ public class MessageHeaderAccessorTests {
|
||||||
assertEquals("baz", actual.get("bar"));
|
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
|
@Test
|
||||||
public void removeHeaders() {
|
public void removeHeaders() {
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
@ -153,7 +173,6 @@ public class MessageHeaderAccessorTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toMap() {
|
public void toMap() {
|
||||||
|
|
||||||
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
|
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
|
||||||
|
|
||||||
accessor.setHeader("foo", "bar1");
|
accessor.setHeader("foo", "bar1");
|
||||||
|
@ -380,7 +399,6 @@ public class MessageHeaderAccessorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static class TestMessageHeaderAccessor extends MessageHeaderAccessor {
|
public static class TestMessageHeaderAccessor extends MessageHeaderAccessor {
|
||||||
|
|
||||||
private TestMessageHeaderAccessor() {
|
private TestMessageHeaderAccessor() {
|
||||||
|
|
Loading…
Reference in New Issue