Search in sources :

Example 26 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project vespa by vespa-engine.

the class JDiscHttpServletTest method requireThatServerRespondsToAllMethods.

@Test
public void requireThatServerRespondsToAllMethods() throws Exception {
    final TestDriver driver = TestDrivers.newInstance(newEchoHandler());
    final URI uri = driver.client().newUri("/status.html");
    driver.client().execute(new HttpGet(uri)).expectStatusCode(is(OK));
    driver.client().execute(new HttpPost(uri)).expectStatusCode(is(OK));
    driver.client().execute(new HttpHead(uri)).expectStatusCode(is(OK));
    driver.client().execute(new HttpPut(uri)).expectStatusCode(is(OK));
    driver.client().execute(new HttpDelete(uri)).expectStatusCode(is(OK));
    driver.client().execute(new HttpOptions(uri)).expectStatusCode(is(OK));
    driver.client().execute(new HttpTrace(uri)).expectStatusCode(is(OK));
    driver.client().execute(new HttpPatch(uri)).expectStatusCode(is(OK));
    assertThat(driver.close(), is(true));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpTrace(org.apache.http.client.methods.HttpTrace) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch) Test(org.testng.annotations.Test)

Example 27 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project wildfly by wildfly.

the class MicroProfileMetricsMetadataTestCase method testJsonHeader.

@Test
public void testJsonHeader() throws Exception {
    final String endpointURL = "http://" + managementClient.getMgmtAddress() + ":" + managementClient.getMgmtPort() + "/metrics";
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        HttpOptions request = new HttpOptions(endpointURL);
        // Due to a regression in https://github.com/smallrye/smallrye-metrics/issues/151, the media type can not specify its charset
        request.setHeader("Accept", "application/json");
        CloseableHttpResponse resp = client.execute(request);
        String content = EntityUtils.toString(resp.getEntity());
        assertEquals(content, 200, resp.getStatusLine().getStatusCode());
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpOptions(org.apache.http.client.methods.HttpOptions) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 28 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project wildfly by wildfly.

the class MicroProfileMetricsMetadataTestCase method testNoJsonHeader.

@Test
public void testNoJsonHeader() throws Exception {
    final String endpointURL = "http://" + managementClient.getMgmtAddress() + ":" + managementClient.getMgmtPort() + "/metrics";
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        CloseableHttpResponse resp = client.execute(new HttpOptions(endpointURL));
        String content = EntityUtils.toString(resp.getEntity());
        resp.close();
        // smallrye-metrics no reports 405 Method Not Allowed when an Options is requested with out the JSON type
        assertEquals(405, resp.getStatusLine().getStatusCode());
        assertTrue("'OPTIONS method is only allowed with application/json media type.' message is expected, but was: " + content, content.contains("OPTIONS method is only allowed with application/json media type."));
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpOptions(org.apache.http.client.methods.HttpOptions) Test(org.junit.Test)

Example 29 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project cxf by apache.

the class CrossOriginSimpleTest method preflightPostClassAnnotationPass.

@Test
public void preflightPostClassAnnotationPass() throws ClientProtocolException, IOException {
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpOptions httpoptions = new HttpOptions("http://localhost:" + PORT + "/antest/unannotatedPost");
    httpoptions.addHeader("Origin", "http://area51.mil:31415");
    httpoptions.addHeader("Content-Type", "application/json");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "POST");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_HEADERS, "X-custom-1");
    HttpResponse response = httpclient.execute(httpoptions);
    assertEquals(200, response.getStatusLine().getStatusCode());
    Header[] origin = response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN);
    assertEquals(1, origin.length);
    assertEquals("http://area51.mil:31415", origin[0].getValue());
    Header[] method = response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS);
    assertEquals(1, method.length);
    assertEquals("POST", method[0].getValue());
    Header[] requestHeaders = response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS);
    assertEquals(1, requestHeaders.length);
    assertEquals("X-custom-1", requestHeaders[0].getValue());
    if (httpclient instanceof Closeable) {
        ((Closeable) httpclient).close();
    }
}
Also used : Header(org.apache.http.Header) HttpClient(org.apache.http.client.HttpClient) Closeable(java.io.Closeable) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 30 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project cxf by apache.

the class CrossOriginSimpleTest method testAnnotatedMethodPreflight.

@Test
public void testAnnotatedMethodPreflight() throws Exception {
    configureAllowOrigins(true, null);
    String r = configClient.replacePath("/setAllowCredentials/false").accept("text/plain").post(null, String.class);
    assertEquals("ok", r);
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpOptions http = new HttpOptions("http://localhost:" + PORT + "/untest/annotatedPut");
    // this is the origin we expect to get.
    http.addHeader("Origin", "http://area51.mil:31415");
    http.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "PUT");
    http.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_HEADERS, "X-custom-1, x-custom-2");
    HttpResponse response = httpclient.execute(http);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertOriginResponse(false, new String[] { "http://area51.mil:31415" }, true, response);
    assertAllowCredentials(response, true);
    List<String> exposeHeadersValues = headerValues(response.getHeaders(CorsHeaderConstants.HEADER_AC_EXPOSE_HEADERS));
    // preflight never returns Expose-Headers
    assertEquals(Collections.emptyList(), exposeHeadersValues);
    List<String> allowHeadersValues = headerValues(response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS));
    assertEquals(Arrays.asList(new String[] { "X-custom-1", "x-custom-2" }), allowHeadersValues);
    if (httpclient instanceof Closeable) {
        ((Closeable) httpclient).close();
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) Closeable(java.io.Closeable) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Aggregations

HttpOptions (org.apache.http.client.methods.HttpOptions)41 Test (org.junit.Test)25 HttpResponse (org.apache.http.HttpResponse)15 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)15 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 Closeable (java.io.Closeable)9 HttpClient (org.apache.http.client.HttpClient)9 HttpHead (org.apache.http.client.methods.HttpHead)9 HttpPost (org.apache.http.client.methods.HttpPost)8 HttpPut (org.apache.http.client.methods.HttpPut)8 Header (org.apache.http.Header)7 HttpGet (org.apache.http.client.methods.HttpGet)7 IOException (java.io.IOException)5 URI (java.net.URI)5 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)5 HttpTrace (org.apache.http.client.methods.HttpTrace)5 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)5 TestRequest (com.android.volley.mock.TestRequest)4 HttpDelete (org.apache.http.client.methods.HttpDelete)4 HttpPatch (org.apache.http.client.methods.HttpPatch)4