Search in sources :

Example 36 with HttpOptions

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

the class TestCorsHttpServer method doTestOption.

@Test
public void doTestOption() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html");
    CloseableHttpResponse response = httpclient.execute(httpOption);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    Assert.assertNotNull("Cors should have added a header: Access-Control-Allow-Origin", response.getLastHeader("Access-Control-Allow-Origin"));
    Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Origin: *", "*", response.getLastHeader("Access-Control-Allow-Origin").getValue());
    response.close();
}
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 37 with HttpOptions

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

the class TestCorsHttpServer method testAccessControlAllowHeaderUsesSystemPropertyWhenSet.

@Test
public void testAccessControlAllowHeaderUsesSystemPropertyWhenSet() throws Exception {
    Assert.assertNull("no System " + SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME + " shoudl be set", System.getProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME));
    final String expectedValue = "origin";
    System.setProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME, expectedValue);
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html");
        CloseableHttpResponse response = httpclient.execute(httpOption);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Headers: " + expectedValue, expectedValue, response.getLastHeader("Access-Control-Allow-Headers").getValue());
        response.close();
    } finally {
        System.clearProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME);
    }
}
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 38 with HttpOptions

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

the class WebDAVCommandsTest method testOptions.

/**
 * Check the DAV, Ms-Author and Allow header
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void testOptions() throws IOException, URISyntaxException {
    // create a user
    Identity user = JunitTestHelper.createAndPersistIdentityAsUser("webdav-1-" + UUID.randomUUID().toString());
    // list root content of its webdav folder
    WebDAVConnection conn = new WebDAVConnection();
    conn.setCredentials(user.getName(), "A6B7C8");
    URI baseUri = conn.getBaseURI().build();
    HttpOptions optionsRoot = conn.createOptions(baseUri);
    HttpResponse optionsResponse = conn.execute(optionsRoot);
    Assert.assertEquals(200, optionsResponse.getStatusLine().getStatusCode());
    // check DAV header
    Header davHeader = optionsResponse.getFirstHeader("DAV");
    String davHeaderValue = davHeader.getValue();
    Assert.assertTrue(davHeaderValue.contains("1"));
    Assert.assertTrue(davHeaderValue.contains("2"));
    // check ms author
    Header msHeader = optionsResponse.getFirstHeader("MS-Author-Via");
    Assert.assertEquals("DAV", msHeader.getValue());
    // check methods
    Header allowHeader = optionsResponse.getFirstHeader("Allow");
    String allowValue = allowHeader.getValue();
    String[] allowedMethods = new String[] { "OPTIONS", "GET", "HEAD", "POST", "DELETE", "TRACE", "PROPPATCH", "COPY", "MOVE", "LOCK", "UNLOCK" };
    for (String allowedMethod : allowedMethods) {
        Assert.assertTrue(allowValue.contains(allowedMethod));
    }
    IOUtils.closeQuietly(conn);
}
Also used : Header(org.apache.http.Header) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) URI(java.net.URI) Test(org.junit.Test) CoursePublishTest(org.olat.restapi.CoursePublishTest)

Example 39 with HttpOptions

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

the class AuthenticationIntegrationTest method testOptionsRequestDoesNotRequireAuth.

@Test
public void testOptionsRequestDoesNotRequireAuth() throws Exception {
    HttpServerTransport httpTransport = internalCluster().getInstance(HttpServerTransport.class);
    InetSocketAddress address = httpTransport.boundAddress().publishAddress().address();
    String uri = String.format(Locale.ENGLISH, "http://%s:%s/", address.getHostName(), address.getPort());
    HttpOptions request = new HttpOptions(uri);
    request.setHeader(HttpHeaderNames.AUTHORIZATION.toString(), "Basic QXJ0aHVyOkV4Y2FsaWJ1cg==");
    request.setHeader(HttpHeaderNames.ORIGIN.toString(), "http://example.com");
    request.setHeader(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD.toString(), "GET");
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse resp = httpClient.execute(request);
    assertThat(resp.getStatusLine().getReasonPhrase(), is("OK"));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InetSocketAddress(java.net.InetSocketAddress) HttpOptions(org.apache.http.client.methods.HttpOptions) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) Test(org.junit.Test)

Example 40 with HttpOptions

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

the class HttpUtil method doRequest.

public static HeadersBodyStatus doRequest(String type, String url, HeadersBody request) throws IOException {
    HttpRequestBase req;
    switch(type) {
        case "get":
            req = new HttpGet(url);
            break;
        case "post":
            req = new HttpPost(url);
            break;
        case "put":
            req = new HttpPut(url);
            break;
        case "delete":
            req = new HttpDelete(url);
            break;
        case "options":
            req = new HttpOptions(url);
            break;
        case "head":
            req = new HttpHead(url);
            break;
        default:
            throw new RuntimeException("Method not supported: " + type);
    }
    addHeaders(req, request.getHeaders());
    if (request.getBody() != null) {
        if (req instanceof HttpEntityEnclosingRequestBase == false) {
            throw new RuntimeException("Request type does not support body: " + type);
        }
        ((HttpEntityEnclosingRequestBase) req).setEntity(new InputStreamEntity(request.getBody()));
    }
    HttpResponse res = getHttpClient().execute(req);
    InputStream responseStream = null;
    if (res.getEntity() != null) {
        responseStream = res.getEntity().getContent();
    } else {
        responseStream = new InputStream() {

            @Override
            public int read() throws IOException {
                return -1;
            }
        };
    }
    Headers headers = new Headers();
    HeaderIterator it = res.headerIterator();
    while (it.hasNext()) {
        org.apache.http.Header header = it.nextHeader();
        headers.add(header.getName(), header.getValue());
    }
    return new HeadersBodyStatus(res.getStatusLine().toString(), headers, responseStream);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpDelete(org.keycloak.client.admin.cli.httpcomponents.HttpDelete) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpHeaders(org.apache.http.HttpHeaders) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HeaderIterator(org.apache.http.HeaderIterator)

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