How can I get authorization token in rest API using rest assured? Is it possible?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 06:41:16

问题


Currently using Postman I have to do post request to API_URL/login and I pass username and password and in return i get token see below:

Example Request:

/login
POST
Body
{
    "username": "admin",
    "password": "admin"
}
Return
{
    "token": "1234-56789-..."
}

Can you tell me how would I do .. i tried using .parameters but it says deprecated...


回答1:


You'd have to know what the schema is of the response. For instance, if the output is like thus:

{
    "success": true,
    "authorization_token": "eyJ0eXAiOiJCZWFy...",
    "refresh_token": "eyJ0eXAiOiJCZWFyZ...",
    "type": "Bearer",

    ...
}

Knowing the schema, you can extract the entire output and parse through it, like thus:

import io.restassured.response.Response;

Response response =  
        RestAssured.given().
            header("Content-Type", "application/json").
            body(loginPayload).
        when().
            post("/login").
        then().
            log().ifError().
            statusCode(200).
            contentType("application/vnd.api+json").
            body("$", hasKey("authorization_token")).                                   //authorization_token is present in the response
            body("any { it.key == 'authorization_token' }", is(notNullValue())).        //authorization_token value is not null - has a value
        extract().response();

The auth token could then be set to a string variable

String auth_token = response.path("authorization_token").toString();

extract().response(); returns the entire reponse, but you could change this to extract().path("authorization_token") for just single string




回答2:


REST Assured supports mapping Java objects to/from JSON. You just need a JSON parser such as Jackson or Gson on the classpath.

First define a class to represent the user credentials:

class Credentials {

    private String username;
    private String password;

    // Getters and setters
}

Then define class to represent the authentication token:

class AuthenticationToken {

    private String token;

    // Getters and setters
}

And finally perform the request to exchange the credentials for the token:

@Test
public void authenticate() {

    Credentials credentials = new Credentials();
    credentials.setUsername("admin");
    credentials.setPassword("password");

    AuthenticationToken authenticationToken =

        given()
            .accept("application/json")
            .contentType("application/json")
            .body(credentials)
        .expect()
            .statusCode(200)
        .when()
            .post("/login")
        .then()
            .log().all()
            .extract()
                .body().as(AuthenticationToken.class);

    assertNotNull(authenticationToken.getToken());
}


来源:https://stackoverflow.com/questions/45188549/how-can-i-get-authorization-token-in-rest-api-using-rest-assured-is-it-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!