使用REST保证在测试中重置连接

时间:2014-03-19 09:30:10

标签: java rest rest-assured

我有一个使用REST保证的测试。在这个测试中,当我将完整的主机和路径传递给get()方法时,一切都很顺利,测试通过。但是,当我尝试使用RestAssured.baseURI和RestAssured.basePath时,就像本教程的这一部分所示:http://code.google.com/p/rest-assured/wiki/Usage#Default_values,测试最终会重置连接。下面我粘贴工作版本和版本与baseURI不起作用,我得到的错误。请帮助:>

工作版:

import com.jayway.restassured.RestAssured;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Iterator;

import static com.jayway.restassured.RestAssured.basic;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;

public class SomeTest {

    @Test(dataProvider = "provideData")
    public void doSomeTest(String someParameter, String someExpectedValue) {

        given().
                auth().basic("login", "pass").
                param("someParameter", someParameter).
        when().
                get("http://some.host/some-base-path/some-path-relevant-in-test").
        then().
                statusCode(200).
                body("some json", containsString(someExpectedValue)).
                log().ifError();
    }

    @DataProvider(name = "provideData")
    public Iterator<Object[]> provideData() {

        //this provides data for test method in irrelevant way
    }

}

不起作用导致连接重置的版本:

import com.jayway.restassured.RestAssured;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Iterator;

import static com.jayway.restassured.RestAssured.basic;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;

public class SomeTest {

    @Test(dataProvider = "provideData")
        public void doSomeTest(String someParameter, String someExpectedValue) {

        RestAssured.baseURI = "http://some.host";
        RestAssured.basePath = "/some-base-path";

        given().
                auth().basic("login", "pass").
                param("someParameter", someParameter).
        when().
                get("/some-path-relevant-in-test").
        then().
                statusCode(200).
                body("some json", containsString(someExpectedValue)).
                log().ifError();
    }

    @DataProvider(name = "provideData")
    public Iterator<Object[]> provideData() {

        //this provides data for test method in irrelevant way
    }
}

错误:

mar 19, 2014 10:26:01 AM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset
mar 19, 2014 10:26:01 AM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: Retrying request

我做错了什么? :(

2 个答案:

答案 0 :(得分:1)

您是否在短时间内提出了很多要求?您可以针对多个请求尝试closing idle connections after each request和/或reuse the HTTP Client instance

答案 1 :(得分:0)

似乎是版本2.8.1中的Restassured修复的错误。 资源泄漏,连接未关闭。请参阅https://github.com/rest-assured/rest-assured/issues/633