1

我需要使用 REST Assured API 自动化 REST API,但遇到问题。使用 REST Assured API,我需要使用 Spring Security 配置登录。

请看下面的代码:

@Before
public void configure() {
        RestAssured.baseURI = "https://example.com";
        RestAssured.basePath = "/my-portal";
}

@Test
public void apiLogin() {
    given().auth().form("username", "password", springSecurity().withLoggingEnabled(new LogConfig())).when().get();
}

我面临的问题是:

实际请求 URIhttps://example.com/j_spring_security_check
预期请求 URIhttps://example.com/my-portal/j_spring_security_check

以下是获取的响应:

Request method: POST
Request URI:    https://example.com/j_spring_security_check
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    j_username={username}
                j_password={password}
Path params:    <none>
Multiparts:     <none>
Headers:        Accept=*/*
                Content-Type=application/x-www-form-urlencoded; charset=ISO-8859-1
Cookies:        <none>
Body:           <none>
HTTP/1.1 404 Not Found
Date: Mon, 06 Feb 2017 10:51:41 GMT
Content-Length: 0
Connection: close
Content-Type: text/plain
4

1 回答 1

0

我得到了这个工作。只是几个错误,它的工作。

以下是工作代码:

final StringWriter writer = new StringWriter();
        final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
        Response s = given().auth().
                        form(userName, password,
                        FormAuthConfig.springSecurity().withLoggingEnabled(new LogConfig(captor, true)))
                .post("/home/myapp.html");

        System.out.println(s.getBody().asString());

它提供 HTTP 状态码:302(即重定向到页面 /home/myapp.html)。

于 2017-02-21T14:31:40.007 回答