org.apache.http.client.methods.HttpOptions Java Examples

The following examples show how to use org.apache.http.client.methods.HttpOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApacheHttpRequestFactory.java    From aws-sdk-java-v2 with Apache License 2.0 7 votes vote down vote up
private HttpRequestBase createApacheRequest(HttpExecuteRequest request, String uri) {
    switch (request.httpRequest().method()) {
        case HEAD:
            return new HttpHead(uri);
        case GET:
            return new HttpGet(uri);
        case DELETE:
            return new HttpDelete(uri);
        case OPTIONS:
            return new HttpOptions(uri);
        case PATCH:
            return wrapEntity(request, new HttpPatch(uri));
        case POST:
            return wrapEntity(request, new HttpPost(uri));
        case PUT:
            return wrapEntity(request, new HttpPut(uri));
        default:
            throw new RuntimeException("Unknown HTTP method name: " + request.httpRequest().method());
    }
}
 
Example #2
Source File: cfHttpConnection.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
private HttpUriRequest resolveMethod( String _method, boolean _multipart ) throws cfmRunTimeException {
	String method = _method.toUpperCase();
	if ( method.equals( "GET" ) ) {
		return new HttpGet();
	} else if ( method.equals( "POST" ) ) {
		return new HttpPost();
	} else if ( method.equals( "HEAD" ) ) {
		return new HttpHead();
	} else if ( method.equals( "TRACE" ) ) {
		return new HttpTrace();
	} else if ( method.equals( "DELETE" ) ) {
		return new HttpDelete();
	} else if ( method.equals( "OPTIONS" ) ) {
		return new HttpOptions();
	} else if ( method.equals( "PUT" ) ) {
		return new HttpPut();
	} else if ( method.equals( "PATCH" ) ) {
		return new HttpPatch();
	}
	throw newRunTimeException( "Unsupported METHOD value [" + method + "]. Valid METHOD values are GET, POST, HEAD, TRACE, DELETE, OPTIONS, PATCH and PUT." );
}
 
Example #3
Source File: ClientImpl.java    From datamill with ISC License 6 votes vote down vote up
protected HttpUriRequest buildHttpRequest(Method method, URI uri) {
    switch (method) {
        case OPTIONS:
            return new HttpOptions(uri);
        case GET:
            return new HttpGet(uri);
        case HEAD:
            return new HttpHead(uri);
        case POST:
            return new HttpPost(uri);
        case PUT:
            return new HttpPut(uri);
        case DELETE:
            return new HttpDelete(uri);
        case TRACE:
            return new HttpTrace(uri);
        case PATCH:
            return new HttpPatch(uri);
        default:
            throw new IllegalArgumentException("Method " + method + " is not implemented!");
    }
}
 
Example #4
Source File: DocTestUtils.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
static boolean isResponseContentValid(String url) {
    if ("true".equals(System.getProperty("bash.skipUrls", "false"))) {
        return true;
    }

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    try {
        CloseableHttpResponse response = httpClient.execute(new HttpOptions(url));

        Assert.assertTrue("Expected response content for " + url, 404 != response.getStatusLine().getStatusCode());

        String content = EntityUtils.toString(response.getEntity());
        return !content.contains("No matches for");
    } catch (Exception e) {
        return false;
    } finally {
        HttpClientUtils.closeQuietly(httpClient);
    }
}
 
Example #5
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example #6
Source File: HttpComponentsRequestFactory.java    From fahrschein with Apache License 2.0 6 votes vote down vote up
/**
 * Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
 * @param method the HTTP method
 * @param uri the URI
 * @return the Commons HttpMethodBase object
 */
private static HttpUriRequest createHttpUriRequest(String method, URI uri) {
    switch (method) {
        case "GET":
            return new HttpGet(uri);
        case "HEAD":
            return new HttpHead(uri);
        case "POST":
            return new HttpPost(uri);
        case "PUT":
            return new HttpPut(uri);
        case "PATCH":
            return new HttpPatch(uri);
        case "DELETE":
            return new HttpDelete(uri);
        case "OPTIONS":
            return new HttpOptions(uri);
        case "TRACE":
            return new HttpTrace(uri);
        default:
            throw new IllegalArgumentException("Invalid HTTP method: " + method);
    }
}
 
Example #7
Source File: JDiscHttpServletTest.java    From vespa with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #8
Source File: HttpComponentsClientHttpRequestFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
 * @param httpMethod the HTTP method
 * @param uri the URI
 * @return the Commons HttpMethodBase object
 */
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
	switch (httpMethod) {
		case GET:
			return new HttpGet(uri);
		case HEAD:
			return new HttpHead(uri);
		case POST:
			return new HttpPost(uri);
		case PUT:
			return new HttpPut(uri);
		case PATCH:
			return new HttpPatch(uri);
		case DELETE:
			return new HttpDelete(uri);
		case OPTIONS:
			return new HttpOptions(uri);
		case TRACE:
			return new HttpTrace(uri);
		default:
			throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
	}
}
 
Example #9
Source File: ApacheHttpRequestFactory.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
private HttpRequestBase createApacheRequest(Request<?> request, String uri, String encodedParams) throws FakeIOException {
    switch (request.getHttpMethod()) {
        case HEAD:
            return new HttpHead(uri);
        case GET:
            return new HttpGet(uri);
        case DELETE:
            return new HttpDelete(uri);
        case OPTIONS:
            return new HttpOptions(uri);
        case PATCH:
            return wrapEntity(request, new HttpPatch(uri), encodedParams);
        case POST:
            return wrapEntity(request, new HttpPost(uri), encodedParams);
        case PUT:
            return wrapEntity(request, new HttpPut(uri), encodedParams);
        default:
            throw new SdkClientException("Unknown HTTP method name: " + request.getHttpMethod());
    }
}
 
Example #10
Source File: HttpComponentsClientHttpRequestFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
 * @param httpMethod the HTTP method
 * @param uri the URI
 * @return the Commons HttpMethodBase object
 */
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
	switch (httpMethod) {
		case GET:
			return new HttpGet(uri);
		case HEAD:
			return new HttpHead(uri);
		case POST:
			return new HttpPost(uri);
		case PUT:
			return new HttpPut(uri);
		case PATCH:
			return new HttpPatch(uri);
		case DELETE:
			return new HttpDelete(uri);
		case OPTIONS:
			return new HttpOptions(uri);
		case TRACE:
			return new HttpTrace(uri);
		default:
			throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
	}
}
 
Example #11
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@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();
    }

}
 
Example #12
Source File: HttpComponentsClientHttpRequestFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
 * @param httpMethod the HTTP method
 * @param uri the URI
 * @return the Commons HttpMethodBase object
 */
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
	switch (httpMethod) {
		case GET:
			return new HttpGet(uri);
		case HEAD:
			return new HttpHead(uri);
		case POST:
			return new HttpPost(uri);
		case PUT:
			return new HttpPut(uri);
		case PATCH:
			return new HttpPatch(uri);
		case DELETE:
			return new HttpDelete(uri);
		case OPTIONS:
			return new HttpOptions(uri);
		case TRACE:
			return new HttpTrace(uri);
		default:
			throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
	}
}
 
Example #13
Source File: RequestUtils.java    From RoboZombie with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Retrieves the proper extension of {@link HttpRequestBase} for the given {@link InvocationContext}. 
 * This implementation is solely dependent upon the {@link RequestMethod} property in the annotated 
 * metdata of the endpoint method definition.</p>
 *
 * @param context
 * 			the {@link InvocationContext} for which a {@link HttpRequestBase} is to be generated 
 * <br><br>
 * @return the {@link HttpRequestBase} translated from the {@link InvocationContext}'s {@link RequestMethod}
 * <br><br>
 * @throws NullPointerException
 * 			if the supplied {@link InvocationContext} was {@code null} 
 * <br><br>
 * @since 1.3.0
 */
static HttpRequestBase translateRequestMethod(InvocationContext context) {
	
	RequestMethod requestMethod = Metadata.findMethod(assertNotNull(context).getRequest());
	
	switch (requestMethod) {
	
		case POST: return new HttpPost();
		case PUT: return new HttpPut();
		case PATCH: return new HttpPatch();
		case DELETE: return new HttpDelete();
		case HEAD: return new HttpHead();
		case TRACE: return new HttpTrace();
		case OPTIONS: return new HttpOptions();
		
		case GET: default: return new HttpGet();
	}
}
 
Example #14
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void preflightPostClassAnnotationFail2() 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-3");
    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();
    }

}
 
Example #15
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@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();
    }

}
 
Example #16
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void preflightPostClassAnnotationPass2() 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, X-custom-2");
    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);
    assertTrue(requestHeaders[0].getValue().contains("X-custom-1"));
    assertTrue(requestHeaders[0].getValue().contains("X-custom-2"));
    if (httpclient instanceof Closeable) {
        ((Closeable)httpclient).close();
    }

}
 
Example #17
Source File: HttpComponentsClientHttpRequestFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
 * @param httpMethod the HTTP method
 * @param uri the URI
 * @return the Commons HttpMethodBase object
 */
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
	switch (httpMethod) {
		case GET:
			return new HttpGet(uri);
		case HEAD:
			return new HttpHead(uri);
		case POST:
			return new HttpPost(uri);
		case PUT:
			return new HttpPut(uri);
		case PATCH:
			return new HttpPatch(uri);
		case DELETE:
			return new HttpDelete(uri);
		case OPTIONS:
			return new HttpOptions(uri);
		case TRACE:
			return new HttpTrace(uri);
		default:
			throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
	}
}
 
Example #18
Source File: WebConsoleSecurityTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testOptions() throws Exception {
    final HttpURLConnection connection = getConnection();
    connection.setRequestMethod(HttpOptions.METHOD_NAME);
    connection.connect();
    assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, connection.getResponseCode());
}
 
Example #19
Source File: HttpClientStackTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Test public void createOptionsRequest() throws Exception {
    TestRequest.Options request = new TestRequest.Options();
    assertEquals(request.getMethod(), Method.OPTIONS);

    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpOptions);
}
 
Example #20
Source File: ApacheHttpStack.java    From jus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 */
static HttpRequestBase createHttpRequest(
        Request<?> request, Map<String, String> additionalHeaders) {
    switch (request.getMethod()) {
        case Request.Method.GET:
            return new HttpGet(request.getUrlString());
        case Request.Method.DELETE:
            return new HttpDelete(request.getUrlString());
        case Request.Method.POST: {
            HttpPost postRequest = new HttpPost(request.getUrlString());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
            setEntityIfNonEmptyBody(postRequest, request);
            return postRequest;
        }
        case Request.Method.PUT: {
            HttpPut putRequest = new HttpPut(request.getUrlString());
            putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
            setEntityIfNonEmptyBody(putRequest, request);
            return putRequest;
        }
        case Request.Method.HEAD:
            return new HttpHead(request.getUrlString());
        case Request.Method.OPTIONS:
            return new HttpOptions(request.getUrlString());
        case Request.Method.TRACE:
            return new HttpTrace(request.getUrlString());
        case Request.Method.PATCH: {
            HttpPatch patchRequest = new HttpPatch(request.getUrlString());
            patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
            setEntityIfNonEmptyBody(patchRequest, request);
            return patchRequest;
        }
        default:
            throw new IllegalStateException("Unknown request method.");
    }
}
 
Example #21
Source File: ServerFrontendITest.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
@Test
@Category({
	LDP.class,
	HappyPath.class
})
@OperateOnDeployment(DEPLOYMENT)
public void testClientContainerSimulation(@ArquillianResource final URL url) throws Exception {
	LOGGER.info("Started {}",testName.getMethodName());
	HELPER.base(url);
	HELPER.setLegacy(false);
	HttpGet get = HELPER.newRequest(MyApplication.ROOT_PERSON_CONTAINER_PATH,HttpGet.class);
	HttpPost post = HELPER.newRequest(MyApplication.ROOT_PERSON_CONTAINER_PATH,HttpPost.class);
	post.setEntity(
		new StringEntity(
				TEST_SUITE_BODY,
			ContentType.create("text/turtle", "UTF-8"))
	);
	HELPER.httpRequest(get);
	String location = HELPER.httpRequest(post).location;
	HELPER.httpRequest(get);
	String path=HELPER.relativize(location);
	HELPER.httpRequest(HELPER.newRequest(path,HttpOptions.class));
	HELPER.httpRequest(HELPER.newRequest(path,HttpGet.class));
	HELPER.httpRequest(HELPER.newRequest(path,HttpDelete.class));
	HELPER.httpRequest(HELPER.newRequest(path,HttpGet.class));
	HELPER.httpRequest(get);
	LOGGER.info("Completed {}",testName.getMethodName());
}
 
Example #22
Source File: HttpServiceImpl.java    From container with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse Options(final String uri) throws IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());
    final HttpOptions options = new HttpOptions(uri);
    final HttpResponse response = client.execute(options);
    return response;
}
 
Example #23
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void simplePostClassAnnotation() 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", "text/plain");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "POST");
    HttpResponse response = httpclient.execute(httpoptions);
    assertEquals(200, response.getStatusLine().getStatusCode());
    if (httpclient instanceof Closeable) {
        ((Closeable)httpclient).close();
    }

}
 
Example #24
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@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();
    }

}
 
Example #25
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotatedMethodPreflight2() 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/annotatedPut2");
    // 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();
    }

}
 
Example #26
Source File: CrossOriginSimpleTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@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();
    }
}
 
Example #27
Source File: HttpClientStackTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
@Test public void createOptionsRequest() throws Exception {
    TestRequest.Options request = new TestRequest.Options();
    assertEquals(request.getMethod(), Method.OPTIONS);

    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpOptions);
}
 
Example #28
Source File: HttpOptionsHandlerIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpEventsQueryHandlerOptions() throws Exception {
    // test query .../events/getEvents for CORS support
    HttpOptions httpOptions = new HttpOptions(getQueryEventsURI(tenantId));
    HttpResponse response = client.execute(httpOptions);
    assertCorsResponseHeaders(response, allowedOrigins, allowedHeaders, allowedMethods, allowedMaxAge);
}
 
Example #29
Source File: HttpOptionsHandlerIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpMetricsIndexHandlerOptions() throws Exception {
    // test query .../metrics/search for CORS support
    HttpOptions httpOptions = new HttpOptions(getQuerySearchURI(tenantId));
    HttpResponse response = client.execute(httpOptions);
    assertCorsResponseHeaders(response, allowedOrigins, allowedHeaders, allowedMethods, allowedMaxAge);
}
 
Example #30
Source File: HttpOptionsHandlerIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpMetricNamesHandlerOptions() throws Exception {
    // test query .../metric_name/search for CORS support
    HttpOptions httpOptions = new HttpOptions(getMetricNameSearchURI(tenantId));
    HttpResponse response = client.execute(httpOptions);
    assertCorsResponseHeaders(response, allowedOrigins, allowedHeaders, allowedMethods, allowedMaxAge);
}