Skip to content

Commit e128ddb

Browse files
committed
Add QUERY HTTP method
Signed-off-by: Mario Daniel Ruiz Saavedra <desiderantes93@gmail.com>
1 parent 12d71c9 commit e128ddb

76 files changed

Lines changed: 813 additions & 82 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ public static BaseBuilder<?> options(String urlTemplate, @Nullable Object... uri
184184
return method(HttpMethod.OPTIONS, urlTemplate, uriVars);
185185
}
186186

187+
/**
188+
* HTTP QUERY variant. See {@link #get(String, Object...)} for general info.
189+
* @param urlTemplate a URL template; the resulting URL will be encoded
190+
* @param uriVars zero or more URI variables
191+
* @return the created builder
192+
*/
193+
public static BodyBuilder query(String urlTemplate, @Nullable Object... uriVars) {
194+
return method(HttpMethod.QUERY, urlTemplate, uriVars);
195+
}
196+
187197
/**
188198
* Create a builder with the given HTTP method and a {@link URI}.
189199
* @param method the HTTP method (GET, POST, etc)

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public RequestHeadersUriSpec<?> options() {
167167
return methodInternal(HttpMethod.OPTIONS);
168168
}
169169

170+
@Override
171+
public RequestBodyUriSpec query() {
172+
return methodInternal(HttpMethod.QUERY);
173+
}
174+
170175
@Override
171176
public RequestBodyUriSpec method(HttpMethod httpMethod) {
172177
return methodInternal(httpMethod);

spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ public interface WebTestClient {
146146
*/
147147
RequestHeadersUriSpec<?> options();
148148

149+
/**
150+
* Prepare an HTTP QUERY request.
151+
* @return a spec for specifying the target URL
152+
* @since 7.1
153+
*/
154+
RequestBodyUriSpec query();
155+
149156
/**
150157
* Prepare a request for the specified {@code HttpMethod}.
151158
* @return a spec for specifying the target URL

spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MockMvcTester.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,21 @@ public MockMvcRequestBuilder options() {
333333
return method(HttpMethod.OPTIONS);
334334
}
335335

336+
/**
337+
* Prepare an HTTP QUERY request.
338+
* <p>The returned builder can be wrapped in {@code assertThat} to enable
339+
* assertions on the result. For multi-statements assertions, use
340+
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
341+
* result. To control the time to wait for asynchronous request to complete
342+
* on a per-request basis, use
343+
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
344+
* @return a request builder for specifying the target URI
345+
* @since 7.1
346+
*/
347+
public MockMvcRequestBuilder query() {
348+
return method(HttpMethod.QUERY);
349+
}
350+
336351
/**
337352
* Prepare a request for the specified {@code HttpMethod}.
338353
* <p>The returned builder can be wrapped in {@code assertThat} to enable

spring-test/src/main/java/org/springframework/test/web/servlet/client/DefaultRestTestClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public RequestHeadersUriSpec<?> delete() {
119119
return methodInternal(HttpMethod.DELETE);
120120
}
121121

122+
@Override
123+
public RequestBodyUriSpec query() {
124+
return methodInternal(HttpMethod.QUERY);
125+
}
126+
122127
@Override
123128
public RequestHeadersUriSpec<?> options() {
124129
return methodInternal(HttpMethod.OPTIONS);

spring-test/src/main/java/org/springframework/test/web/servlet/client/RestTestClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ public interface RestTestClient {
119119
*/
120120
RequestHeadersUriSpec<?> delete();
121121

122+
/**
123+
* Prepare an HTTP QUERY request.
124+
* @return a spec for specifying the target URL
125+
* @since 7.1
126+
*/
127+
RequestBodyUriSpec query();
128+
122129
/**
123130
* Prepare an HTTP OPTIONS request.
124131
* @return a spec for specifying the target URL

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ public static MockHttpServletRequestBuilder head(URI uri) {
175175
return new MockHttpServletRequestBuilder(HttpMethod.HEAD).uri(uri);
176176
}
177177

178+
/**
179+
* Create a {@link MockHttpServletRequestBuilder} for a QUERY request.
180+
* @param uriTemplate a URI template; the resulting URI will be encoded
181+
* @param uriVariables zero or more URI variables
182+
* @since 7.1
183+
*/
184+
public static MockHttpServletRequestBuilder query(String uriTemplate, @Nullable Object... uriVariables) {
185+
return new MockHttpServletRequestBuilder(HttpMethod.QUERY).uri(uriTemplate, uriVariables);
186+
}
187+
188+
/**
189+
* Create a {@link MockHttpServletRequestBuilder} for a QUERY request.
190+
* @param uri the URI
191+
* @since 7.1
192+
*/
193+
public static MockHttpServletRequestBuilder query(URI uri) {
194+
return new MockHttpServletRequestBuilder(HttpMethod.QUERY).uri(uri);
195+
}
196+
178197
/**
179198
* Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
180199
* @param method the HTTP method (GET, POST, etc.)

spring-test/src/test/java/org/springframework/test/web/servlet/client/RestTestClientTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class RestTestClientTests {
5757
class HttpMethods {
5858

5959
@ParameterizedTest
60-
@ValueSource(strings = {"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"})
60+
@ValueSource(strings = {"GET", "POST", "PUT", "DELETE", "PATCH", "QUERY", "HEAD"})
6161
void method(String method) {
6262
RestTestClientTests.this.client.method(HttpMethod.valueOf(method)).uri("/test")
6363
.exchange()
@@ -118,10 +118,18 @@ void options() {
118118
RestTestClientTests.this.client.options().uri("/test")
119119
.exchange()
120120
.expectStatus().isOk()
121-
.expectHeader().valueEquals("Allow", "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS")
121+
.expectHeader().valueEquals("Allow", "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,QUERY")
122122
.expectBody().isEmpty();
123123
}
124124

125+
@Test
126+
void testQuery() {
127+
RestTestClientTests.this.client.query().uri("/test")
128+
.exchange()
129+
.expectStatus().isOk()
130+
.expectBody().jsonPath("$.method").isEqualTo("QUERY");
131+
}
132+
125133
}
126134

127135

spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import jakarta.servlet.http.Cookie;
3232
import org.assertj.core.api.ThrowingConsumer;
3333
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.params.ParameterizedTest;
35+
import org.junit.jupiter.params.provider.ValueSource;
3436

3537
import org.springframework.http.HttpHeaders;
3638
import org.springframework.http.HttpMethod;
@@ -418,18 +420,21 @@ void requestParameterFromMultiValueMap() {
418420
assertThat(request.getParameterMap().get("foo")).containsExactly("bar", "baz");
419421
}
420422

421-
@Test
422-
void requestParameterFromRequestBodyFormData() {
423+
@ValueSource(strings = {"POST", "QUERY"})
424+
@ParameterizedTest()
425+
void requestParameterFromRequestBodyFormData(String methodName) {
423426
String contentType = "application/x-www-form-urlencoded;charset=UTF-8";
424427
String body = "name+1=value+1&name+2=value+A&name+2=value+B&name+3";
425428

426-
MockHttpServletRequest request = new MockHttpServletRequestBuilder(POST).uri("/foo")
429+
HttpMethod method = HttpMethod.valueOf(methodName);
430+
MockHttpServletRequest request = new MockHttpServletRequestBuilder(method).uri("/foo")
427431
.contentType(contentType).content(body.getBytes(UTF_8))
428432
.buildRequest(this.servletContext);
429433

430434
assertThat(request.getParameterMap().get("name 1")).containsExactly("value 1");
431435
assertThat(request.getParameterMap().get("name 2")).containsExactly("value A", "value B");
432436
assertThat(request.getParameterMap().get("name 3")).containsExactly((String) null);
437+
433438
}
434439

435440
@Test

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ public class HttpHeaders implements Serializable {
129129
* @see <a href="https://tools.ietf.org/html/rfc7233#section-2.3">Section 5.3.5 of RFC 7233</a>
130130
*/
131131
public static final String ACCEPT_RANGES = "Accept-Ranges";
132+
133+
/**
134+
* The HTTP {@code Accept-Query} header field name.
135+
* @since 7.1
136+
* @see <a href="https://www.rfc-editor.org/rfc/rfc10008.html#section-3">Section 3 of RFC 10008</a>
137+
*/
138+
public static final String ACCEPT_QUERY = "Accept-Query";
132139
/**
133140
* The CORS {@code Access-Control-Allow-Credentials} response header field name.
134141
* @see <a href="https://www.w3.org/TR/cors/">CORS W3C recommendation</a>
@@ -648,6 +655,27 @@ public List<MediaType> getAcceptPatch() {
648655
return MediaType.parseMediaTypes(get(ACCEPT_PATCH));
649656
}
650657

658+
/**
659+
* Set the list of acceptable {@linkplain MediaType media types} for
660+
* {@code QUERY} methods, as specified by the {@code Accept-Query} header.
661+
* @since 7.1
662+
*/
663+
public void setAcceptQuery(List<MediaType> mediaTypes) {
664+
set(ACCEPT_QUERY, MediaType.toString(mediaTypes));
665+
}
666+
667+
/**
668+
* Return the list of acceptable {@linkplain MediaType media types} for
669+
* {@code QUERY} methods, as specified by the {@code Accept-Query} header.
670+
* <p>Returns an empty list when the acceptable media types are unspecified.
671+
* @since 7.1
672+
*/
673+
public List<MediaType> getAcceptQuery() {
674+
return MediaType.parseMediaTypes(get(ACCEPT_QUERY));
675+
}
676+
677+
678+
651679
/**
652680
* Set the (new) value of the {@code Access-Control-Allow-Credentials} response header.
653681
*/

0 commit comments

Comments
 (0)