我在我的應用程式中使用Spring Boot 2.6.0和Spring MVC。我想將我的控制器協議從版本切換http/1.1到http/2. 首先,我為此撰寫了下一個集成測驗:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest {
@Autowired
private TestRestTemplate template;
@Test
public void canGet() {
ResponseEntity<JsonResponse> entity = template.getForEntity("/foo", JsonResponse.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertNotNull(entity.getBody());
//todo: assertEquals("http/2", entity.getProtocol());
//todo: assertEquals("http/1.1", entity.getProtocol())
}
}
但是entity.getProtocol()方法不存在,我找不到任何其他方法來測驗協議版本。有誰知道如何在 spring boot 應用程式中正確測驗協議版本?
uj5u.com熱心網友回復:
有一個簡單的 Spring-Starter 應用程式,帶有一個(休息)控制器,如:
@GetMapping("/foo")
public String foo() {
return "bar";
}
java >= 11,我們可以用java.net.http客戶端測驗HTTP協議版本(無附加依賴):
package com.example.test.http.version;
import java.io.IOException;
import java.net.URI;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class IntegrationTests {
@LocalServerPort
private int port;
@Test
public void testJavaDotNet() throws IOException, InterruptedException {
java.net.http.HttpClient client = java.net.http.HttpClient.newBuilder()
.build(); // or configure a (test) bean
java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" port "/foo"))
.build();
java.net.http.HttpResponse<String> response = client.send(request, java.net.http.HttpResponse.BodyHandlers.ofString());
assertNotNull(response);
assertEquals(java.net.http.HttpClient.Version.HTTP_1_1, response.version());
}
}
關鍵點:
HttpResponse.version()- java.net.http.HttpClient
使用 java < 11但也可以使用:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<!-- <version>4.5.13</version> managed via spring-boot-dependencies -->
<scope>test</scope>
</dependency>
..和:
@Test
public void testApache() throws IOException {
org.apache.http.client.HttpClient client = org.apache.http.impl.client.HttpClientBuilder.create()
.build();// or configure (test) bean(s)
org.apache.http.HttpResponse response = client.execute(
new org.apache.http.client.methods.HttpGet("http://localhost:" port "/foo")
);
org.apache.http.ProtocolVersion protocolV = response.getStatusLine().getProtocolVersion();
assertNotNull(protocolV);
assertEquals("HTTP", protocolV.getProtocol());
assertEquals(1, protocolV.getMajor());
assertEquals(1, protocolV.getMinor());
}
HttpResponse.getStatusLine()- Apache HttpClient
對不起,OkHttp!
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<!-- <version>3.14.9</version> managed via spring-boot-dependencies -->
<scope>test</scope>
</dependency>
而且,“瞧”:
@Test
public void testOkHttp() throws IOException {
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient();
okhttp3.Request request = new okhttp3.Request.Builder().url(getUriString("/foo")).build();
try (okhttp3.Response response = client.newCall(request).execute()) {
assertEquals(okhttp3.Protocol.HTTP_1_1, response.protocol());
} catch (RuntimeException reexc) {
org.junit.jupiter.api.Assertions.fail(reexc);
}
}
Response.protocol- OkHttp
- github 上的示例存盤庫。
可能這不是最后一個選項......當你以某種方式設法訪問ServletRequest(在你的測驗中)時,你可以發出:getProtocol(),就像這里。
uj5u.com熱心網友回復:
您可以將 OkHttp 與 Spring 結合使用,如下面的回答所回答: Springboot 2:有沒有辦法將 TestRestTemplate 配置為僅使用 HTTP/2 協議?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/365866.html
