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();
}
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);
}
}
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);
}
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"));
}
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);
}
Aggregations