2012-08-23 8 views
6

私は残りの保証を使用してREST APIをテストしています。 URLと本文の両方のパラメータでPOSTしようとするとエラーに遭遇しています。これは、手動でテストするときに正しく動作します。安心保証 - パラメータと本文で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) 
... 

はなぜ残りは両方のパラメータやボディ内容で許可されていませアシュアードん実行すると、次のエラーがスローされます。URLを形成するパラメータを削除すると、

テストコードオプション

ではありませんポスト?

+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は、リクエスト本体で送信されるパラメータをフォームにデフォルト設定します。

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

}

関連する問題