2012-08-23 4 views
6

나는 Rest Assured를 사용하여 REST API를 테스트하고 있습니다. url과 body 내용의 매개 변수를 모두 사용하여 POST를 시도 할 때 오류가 발생합니다. 이것은 수동으로 테스트 할 때 올바르게 작동합니다. 매개 변수가 URL을 형성 제거Rest Assured - 매개 변수 및 본문으로 POST 할 수 없습니다.

테스트 코드 옵션을 선택하지 않습니다 :

String endpoint = http://localhost:8080/x/y/z/id?custom=test; 
String body = "[{\"boolField\":true,\"intField\":991}, 
       {\"boolField\":false,\"intField\":998}]"; 
expect().spec(OK).given().body(body).post(endpoint); 

나머지는 허용하지 피보험자 않는 이유

You can either send parameters OR body content in the POST, not both! 

java.lang.IllegalStateException: You can either send parameters OR body content in the POST, not both! 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77) 
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:198) 
at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:282) 
at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$sendRequest(RequestSpecificationImpl.groovy) 
at com.jayway.restassured.internal.RequestSpecificationImpl$this$2$sendRequest.callCurrent(Unknown Source) 
at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy:83) 
... 

을 실행할 때 다음과 같은 오류가 발생 두 매개 변수 및 본문 내용에 게시물?

+0

임 사용하여 나머지 오히려 오래된 인 1.1.6을 피보험자 다음은 예입니다. 그러나 [github] (https://github.com/jayway/rest-assured/blob/master/rest-assured/src/main/groovy/com/jayway/restassured/internal/RequestSpecificationImpl.groovy)의 코드를 보면) 이것은 여전히 ​​문제로 보입니다. –

+0

Post 매개 변수와 본문을 가질 수 있다는 것을 몰랐기 때문에 Rest Assured의 작성자도 마찬가지 였을 것입니다. 너 자신을 안심 시켜라.이 수표를 주석 처리 해 봤니? – mercutio

+0

님이 rest-assured 관련 문제를 생성했습니다. http://code.google.com/p/rest-assured/issues/detail?id=196&thanks=196&ts=1346105863 –

답변

16

"param"또는 "parameter"가 아닌 queryParameter로 매개 변수를 지정해야합니다. POST 용 Param은 요청 본문에 전송되는 매개 변수를 기본값으로 사용합니다.

e.e.

given(). 
     queryParam("name, "value"). 
     body(..). 
when(). 
     post(..); 
+0

또한 1.1.6을 사용하는 경우 쿼리 URL에 지정한 매개 변수가 POST의 매개 변수가 아니라 폼 매개 변수로 취급되는 버그가 있다고 생각합니다. 이것은 오래전에 고쳐졌습니다. 정말로 새로운 버전으로 업데이트해야합니다. – Johan

0

저는 안심할 수는 없지만 매개 변수를 본문으로 옮길 수 있어야합니다. 이것이 일반적인 POST 매개 변수의 작동 방식입니다. 요청 URL의 일부로 매개 변수를 갖는 것은 일반적으로 GET에 대해서만 수행됩니다. 어쩌면 "커스텀 = 테스트"를 바디의 첫 번째 라인으로 만들려고합니까?

+0

불행히도 URL에서 매개 변수를 제거하는 것은 옵션이 아닙니다. –

0

매개 변수를 queryParam으로 지정해야합니다.

RequestSpecification request=new RequestSpecBuilder().build(); 
ResponseSpecification response=new ResponseSpecBuilder().build(); 
@Test 
public void test(){ 
    User user=new User(); 
    given() 
    .spec(request) 
    .queryParam(query_param1_name, query_param1_name_value) 
    .queryParam(query_param2_name, query_param2_name_value) 
    .contentType(ContentType.JSON) 
    .body(user) 
    .post(API_ENDPOINT) 
    .then() 
    .statusCode(200).log().all(); 

}

관련 문제