提交 203b22b2 编写于 作者: P Phillip Webb

UriComponentsBuilder supports query without value

Fix UriComponentsBuilder to support query parameters that do not
include a value without losing '='. The following styles are now
supported:

    http://example.com/foo?bar=baz
    http://example.com/foo?bar=
    http://example.com/foo?bar

Issue: SPR-10215
上级 f32ce3a6
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 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.
...@@ -53,7 +53,7 @@ import org.springframework.util.StringUtils; ...@@ -53,7 +53,7 @@ import org.springframework.util.StringUtils;
*/ */
public class UriComponentsBuilder { public class UriComponentsBuilder {
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)=?([^&]+)?"); private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
private static final String SCHEME_PATTERN = "([^:/?#]+):"; private static final String SCHEME_PATTERN = "([^:/?#]+):";
...@@ -496,8 +496,10 @@ public class UriComponentsBuilder { ...@@ -496,8 +496,10 @@ public class UriComponentsBuilder {
Matcher m = QUERY_PARAM_PATTERN.matcher(query); Matcher m = QUERY_PARAM_PATTERN.matcher(query);
while (m.find()) { while (m.find()) {
String name = m.group(1); String name = m.group(1);
String value = m.group(2); String eq = m.group(2);
queryParam(name, value); String value = m.group(3);
queryParam(name, (value != null ? value :
(StringUtils.hasLength(eq) ? "" : null)));
} }
} }
else { else {
......
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 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.
...@@ -27,10 +27,12 @@ import org.junit.Test; ...@@ -27,10 +27,12 @@ import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Phillip Webb
*/ */
public class UriComponentsBuilderTests { public class UriComponentsBuilderTests {
...@@ -312,4 +314,22 @@ public class UriComponentsBuilderTests { ...@@ -312,4 +314,22 @@ public class UriComponentsBuilderTests {
assertEquals("mailto:foo@example.com", result.toUriString()); assertEquals("mailto:foo@example.com", result.toUriString());
} }
@Test
public void queryParamWithValueWithEquals() throws Exception {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar=baz").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar=baz"));
}
@Test
public void queryParamWithoutValueWithEquals() throws Exception {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar=").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar="));
}
@Test
public void queryParamWithoutValueWithoutEquals() throws Exception {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar"));
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册