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 spark by perwendel.
the class SparkTestUtil method getHttpRequest.
private HttpUriRequest getHttpRequest(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) {
try {
String protocol = secureConnection ? "https" : "http";
String uri = protocol + "://localhost:" + port + path;
if (requestMethod.equals("GET")) {
HttpGet httpGet = new HttpGet(uri);
httpGet.setHeader("Accept", acceptType);
addHeaders(reqHeaders, httpGet);
return httpGet;
}
if (requestMethod.equals("POST")) {
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Accept", acceptType);
addHeaders(reqHeaders, httpPost);
httpPost.setEntity(new StringEntity(body));
return httpPost;
}
if (requestMethod.equals("PATCH")) {
HttpPatch httpPatch = new HttpPatch(uri);
httpPatch.setHeader("Accept", acceptType);
addHeaders(reqHeaders, httpPatch);
httpPatch.setEntity(new StringEntity(body));
return httpPatch;
}
if (requestMethod.equals("DELETE")) {
HttpDelete httpDelete = new HttpDelete(uri);
addHeaders(reqHeaders, httpDelete);
httpDelete.setHeader("Accept", acceptType);
return httpDelete;
}
if (requestMethod.equals("PUT")) {
HttpPut httpPut = new HttpPut(uri);
httpPut.setHeader("Accept", acceptType);
addHeaders(reqHeaders, httpPut);
httpPut.setEntity(new StringEntity(body));
return httpPut;
}
if (requestMethod.equals("HEAD")) {
HttpHead httpHead = new HttpHead(uri);
addHeaders(reqHeaders, httpHead);
return httpHead;
}
if (requestMethod.equals("TRACE")) {
HttpTrace httpTrace = new HttpTrace(uri);
addHeaders(reqHeaders, httpTrace);
return httpTrace;
}
if (requestMethod.equals("OPTIONS")) {
HttpOptions httpOptions = new HttpOptions(uri);
addHeaders(reqHeaders, httpOptions);
return httpOptions;
}
if (requestMethod.equals("LOCK")) {
HttpLock httpLock = new HttpLock(uri);
addHeaders(reqHeaders, httpLock);
return httpLock;
}
throw new IllegalArgumentException("Unknown method " + requestMethod);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
use of org.apache.http.client.methods.HttpOptions in project ats-framework by Axway.
the class HttpClient method performHttpRequest.
/**
* Performs some supported HTTP request. Currently <i>read only<i> requests
* are supported: GET, HEAD and OPTIONS
*
* @param requestedHostRelativeUrl location/query without host and port like: "/my_dir/res?myParam=1"
* @param httpMethodName any of the currently supported HTTP methods: GET, HEAD and OPTIONS
* @param needResponse whether caller wants to get the contents returned from this request,
* if false - then null is returned
* @return the response content if present and requested by the caller
* @throws FileTransferClientException
*/
public String performHttpRequest(String requestedHostRelativeUrl, String httpMethodName, boolean needResponse) throws FileTransferException {
checkClientInitialized();
final String getUrl = constructGetUrl(requestedHostRelativeUrl);
HttpRequestBase httpMethod = null;
if (!StringUtils.isNullOrEmpty(httpMethodName)) {
httpMethodName = httpMethodName.trim().toUpperCase();
switch(httpMethodName) {
case "GET":
httpMethod = new HttpGet(getUrl);
break;
case "HEAD":
httpMethod = new HttpHead(getUrl);
break;
case "OPTIONS":
httpMethod = new HttpOptions(getUrl);
break;
}
}
if (httpMethod == null) {
throw new IllegalArgumentException("This method supports only GET, HEAD and OPTIONS methods while you have provided '" + httpMethodName + "'");
}
log.info("Performing " + httpMethodName + " request to: " + getUrl);
addRequestHeaders(httpMethod);
return (String) processHttpRequest(httpMethod, needResponse, true);
}
use of org.apache.http.client.methods.HttpOptions in project jena by apache.
the class FusekiTest method execOptions.
/** Do an HTTP Options. */
public static String execOptions(String url) {
// Prepare and execute
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpUriRequest request = new HttpOptions(url);
HttpResponse response = httpClient.execute(request, (HttpContext) null);
// Response
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (HttpSC.isClientError(statusCode) || HttpSC.isServerError(statusCode)) {
// Error responses can have bodies so it is important to clear up.
String contentPayload = "";
if (response.getEntity() != null)
contentPayload = EntityUtils.toString(response.getEntity());
throw new HttpException(statusCode, statusLine.getReasonPhrase(), contentPayload);
}
HttpResponseLib.nullResponse.handle(url, response);
return response.getFirstHeader(HttpNames.hAllow).getValue();
} catch (IOException ex) {
throw new HttpException(ex);
}
}
Aggregations