gpt4 book ai didi

java - Spring Boot 。如何测试http协议(protocol)版本?

转载 作者:行者123 更新时间:2023-12-05 05:52:35 26 4
gpt4 key购买 nike

我在我的应用程序中使用 Spring Boot 2.6.0Spring MVC。我想将我的 Controller 协议(protocol)从版本 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() 方法不存在,我找不到任何其他方法来测试协议(protocol)版本。有谁知道如何在 spring boot 应用程序中正确测试协议(protocol)版本?

最佳答案

有一个 simple Spring-Starter application ,带有(休息) Controller ,如:

@GetMapping("/foo")
public String foo() {
return "bar";
}

使用java>=11,我们可以用java.net.http客户端测试HTTP协议(protocol)版本(无额外依赖):

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());
}
}

要点:


使用 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());
}

对不起,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);
}
}


可能这不是最后的选择......当您以某种方式设法访问 ServletRequest(在您的测试中)时,您可以发出:getProtocol() , 比如 here .

关于java - Spring Boot 。如何测试http协议(protocol)版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70109143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com