io.restassured.config.HttpClientConfig Java Examples

The following examples show how to use io.restassured.config.HttpClientConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IncompletePostTestCase.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Test
public void testIncompleteWrite() throws Exception {
    PostEndpoint.invoked = false;

    //make sure incomplete writes do not block threads
    //and that incoplete data is not delivered to the endpoint
    for (int i = 0; i < 1000; ++i) {
        Socket socket = new Socket(url.getHost(), url.getPort());
        socket.getOutputStream().write(
                "POST /post HTTP/1.1\r\nHost: localhost\r\nContent-length:10\r\n\r\ntest".getBytes(StandardCharsets.UTF_8));
        socket.getOutputStream().flush();
        socket.getOutputStream().close();
        socket.close();
    }

    Assertions.assertFalse(PostEndpoint.invoked);
    RestAssuredConfig config = RestAssured.config()
            .httpClient(HttpClientConfig.httpClientConfig()
                    .setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000)
                    .setParam(CoreConnectionPNames.SO_TIMEOUT, 1000));

    RestAssured.given().config(config).get("/post").then().body(Matchers.is("ok"));
}
 
Example #2
Source File: AccessLogFileTestCase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleLogMessageToFile() throws IOException, InterruptedException {
    // issue the request with a specific HTTP protocol version, so that we can then verify
    // the protocol value logged in the access log file
    final RestAssuredConfig http10Config = RestAssured.config().httpClient(
            new HttpClientConfig().setParam(CoreProtocolPNames.PROTOCOL_VERSION, new ProtocolVersion("HTTP", 1, 0)));
    final RequestSpecification requestSpec = new RequestSpecBuilder().setConfig(http10Config).build();
    final String paramValue = UUID.randomUUID().toString();
    RestAssured.given(requestSpec).get("/does-not-exist?foo=" + paramValue);

    Awaitility.given().pollInterval(100, TimeUnit.MILLISECONDS)
            .atMost(10, TimeUnit.SECONDS)
            .untilAsserted(new ThrowingRunnable() {
                @Override
                public void run() throws Throwable {
                    try (Stream<Path> files = Files.list(logDirectory)) {
                        Assertions.assertEquals(1, (int) files.count());
                    }
                    Path path = logDirectory.resolve("server.log");
                    Assertions.assertTrue(Files.exists(path));
                    String data = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
                    Assertions.assertTrue(data.contains("404"));
                    Assertions.assertTrue(data.contains("/does-not-exist"));
                    Assertions.assertTrue(data.contains("?foo=" + paramValue),
                            "access log is missing query params");
                    Assertions.assertFalse(data.contains("?foo=" + paramValue + "?foo=" + paramValue),
                            "access log contains duplicated query params");
                    Assertions.assertTrue(data.contains("HTTP/1.0"),
                            "HTTP/1.0 protocol value is missing in the access log");
                }
            });
}
 
Example #3
Source File: VerifyResponseSenderExceptionBehaviorComponentTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
private RestAssuredConfig noRetryRestAssuredConfig() {
    return RestAssuredConfig
        .config()
        .httpClient(HttpClientConfig.httpClientConfig().httpClientFactory(
            () -> {
                DefaultHttpClient result = new DefaultHttpClient();
                result.setHttpRequestRetryHandler((exception, executionCount, context) -> false);
                return result;
            })
        );
}
 
Example #4
Source File: CurlLoggingRestAssuredConfigFactory.java    From curl-logger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Updates a given REST-assured configuration to generate curl command using custom options.
 *
 * @param config  an original configuration to update
 * @param options options defining curl generation
 * @return updated configuration; note original configuration remain unchanged.
 */
public static RestAssuredConfig updateConfig(RestAssuredConfig config, Options options) {
  HttpClientConfig.HttpClientFactory originalFactory = getHttpClientFactory(config);
  return config
      .httpClient(config.getHttpClientConfig()
          .reuseHttpClientInstance()
          .httpClientFactory(new MyHttpClientFactory(originalFactory, new CurlLoggingInterceptor(options))));
}
 
Example #5
Source File: CurlLoggingRestAssuredConfigFactory.java    From curl-logger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static HttpClientConfig.HttpClientFactory getHttpClientFactory(RestAssuredConfig config) {
  try {
    Field f = HttpClientConfig.class.getDeclaredField("httpClientFactory");
    f.setAccessible(true);
    HttpClientConfig httpClientConfig = config.getHttpClientConfig();
    HttpClientConfig.HttpClientFactory httpClientFactory = (HttpClientConfig.HttpClientFactory) f.get(httpClientConfig);
    f.setAccessible(false);
    return httpClientFactory;
  } catch (NoSuchFieldException | IllegalAccessException e) {
    throw new RuntimeException(e);
  }
}
 
Example #6
Source File: CurlLoggingRestAssuredConfigFactoryTest.java    From curl-logger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldIncludeCurlInterceptorWhenUpdatingExistingConfig() {

  HttpClientConfig httpClientConfig = HttpClientConfig.httpClientConfig()
      .setParam("TestParam", "TestValue")
      .httpClientFactory(
          new HttpClientConfig.HttpClientFactory() {
            @Override
            public HttpClient createHttpClient() {
              DefaultHttpClient client = new DefaultHttpClient();
              client.addRequestInterceptor(new MyRequestInerceptor());
              return client;
            }
          }
      );
  final RestAssuredConfig config = RestAssuredConfig.config()
      .httpClient(httpClientConfig);

  RestAssuredConfig updatedConfig = CurlLoggingRestAssuredConfigFactory.updateConfig(config, Options.builder().build());

  // original configuration has not been modified
  assertThat(updatedConfig, not(equalTo(config)));
  AbstractHttpClient clientConfig = (AbstractHttpClient) config.getHttpClientConfig().httpClientInstance();
  assertThat(clientConfig, not(new ContainsRequestInterceptor(CurlLoggingInterceptor.class)));
  assertThat(clientConfig, new ContainsRequestInterceptor(MyRequestInerceptor.class));
  assertThat(updatedConfig.getHttpClientConfig().params().get("TestParam"), equalTo("TestValue"));

  // curl logging interceptor is included
  AbstractHttpClient updateClientConfig = (AbstractHttpClient) updatedConfig.getHttpClientConfig().httpClientInstance();
  assertThat(updateClientConfig, new ContainsRequestInterceptor(CurlLoggingInterceptor.class));

  // original interceptors are preserved in new configuration
  assertThat(updateClientConfig, new ContainsRequestInterceptor(MyRequestInerceptor.class));
  // original parameters are preserved in new configuration
  assertThat(updatedConfig.getHttpClientConfig().params().get("TestParam"), equalTo("TestValue"));

}
 
Example #7
Source File: RestAssuredIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
private static RestAssuredConfig getRestAssuredConfig(final ClientCredential credential) {
    return RestAssuredConfig.config().httpClient(HttpClientConfig.httpClientConfig().httpClientFactory(new HttpClientConfig.HttpClientFactory() {
        @Override
        public HttpClient createHttpClient() {
            final DefaultHttpClient client = new DefaultHttpClient();
            client.addRequestInterceptor(new ApacheHttpClientEdgeGridInterceptor(credential));
            client.setRoutePlanner(new ApacheHttpClientEdgeGridRoutePlanner(credential));
            return client;
        }
    }));
}
 
Example #8
Source File: CurlLoggingRestAssuredConfigFactory.java    From curl-logger with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public MyHttpClientFactory(HttpClientConfig.HttpClientFactory wrappedFactory,
                           CurlLoggingInterceptor curlLoggingInterceptor) {
  this.wrappedFactory = wrappedFactory;
  this.curlLoggingInterceptor = curlLoggingInterceptor;
}