From 9919a9823106d2572ffd200cacd4610554602a01 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 25 Jun 2014 23:12:35 +0200 Subject: [PATCH] HttpHeaders fails getAllow if set to EmptyCollection Prior to this commit, calls to getAllow would fail is setAllow was set to an EmptyCollection right before. java.lang.IllegalArgumentException: No enum constant org.springframework.http.HttpMethod This commit fixes this by testing the header value for an empty value before trying to use it to get a value from the Enum. Issue: SPR-11917 --- .../org/springframework/http/HttpHeaders.java | 2 +- .../springframework/http/HttpHeadersTests.java | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 3f3391dc59..6810b90bad 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -224,7 +224,7 @@ public class HttpHeaders implements MultiValueMap, Serializable */ public Set getAllow() { String value = getFirst(ALLOW); - if (value != null) { + if (!StringUtils.isEmpty(value)) { List allowedMethod = new ArrayList(5); String[] tokens = value.split(",\\s*"); for (String token : tokens) { diff --git a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java index caa6c1804c..086cbfba6e 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2014 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. @@ -22,18 +22,23 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.Collections; import java.util.EnumSet; import java.util.GregorianCalendar; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.TimeZone; +import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; -/** @author Arjen Poutsma */ +/** + * @author Arjen Poutsma + */ public class HttpHeadersTests { private HttpHeaders headers; @@ -256,5 +261,13 @@ public class HttpHeadersTests { headers.getFirst("Content-Disposition")); } + // SPR-11917 + + @Test + public void getAllowEmptySet() { + headers.setAllow(Collections. emptySet()); + + assertThat(headers.getAllow(), Matchers.emptyCollectionOf(HttpMethod.class)); + } }