java 为 Spring TestRestTemplate 集成测试添加标头值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34641502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Header Value For Spring TestRestTemplate Integration Test
提问by DavidR
I am using TestRestTemplate
for integration testing on our product.
我正在使用TestRestTemplate
我们的产品进行集成测试。
I have one test that looks like this:
我有一个看起来像这样的测试:
@Test
public void testDeviceQuery() {
ResponseEntity<Page> deviceInfoPage = template.getForEntity(base, Page.class);
// validation code here
}
This particular request expects a Header value. Can someone please let me know how I can add a header to the TestRestTemplate
call?
此特定请求需要一个 Header 值。有人可以让我知道如何在TestRestTemplate
通话中添加标题吗?
采纳答案by DagR
If you want all of your requests using TestRestTemplate
to include certain headers, you could add the following to your setup:
如果您希望所有请求TestRestTemplate
都包含某些标头,则可以将以下内容添加到您的设置中:
testRestTemplate.getRestTemplate().setInterceptors(
Collections.singletonList((request, body, execution) -> {
request.getHeaders()
.add("header-name", "value");
return execution.execute(request, body);
}));
回答by Ali Dehghani
Update: As of Spring Boot 1.4.0, TestRestTemplate
does not extend RestTemplate
anymore but still provides the same API as RestTemplate
.
更新:从 Spring Boot 1.4.0 开始,TestRestTemplate
不再扩展RestTemplate
,但仍提供与RestTemplate
.
TestRestTemplate
extends provides the same API as the RestTemplate
RestTemplate
, so you can use that same API for sending requests. For example:
TestRestTemplate
延伸 提供与 相同的 API RestTemplate
RestTemplate
,因此您可以使用相同的 API 发送请求。例如:
HttpHeaders headers = new HttpHeaders();
headers.add("your_header", "its_value");
template.exchange(base, HttpMethod.GET, new HttpEntity<>(headers), Page.class);
回答by Vasu K S
If you want to use multiple headers for all your requests, you can add the below
如果要为所有请求使用多个标头,可以添加以下内容
import org.apache.http.Header;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
private void setTestRestTemplateHeaders() {
Header header = new BasicHeader("header", "value");
Header header2 = new BasicHeader("header2", "value2");
List<Header> headers = new ArrayList<Header>();
headers.add(header);
headers.add(header2);
CloseableHttpClient httpClient = HttpClients.custom().setDefaultHeaders(headers).build();
testRestTemplate.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
Once the headers are set you can either use TestRestTemplate [testRestTemplate]
or RestTemplate [testRestTemplate.getRestTemplate()]
for your REST calls
设置标头后,您可以使用TestRestTemplate [testRestTemplate]
或RestTemplate [testRestTemplate.getRestTemplate()]
用于您的 REST 调用