2017-07-19 41 views
0

目前使用的邮路我要做的POST请求API_URL /登录和我通过用户名和密码,作为回报,我得到令牌见下图:如何在休息API中使用放心获取授权令牌?可能吗?

示例请求:

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

你能告诉我怎么会我做..我尝试使用。参数,但它说已弃用...

+0

@Test 公共无效testStatusCode(){给予() \t \t字符串响应= 。 ()“012h.asString();参数(”用户名“,”wahmed“ JsonPath jsonPath = new JsonPath(response); String accessToken = jsonPath.getString(“access_token”); \t } } –

+0

@Test 公共无效testStatusCode(){ \t/* \t RestAssured.baseURI = System.getProperty( “http://qa-takehome.dev.aetion.com:4440/login” ); \t PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme(); \t authScheme.setUserName(“wahmed”); \t authScheme.setPassword(“wahmed123”); \t RestAssured.authentication = authScheme; \t */ } –

回答

0

你必须知道模式是什么e回应。例如,如果输出类似这样:

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

    ... 
} 

知道的模式,可以提取整个输出,并通过它分析,像这样的:

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

的身份验证令牌然后可以设置成一个字符串变量

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

extract()。response();返回整个效应初探,但你可以改变这种提取()。路径( “authorization_token”)只是一个字符串

+0

我可以得到令牌现在我不明白我怎么能通过这个创建用户我可以添加多个键:值在头中? public void crea teUser1(){ Response response = RestAssured.given()。header(“Content-Type”,“application/json”).body(post).when()。post(“http://qa-takehome.dev .aetion.com:4440/user“)。then()。log()。ifError()。statusCode(200).contentType(”application/json“)。body(”$“,hasKey(”email“)) 。 body(“any {it.key =='email'}”)是(notNullValue())。extract()。response(); String resp_email = response.path(“email”)。toString(); System.out.println(resp_email);} –

+0

响应响应= \t \t RestAssured.given()。 \t \t \t auth()。 \t \t \t oauth2(auth_token)。 \t \t header(“Content-Type”,“application/json”)。 \t \t body(post)。 \t \t when()。 \t \t后(“http://qa-takehome.dev.aetion.com:4440/user”)。 \t \t then()。 \t \t log()。ifError()。 \t \t statusCode(200)。 \t \t contentType(“application/json”)。 \t \t body(“$”,hasKey(“email”))。 \t \t body(“any {it.key =='email'}”,is(notNullValue()))。 \t \t extract()。response(); –

0

REST保证支持映射Java对象到/从JSON。您只需要在类路径中使用JSON解析器(如Jackson或Gson)。

首先定义一个类来表示用户证书:

class Credentials { 

    private String username; 
    private String password; 

    // Getters and setters 
} 

然后定义类来表示认证令牌:

class AuthenticationToken { 

    private String token; 

    // Getters and setters 
} 

最后执行交换的令牌的证书请求:

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

.post(“/ auth”)必须替换为.post(“http://qa-takehome.dev.aetion。com:4440/login“) –

+0

如果我必须在subsquent方法中使用这个标记,我该怎么做?AutehnticationToken类中的setter正在采用String参数,而这是json格式?我需要先转换吗?我需要在全局声明AuthenticationToken变量,因为这将用于可疑方法? –

+0

@WaheedAhmed首先,您应该花一些时间阅读[REST Assured documentation](https://github.com/rest-assured/rest)第二,我的答案旨在为您提供关于如何执行身份验证的指导,而不是完整的复制和粘贴解决方案。了解代码的作用,然后在您的项目中使用它。如果你需要在一个新的请求令牌,将令牌存储在一个变量中,并使用它... –

相关问题