Rest assured 请放心-使用localhost进行身份验证时出现错误404

Rest assured 请放心-使用localhost进行身份验证时出现错误404,rest-assured,Rest Assured,我正在使用localhost测试一个API,在使用Postman进行的手动测试中,它运行良好,如屏幕截图所示: 如屏幕截图所示,需要在请求主体中发送用户名和密码,然后API在响应头中返回身份验证密钥,状态为200 OK 但是当我尝试自动化测试以查看是否可以从报头响应中检索密钥时,测试失败,状态代码404,我甚至无法继续查看它是否在报头中获得授权。在自动化此类请求时,我是否缺少一些东西 import io.restassured.RestAssured; import io.restassure

我正在使用localhost测试一个API,在使用Postman进行的手动测试中,它运行良好,如屏幕截图所示:

如屏幕截图所示,需要在请求主体中发送用户名和密码,然后API在响应头中返回身份验证密钥,状态为200 OK

但是当我尝试自动化测试以查看是否可以从报头响应中检索密钥时,测试失败,状态代码404,我甚至无法继续查看它是否在报头中获得授权。在自动化此类请求时,我是否缺少一些东西

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class reusableMethods {

    public static String getSessionKey() {

        RestAssured.baseURI = "http://localhost:3000";
        Response res =
                given().log().all().
                        header("Content-Type", "application/json").
                        body("{\n" +
                                "  \"email\": \"admin@test.com.br\",\n" +
                                "  \"password\": \"passw0rd\"\n" +
                                "}").
                        when().
                            post("/api/cms/v1/auth/login").
                        then().assertThat().
                            statusCode(200).
                            extract().response();

        String headerValue = res.header("Authorization");
        System.out.println(headerValue);
        return headerValue;

    }
当我运行测试时,我总是会得到相同的错误,如下所示以及日志:

Request method: POST
Request URI:    http://localhost:3000/api/cms/v1/auth
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        Accept=*/*
                Content-Type=application/json; charset=UTF-8
Cookies:        <none>
Multiparts:     <none>
Body:
{
    "email": "admin@test.com.br",
    "password": "passw0rd"
}

java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <404>.
请求方法:POST
请求URI:http://localhost:3000/api/cms/v1/auth
代理:
请求参数:
查询参数:
表格参数:
路径参数:
标题:接受=*/*
内容类型=应用程序/json;字符集=UTF-8
曲奇饼:
多部分:
正文:
{
“电子邮件”:admin@test.com.br",
“密码”:“passw0rd”
}
java.lang.AssertionError:1预期失败。
应为状态代码,但为。
您可以使用auth()方法通过身份验证。 它有多个选项,您需要查看哪一个适合您的授权

如果您使用的是OAuth令牌,则类似于:

 Response res =  given()
                .auth().oauth2("Your token")
                .log().all().
                header("Content-Type", "application/json").
                body("{\n" + "  \"email\": \"admin@test.com.br\",\n" +"  \"password\": \"passw0rd\"\n" +"}").
                when().
                    post("/api/cms/v1/auth/login").
                then().
                    assertThat().
                        statusCode(200).extract().response();