2017-07-19 40 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)第二,我的答案旨在爲您提供關於如何執行身份驗證的指導,而不是完整的複製和粘貼解決方案。瞭解代碼的作用,然後在您的項目中使用它。如果你需要在一個新的請求令牌,將令牌存儲在一個變量中,並使用它... –

相關問題