Search in sources :

Example 36 with RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder in project rest-assured by rest-assured.

the class LogIfValidationFailsITest method doesnt_log_request_or_response_when_test_fails_when_using_non_static_request_spec_declared_before_enable_logging_since_config_is_immutable_and_spec_config_has_precedence_over_global_config.

@Test
public void doesnt_log_request_or_response_when_test_fails_when_using_non_static_request_spec_declared_before_enable_logging_since_config_is_immutable_and_spec_config_has_precedence_over_global_config() {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    RequestSpecification specification = new RequestSpecBuilder().setConfig(RestAssured.config().logConfig(RestAssured.config().getLogConfig().defaultStream(captor).and().enablePrettyPrinting(true))).addHeader("Api-Key", "1234").build();
    RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.HEADERS);
    try {
        RestAssured.given().spec(specification).param("firstName", "John").param("lastName", "Doe").header("Content-type", "application/json").when().get("/greet").then().body("firstName", equalTo("Hello, Johan2!"));
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), isEmptyString());
    }
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) RequestSpecification(io.restassured.specification.RequestSpecification) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) Test(org.junit.Test)

Example 37 with RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder in project rest-assured by rest-assured.

the class LogIfValidationFailsITest method logging_of_both_request_and_response_validation_works_when_test_fails_when_using_non_static_request_spec_declared_after_enable_logging.

@Test
public void logging_of_both_request_and_response_validation_works_when_test_fails_when_using_non_static_request_spec_declared_after_enable_logging() {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.HEADERS);
    RequestSpecification specification = new RequestSpecBuilder().setConfig(RestAssured.config().logConfig(RestAssured.config().getLogConfig().defaultStream(captor).and().enablePrettyPrinting(true))).addHeader("Api-Key", "1234").build();
    try {
        RestAssured.given().spec(specification).param("firstName", "John").param("lastName", "Doe").header("Content-type", "application/json").when().get("/greet").then().body("firstName", equalTo("Hello, Johan2!"));
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), equalTo("Headers:\t\tApi-Key=1234\n\t\t\t\tAccept=*/*\n\t\t\t\tContent-Type=application/json; charset=" + RestAssured.config().getEncoderConfig().defaultCharsetForContentType(ContentType.JSON) + "\n\nContent-Type: application/json;charset=utf-8\nContent-Length: 33\nServer: Jetty(9.3.2.v20150730)\n"));
    }
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) RequestSpecification(io.restassured.specification.RequestSpecification) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) Test(org.junit.Test)

Example 38 with RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder in project rest-assured by rest-assured.

the class ProxyITest method using_proxy_with_specification.

@Test
public void using_proxy_with_specification() {
    RequestSpecification specification = new RequestSpecBuilder().setProxy("localhost").build();
    given().specification(specification).param("firstName", "John").param("lastName", "Doe").when().get("/greetJSON").then().header("Via", not(isEmptyOrNullString()));
}
Also used : RequestSpecification(io.restassured.specification.RequestSpecification) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) Test(org.junit.Test)

Example 39 with RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder in project rest-assured by rest-assured.

the class RequestSpecMergingITest method setupRA.

@BeforeClass
public static void setupRA() {
    RestAssured.baseURI = "http://example.com";
    RestAssured.port = 80;
    RestAssured.requestSpecification = new RequestSpecBuilder().addHeader("authorization", "abracadabra").build();
}
Also used : RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) BeforeClass(org.junit.BeforeClass)

Example 40 with RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder in project rest-assured by rest-assured.

the class RequestSpecificationITest method filterable_request_specification_returns_correct_port.

@Test
public void filterable_request_specification_returns_correct_port() {
    try {
        RestAssured.baseURI = "http://localhost:8089";
        RequestSpecification spec = new RequestSpecBuilder().addHeader("authorization", "abracadabra").build();
        given().spec(spec).filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                assertThat(requestSpec.getPort(), is(8089));
                return new ResponseBuilder().setStatusCode(200).build();
            }
        }).when().get("/test");
    } finally {
        RestAssured.reset();
    }
}
Also used : Response(io.restassured.response.Response) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) Filter(io.restassured.filter.Filter) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) RequestSpecification(io.restassured.specification.RequestSpecification) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Aggregations

RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)43 RequestSpecification (io.restassured.specification.RequestSpecification)40 Test (org.junit.Test)39 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)7 Filter (io.restassured.filter.Filter)4 Header (io.restassured.http.Header)4 PrintStream (java.io.PrintStream)4 StringWriter (java.io.StringWriter)4 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)4 FilterContext (io.restassured.filter.FilterContext)3 Response (io.restassured.response.Response)3 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)3 Headers (io.restassured.http.Headers)2 List (java.util.List)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 SecurityTest (com.capgemini.ntc.security.SecurityTest)1 ResponseBuilder (io.restassured.builder.ResponseBuilder)1 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)1 LogConfig (io.restassured.config.LogConfig)1 Cookies (io.restassured.http.Cookies)1