use of org.apache.http.client.methods.HttpOptions in project cxf by apache.
the class CrossOriginSimpleTest method preflightPostClassAnnotationFail.
@Test
public void preflightPostClassAnnotationFail() throws ClientProtocolException, IOException {
HttpClient httpclient = HttpClientBuilder.create().build();
HttpOptions httpoptions = new HttpOptions("http://localhost:" + PORT + "/antest/unannotatedPost");
httpoptions.addHeader("Origin", "http://in.org");
// nonsimple header
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());
assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN).length);
assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS).length);
assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS).length);
if (httpclient instanceof Closeable) {
((Closeable) httpclient).close();
}
}
use of org.apache.http.client.methods.HttpOptions in project cxf by apache.
the class CrossOriginSimpleTest method testAnnotatedLocalPreflightNoGo.
@Test
public void testAnnotatedLocalPreflightNoGo() 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 + "/antest/delete");
// this is the origin we expect to get.
http.addHeader("Origin", "http://area51.mil:4444");
http.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "DELETE");
HttpResponse response = httpclient.execute(http);
assertEquals(200, response.getStatusLine().getStatusCode());
assertOriginResponse(false, new String[] { "http://area51.mil:4444" }, false, response);
// we could check that the others are also missing.
if (httpclient instanceof Closeable) {
((Closeable) httpclient).close();
}
}
use of org.apache.http.client.methods.HttpOptions in project cxf by apache.
the class CrossOriginSimpleTest method testAnnotatedLocalPreflight.
@Test
public void testAnnotatedLocalPreflight() 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 + "/antest/delete");
// this is the origin we expect to get.
http.addHeader("Origin", "http://area51.mil:3333");
http.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "DELETE");
HttpResponse response = httpclient.execute(http);
assertEquals(200, response.getStatusLine().getStatusCode());
assertOriginResponse(false, new String[] { "http://area51.mil:3333" }, true, response);
assertAllowCredentials(response, false);
List<String> exposeHeadersValues = headerValues(response.getHeaders(CorsHeaderConstants.HEADER_AC_EXPOSE_HEADERS));
// preflight never returns Expose-Headers
assertEquals(Collections.emptyList(), exposeHeadersValues);
List<String> allowedMethods = headerValues(response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS));
assertEquals(Arrays.asList("DELETE PUT"), allowedMethods);
if (httpclient instanceof Closeable) {
((Closeable) httpclient).close();
}
}
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 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);
}
}
Aggregations