提问人:p0tta 提问时间:8/11/2023 更新时间:8/11/2023 访问量:29
HttpURLConnection 无法在 Java 测试中正确捕获 HTTP 302
HttpURLConnection unable to capture HTTP 302 correctly in a Java test
问:
我正在尝试为重定向到另一个 URL 的 URL /rdrct 编写测试,例如同一主机上的 /test。在应用程序上,重定向工作正常,我在网络检查器中看到 HTTP 302,但是当我编写 Java 测试时,它失败了,并且出现以下错误。
预期:<200>但实际为:<302>
@Test
public void testRedirect() throws Exception {
String uri = "http://localhost:" + port + "/rdrct";
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,"
+ "image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
connection.connect();
int responseCode = connection.getResponseCode();
assertEquals(responseCode, 302);
}
我错过了什么或做错了什么?
答: 暂无答案
评论
assertEquals
/test