Using the last value of duplicate headers as that is the behaviour displayed by webclient and other clients as well.

This commit is contained in:
Arpit Mohan 2020-05-14 08:49:06 +05:30
parent cdabc8816b
commit a9580effa7
2 changed files with 19 additions and 10 deletions

View File

@ -36,6 +36,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
@ -51,8 +52,10 @@ import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLDecoder;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@ -252,12 +255,11 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
}
if (actionConfiguration.getHeaders() != null) {
Map<String, String> reqHeaderMap = actionConfiguration.getHeaders().stream()
.collect(Collectors.toMap(Property::getKey,
Property::getValue,
// If there are duplicate values, ignore the second one
(header1, header2) -> header1));
actionExecutionResult.setRequestHeaders(objectMapper.valueToTree(reqHeaderMap));
MultiValueMap<String, String> reqMultiMap = CollectionUtils.toMultiValueMap(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH));
actionConfiguration.getHeaders().stream()
.forEach(header -> reqMultiMap.put(header.getKey(), Arrays.asList(header.getValue())));
actionExecutionResult.setRequestHeaders(objectMapper.valueToTree(reqMultiMap));
log.debug("Got request headers in actionExecutionResult as: {}", actionExecutionResult.getRequestHeaders());
}

View File

@ -22,9 +22,12 @@ import org.springframework.http.HttpMethod;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -246,7 +249,7 @@ public class ActionServiceTest {
actionConfiguration.setHeaders(List.of(
new Property("random-header-key", "random-header-value"),
new Property("dup-key", "dup-value1"),
new Property("dup-key", "dup-value2")
new Property("DUP-key", "dup-value2")
));
action.setActionConfiguration(actionConfiguration);
@ -256,9 +259,11 @@ public class ActionServiceTest {
Mono<ActionExecutionResult> executionResultMono = executeAction(executeActionDTO, actionConfiguration, mockResult);
StepVerifier.create(executionResultMono)
.assertNext(result -> {
// The fxn should have ignored all duplicate headers
// Assert that the fxn should pick up the latest key based on the case-insensitive values
assertThat(result.getRequestHeaders().size()).isEqualTo(2);
Map<String, String> resultHeader = Map.of("random-header-key", "random-header-value", "dup-key", "dup-value1");
MultiValueMap<String, String> resultHeader = CollectionUtils.toMultiValueMap(Map.of(
"random-header-key", Arrays.asList("random-header-value"),
"DUP-key", Arrays.asList("dup-value2")));
assertThat(result.getRequestHeaders()).isEqualTo(objectMapper.valueToTree(resultHeader));
})
.verifyComplete();
@ -288,7 +293,9 @@ public class ActionServiceTest {
.assertNext(result -> {
// The fxn should have ignored all duplicate headers
assertThat(result.getRequestHeaders().size()).isEqualTo(1);
Map<String, String> resultHeader = Map.of("random-header-key", "random-header-value");
MultiValueMap<String, String> resultHeader = CollectionUtils.toMultiValueMap(Map.of(
"random-header-key", Arrays.asList("random-header-value")));
assertThat(result.getRequestHeaders()).isEqualTo(objectMapper.valueToTree(resultHeader));
})
.verifyComplete();