Skip to content

Commit

Permalink
Include attributes in request built by DefaultServerRequestBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed May 19, 2020
1 parent b5b718f commit d0b2299
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -63,6 +63,7 @@
* Default {@link ServerRequest.Builder} implementation.
*
* @author Arjen Poutsma
* @author Sam Brannen
* @since 5.1
*/
class DefaultServerRequestBuilder implements ServerRequest.Builder {
Expand All @@ -84,7 +85,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
private Flux<DataBuffer> body = Flux.empty();


public DefaultServerRequestBuilder(ServerRequest other) {
DefaultServerRequestBuilder(ServerRequest other) {
Assert.notNull(other, "ServerRequest must not be null");
this.messageReaders = other.messageReaders();
this.exchange = other.exchange();
Expand Down Expand Up @@ -180,7 +181,7 @@ public ServerRequest build() {
ServerHttpRequest serverHttpRequest = new BuiltServerHttpRequest(this.exchange.getRequest().getId(),
this.methodName, this.uri, this.headers, this.cookies, this.body);
ServerWebExchange exchange = new DelegatingServerWebExchange(
serverHttpRequest, this.exchange, this.messageReaders);
serverHttpRequest, this.attributes, this.exchange, this.messageReaders);
return new DefaultServerRequest(exchange, this.messageReaders);
}

Expand Down Expand Up @@ -301,16 +302,19 @@ private static class DelegatingServerWebExchange implements ServerWebExchange {

private final ServerHttpRequest request;

private final Map<String, Object> attributes;

private final ServerWebExchange delegate;

private final Mono<MultiValueMap<String, String>> formDataMono;

private final Mono<MultiValueMap<String, Part>> multipartDataMono;

public DelegatingServerWebExchange(
ServerHttpRequest request, ServerWebExchange delegate, List<HttpMessageReader<?>> messageReaders) {
DelegatingServerWebExchange(ServerHttpRequest request, Map<String, Object> attributes,
ServerWebExchange delegate, List<HttpMessageReader<?>> messageReaders) {

this.request = request;
this.attributes = attributes;
this.delegate = delegate;
this.formDataMono = initFormData(request, messageReaders);
this.multipartDataMono = initMultipartData(request, messageReaders);
Expand Down Expand Up @@ -359,11 +363,17 @@ private static Mono<MultiValueMap<String, Part>> initMultipartData(ServerHttpReq
}
return EMPTY_MULTIPART_DATA;
}

@Override
public ServerHttpRequest getRequest() {
return this.request;
}

@Override
public Map<String, Object> getAttributes() {
return this.attributes;
}

@Override
public Mono<MultiValueMap<String, String>> getFormData() {
return this.formDataMono;
Expand All @@ -381,11 +391,6 @@ public ServerHttpResponse getResponse() {
return this.delegate.getResponse();
}

@Override
public Map<String, Object> getAttributes() {
return this.delegate.getAttributes();
}

@Override
public Mono<WebSession> getSession() {
return this.delegate.getSession();
Expand Down Expand Up @@ -442,4 +447,5 @@ public String getLogPrefix() {
return this.delegate.getLogPrefix();
}
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -31,9 +31,13 @@
import org.springframework.web.testfixture.server.MockServerWebExchange;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

/**
* Unit tests for {@link DefaultServerRequestBuilder}.
*
* @author Arjen Poutsma
* @author Sam Brannen
*/
public class DefaultServerRequestBuilderTests {

Expand All @@ -58,14 +62,18 @@ public void from() {
.method(HttpMethod.HEAD)
.headers(httpHeaders -> httpHeaders.set("foo", "baar"))
.cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build()))
.attribute("attr1", "value1")
.attributes(attributes -> attributes.put("attr2", "value2"))
.body(body)
.build();

assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
assertThat(result.headers().asHttpHeaders().size()).isEqualTo(1);
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
assertThat(result.cookies().size()).isEqualTo(1);
assertThat(result.cookies()).hasSize(1);
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
assertThat(result.attributes()).hasSize(3); // 3rd one is the LOG_ID
assertThat(result.attributes()).contains(entry("attr1", "value1"), entry("attr2", "value2"));

StepVerifier.create(result.bodyToFlux(String.class))
.expectNext("baz")
Expand Down

0 comments on commit d0b2299

Please sign in to comment.