DefaultClientRequestBuilderTests.java 4.2 KB
Newer Older
1
/*
2
 * Copyright 2002-2017 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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.
 */

17
package org.springframework.web.reactive.function.client;
18 19 20 21 22 23 24 25

import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import reactor.core.publisher.Mono;
26
import reactor.test.StepVerifier;
27 28 29 30 31 32 33

import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.HttpMessageWriter;
34
import org.springframework.mock.http.client.reactive.test.MockClientHttpRequest;
35
import org.springframework.web.reactive.function.BodyInserter;
36

37
import static java.nio.charset.StandardCharsets.UTF_8;
J
Juergen Hoeller 已提交
38 39
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
40 41 42
import static org.springframework.http.HttpMethod.DELETE;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
43 44 45 46 47 48 49 50

/**
 * @author Arjen Poutsma
 */
public class DefaultClientRequestBuilderTests {

	@Test
	public void from() throws Exception {
51
		ClientRequest other = ClientRequest.method(GET, URI.create("http://example.com"))
52 53
				.header("foo", "bar")
				.cookie("baz", "qux").build();
54
		ClientRequest result = ClientRequest.from(other).build();
55
		assertEquals(new URI("http://example.com"), result.url());
56
		assertEquals(GET, result.method());
57 58 59 60 61 62 63
		assertEquals("bar", result.headers().getFirst("foo"));
		assertEquals("qux", result.cookies().getFirst("baz"));
	}

	@Test
	public void method() throws Exception {
		URI url = new URI("http://example.com");
64
		ClientRequest result = ClientRequest.method(DELETE, url).build();
65
		assertEquals(url, result.url());
66
		assertEquals(DELETE, result.method());
67 68 69 70
	}

	@Test
	public void cookie() throws Exception {
71
		ClientRequest result = ClientRequest.method(GET, URI.create("http://example.com"))
72 73 74 75 76 77
				.cookie("foo", "bar").build();
		assertEquals("bar", result.cookies().getFirst("foo"));
	}

	@Test
	public void build() throws Exception {
78
		ClientRequest result = ClientRequest.method(GET, URI.create("http://example.com"))
79 80 81 82
				.header("MyKey", "MyValue")
				.cookie("foo", "bar")
				.build();

83
		MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
84
		ExchangeStrategies strategies = mock(ExchangeStrategies.class);
85 86 87 88 89

		result.writeTo(request, strategies).block();

		assertEquals("MyValue", request.getHeaders().getFirst("MyKey"));
		assertEquals("bar", request.getCookies().getFirst("foo").getValue());
90
		StepVerifier.create(request.getBody()).expectComplete().verify();
91 92 93 94 95
	}

	@Test
	public void bodyInserter() throws Exception {
		String body = "foo";
96
		BodyInserter<String, ClientHttpRequest> inserter =
97 98 99 100 101 102 103 104
				(response, strategies) -> {
					byte[] bodyBytes = body.getBytes(UTF_8);
					ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
					DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);

					return response.writeWith(Mono.just(buffer));
				};

105 106
		ClientRequest result = ClientRequest.method(POST, URI.create("http://example.com"))
				.body(inserter).build();
107 108

		List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
109
		messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
110

111
		ExchangeStrategies strategies = mock(ExchangeStrategies.class);
112
		when(strategies.messageWriters()).thenReturn(messageWriters);
113

114
		MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
115 116 117 118 119
		result.writeTo(request, strategies).block();
		assertNotNull(request.getBody());
	}

}