parent
75d006d2f9
commit
fd6fc30eea
|
@ -38,6 +38,7 @@ import org.springframework.http.HttpMethod;
|
|||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||
import org.springframework.http.client.reactive.ClientHttpRequestFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
|
||||
/**
|
||||
|
@ -137,6 +138,21 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows performing more complex operations with a strategy. For example, a
|
||||
* {@link RequestPostProcessor} implementation might accept the arguments of
|
||||
* username and password and set an HTTP Basic authentication header.
|
||||
*
|
||||
* @param postProcessor the {@link RequestPostProcessor} to use. Cannot be null.
|
||||
*
|
||||
* @return this instance for further modifications.
|
||||
*/
|
||||
public DefaultHttpRequestBuilder apply(RequestPostProcessor postProcessor) {
|
||||
Assert.notNull(postProcessor, "`postProcessor` is required");
|
||||
postProcessor.postProcess(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ClientHttpRequest build(ClientHttpRequestFactory factory) {
|
||||
ClientHttpRequest request = factory.createRequest(this.httpMethod, this.url, this.httpHeaders);
|
||||
request.getHeaders().putAll(this.httpHeaders);
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2002-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.client.reactive;
|
||||
|
||||
/**
|
||||
* Allows post processing the {@link DefaultHttpRequestBuilder} for strategy for
|
||||
* performing more complex operations.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @see DefaultHttpRequestBuilder#apply(RequestPostProcessor)
|
||||
*/
|
||||
public interface RequestPostProcessor {
|
||||
|
||||
/**
|
||||
* Implementations can modify the {@link DefaultHttpRequestBuilder} passed
|
||||
* in.
|
||||
*
|
||||
* @param toPostProcess
|
||||
* the {@link DefaultHttpRequestBuilder} to be modified.
|
||||
*/
|
||||
void postProcess(DefaultHttpRequestBuilder toPostProcess);
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2002-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.client.reactive;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rob Winch
|
||||
*
|
||||
*/
|
||||
public class DefaultHttpRequestBuilderTests {
|
||||
private DefaultHttpRequestBuilder builder;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
builder = new DefaultHttpRequestBuilder(HttpMethod.GET, "https://example.com/foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply() {
|
||||
RequestPostProcessor postProcessor = mock(RequestPostProcessor.class);
|
||||
|
||||
builder.apply(postProcessor);
|
||||
|
||||
verify(postProcessor).postProcess(builder);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void applyNullPostProcessorThrowsIllegalArgumentException() {
|
||||
builder.apply(null);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue