2017-11-06 5 views
0

를 사용하여 인증 할 때, 그것은 우체부를 사용하여 잘 갔다 수동 테스트에서, 스크린 샷에서 다음과 같이 스크린 샷에서 볼 수 있듯이 Postman나머지는 피보험자 - 오류 404을 내가 localhost를 사용하여 API를 테스트하고있어 로컬 호스트

, 그럴 필요는 있어요 요청 본문에 사용자 이름과 암호를 보내려면 API가 응답 헤더에 Authentication 키를 반환하고 Status 200 OK를 반환합니다.

그러나 헤더 응답에서 키를 검색 할 수 있는지 테스트를 자동화하려고 시도했을 때 statusCode 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\": \"[email protected]\",\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": "[email protected]", 
    "password": "passw0rd" 
} 

java.lang.AssertionError: 1 expectation failed. 
Expected status code <200> but was <404>. 

답변

0

. 여러 가지 옵션이 있습니다. 어느 것이 당신의 인증에 적합한 지 알아야합니다.

귀하의 OAuth 토큰을 사용하는 경우, 그것은 같은 것입니다 :

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