org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod Java Examples

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod. 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: RouterHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpRequest httpRequest) {
	if (HttpHeaders.is100ContinueExpected(httpRequest)) {
		channelHandlerContext.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
		return;
	}

	// Route
	HttpMethod method = httpRequest.getMethod();
	QueryStringDecoder qsd = new QueryStringDecoder(httpRequest.uri());
	RouteResult<?> routeResult = router.route(method, qsd.path(), qsd.parameters());

	if (routeResult == null) {
		respondNotFound(channelHandlerContext, httpRequest);
		return;
	}

	routed(channelHandlerContext, routeResult, httpRequest);
}
 
Example #2
Source File: Router.java    From flink with Apache License 2.0 6 votes vote down vote up
public RouteResult<T> route(HttpMethod method, String path, Map<String, List<String>> queryParameters) {
	MethodlessRouter<T> router = routers.get(method);
	if (router == null) {
		router = anyMethodRouter;
	}

	String[] tokens = decodePathTokens(path);

	RouteResult<T> ret = router.route(path, path, queryParameters, tokens);
	if (ret != null) {
		return new RouteResult<T>(path, path, ret.pathParams(), queryParameters, ret.target());
	}

	if (router != anyMethodRouter) {
		ret = anyMethodRouter.route(path, path, queryParameters, tokens);
		if (ret != null) {
			return new RouteResult<T>(path, path, ret.pathParams(), queryParameters, ret.target());
		}
	}

	if (notFound != null) {
		return new RouteResult<T>(path, path, Collections.<String, String>emptyMap(), queryParameters, notFound);
	}

	return null;
}
 
Example #3
Source File: RouterHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpRequest httpRequest) {
	if (HttpHeaders.is100ContinueExpected(httpRequest)) {
		channelHandlerContext.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
		return;
	}

	// Route
	HttpMethod method = httpRequest.getMethod();
	QueryStringDecoder qsd = new QueryStringDecoder(httpRequest.uri());
	RouteResult<?> routeResult = router.route(method, qsd.path(), qsd.parameters());

	if (routeResult == null) {
		respondNotFound(channelHandlerContext, httpRequest);
		return;
	}

	routed(channelHandlerContext, routeResult, httpRequest);
}
 
Example #4
Source File: Router.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns all methods that this router handles. For {@code OPTIONS *}.
 */
public Set<HttpMethod> allAllowedMethods() {
	if (anyMethodRouter.size() > 0) {
		Set<HttpMethod> ret = new HashSet<HttpMethod>(9);
		ret.add(HttpMethod.CONNECT);
		ret.add(HttpMethod.DELETE);
		ret.add(HttpMethod.GET);
		ret.add(HttpMethod.HEAD);
		ret.add(HttpMethod.OPTIONS);
		ret.add(HttpMethod.PATCH);
		ret.add(HttpMethod.POST);
		ret.add(HttpMethod.PUT);
		ret.add(HttpMethod.TRACE);
		return ret;
	} else {
		return new HashSet<HttpMethod>(routers.keySet());
	}
}
 
Example #5
Source File: Router.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns allowed methods for a specific URI.
 *
 * <p>For {@code OPTIONS *}, use {@link #allAllowedMethods()} instead of this method.
 */
public Set<HttpMethod> allowedMethods(String uri) {
	QueryStringDecoder decoder = new QueryStringDecoder(uri);
	String[] tokens = PathPattern.removeSlashesAtBothEnds(decoder.path()).split("/");

	if (anyMethodRouter.anyMatched(tokens)) {
		return allAllowedMethods();
	}

	Set<HttpMethod> ret = new HashSet<HttpMethod>(routers.size());
	for (Map.Entry<HttpMethod, MethodlessRouter<T>> entry : routers.entrySet()) {
		MethodlessRouter<T> router = entry.getValue();
		if (router.anyMatched(tokens)) {
			HttpMethod method = entry.getKey();
			ret.add(method);
		}
	}

	return ret;
}
 
Example #6
Source File: Router.java    From flink with Apache License 2.0 6 votes vote down vote up
public RouteResult<T> route(HttpMethod method, String path, Map<String, List<String>> queryParameters) {
	MethodlessRouter<T> router = routers.get(method);
	if (router == null) {
		router = anyMethodRouter;
	}

	String[] tokens = decodePathTokens(path);

	RouteResult<T> ret = router.route(path, path, queryParameters, tokens);
	if (ret != null) {
		return new RouteResult<T>(path, path, ret.pathParams(), queryParameters, ret.target());
	}

	if (router != anyMethodRouter) {
		ret = anyMethodRouter.route(path, path, queryParameters, tokens);
		if (ret != null) {
			return new RouteResult<T>(path, path, ret.pathParams(), queryParameters, ret.target());
		}
	}

	if (notFound != null) {
		return new RouteResult<T>(path, path, Collections.<String, String>emptyMap(), queryParameters, notFound);
	}

	return null;
}
 
Example #7
Source File: Router.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public RouteResult<T> route(HttpMethod method, String path, Map<String, List<String>> queryParameters) {
	MethodlessRouter<T> router = routers.get(method);
	if (router == null) {
		router = anyMethodRouter;
	}

	String[] tokens = decodePathTokens(path);

	RouteResult<T> ret = router.route(path, path, queryParameters, tokens);
	if (ret != null) {
		return new RouteResult<T>(path, path, ret.pathParams(), queryParameters, ret.target());
	}

	if (router != anyMethodRouter) {
		ret = anyMethodRouter.route(path, path, queryParameters, tokens);
		if (ret != null) {
			return new RouteResult<T>(path, path, ret.pathParams(), queryParameters, ret.target());
		}
	}

	if (notFound != null) {
		return new RouteResult<T>(path, path, Collections.<String, String>emptyMap(), queryParameters, notFound);
	}

	return null;
}
 
Example #8
Source File: Router.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns allowed methods for a specific URI.
 *
 * <p>For {@code OPTIONS *}, use {@link #allAllowedMethods()} instead of this method.
 */
public Set<HttpMethod> allowedMethods(String uri) {
	QueryStringDecoder decoder = new QueryStringDecoder(uri);
	String[] tokens = PathPattern.removeSlashesAtBothEnds(decoder.path()).split("/");

	if (anyMethodRouter.anyMatched(tokens)) {
		return allAllowedMethods();
	}

	Set<HttpMethod> ret = new HashSet<HttpMethod>(routers.size());
	for (Map.Entry<HttpMethod, MethodlessRouter<T>> entry : routers.entrySet()) {
		MethodlessRouter<T> router = entry.getValue();
		if (router.anyMatched(tokens)) {
			HttpMethod method = entry.getKey();
			ret.add(method);
		}
	}

	return ret;
}
 
Example #9
Source File: Router.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns all methods that this router handles. For {@code OPTIONS *}.
 */
public Set<HttpMethod> allAllowedMethods() {
	if (anyMethodRouter.size() > 0) {
		Set<HttpMethod> ret = new HashSet<HttpMethod>(9);
		ret.add(HttpMethod.CONNECT);
		ret.add(HttpMethod.DELETE);
		ret.add(HttpMethod.GET);
		ret.add(HttpMethod.HEAD);
		ret.add(HttpMethod.OPTIONS);
		ret.add(HttpMethod.PATCH);
		ret.add(HttpMethod.POST);
		ret.add(HttpMethod.PUT);
		ret.add(HttpMethod.TRACE);
		return ret;
	} else {
		return new HashSet<HttpMethod>(routers.keySet());
	}
}
 
Example #10
Source File: Router.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns allowed methods for a specific URI.
 *
 * <p>For {@code OPTIONS *}, use {@link #allAllowedMethods()} instead of this method.
 */
public Set<HttpMethod> allowedMethods(String uri) {
	QueryStringDecoder decoder = new QueryStringDecoder(uri);
	String[] tokens = PathPattern.removeSlashesAtBothEnds(decoder.path()).split("/");

	if (anyMethodRouter.anyMatched(tokens)) {
		return allAllowedMethods();
	}

	Set<HttpMethod> ret = new HashSet<HttpMethod>(routers.size());
	for (Map.Entry<HttpMethod, MethodlessRouter<T>> entry : routers.entrySet()) {
		MethodlessRouter<T> router = entry.getValue();
		if (router.anyMatched(tokens)) {
			HttpMethod method = entry.getKey();
			ret.add(method);
		}
	}

	return ret;
}
 
Example #11
Source File: RouterHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpRequest httpRequest) {
	if (HttpHeaders.is100ContinueExpected(httpRequest)) {
		channelHandlerContext.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
		return;
	}

	// Route
	HttpMethod method = httpRequest.getMethod();
	QueryStringDecoder qsd = new QueryStringDecoder(httpRequest.uri());
	RouteResult<?> routeResult = router.route(method, qsd.path(), qsd.parameters());

	if (routeResult == null) {
		respondNotFound(channelHandlerContext, httpRequest);
		return;
	}

	routed(channelHandlerContext, routeResult, httpRequest);
}
 
Example #12
Source File: Router.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns all methods that this router handles. For {@code OPTIONS *}.
 */
public Set<HttpMethod> allAllowedMethods() {
	if (anyMethodRouter.size() > 0) {
		Set<HttpMethod> ret = new HashSet<HttpMethod>(9);
		ret.add(HttpMethod.CONNECT);
		ret.add(HttpMethod.DELETE);
		ret.add(HttpMethod.GET);
		ret.add(HttpMethod.HEAD);
		ret.add(HttpMethod.OPTIONS);
		ret.add(HttpMethod.PATCH);
		ret.add(HttpMethod.POST);
		ret.add(HttpMethod.PUT);
		ret.add(HttpMethod.TRACE);
		return ret;
	} else {
		return new HashSet<HttpMethod>(routers.keySet());
	}
}
 
Example #13
Source File: AbstractHandlerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = TestingRestfulGateway.newBuilder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #14
Source File: HttpTestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple PATCH request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to PATCH (http://$host:$host/$path)
 */
public void sendPatchRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
		HttpMethod.PATCH, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #15
Source File: HttpTestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple GET request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to GET (http://$host:$host/$path)
 */
public void sendGetRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
			HttpMethod.GET, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #16
Source File: HttpTestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple GET request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to GET (http://$host:$host/$path)
 */
public void sendGetRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
			HttpMethod.GET, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #17
Source File: HttpTestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple PATCH request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to PATCH (http://$host:$host/$path)
 */
public void sendPatchRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
		HttpMethod.PATCH, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #18
Source File: Router.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private MethodlessRouter<T> getMethodlessRouter(HttpMethod method) {
	if (method == null) {
		return anyMethodRouter;
	}

	MethodlessRouter<T> router = routers.get(method);
	if (router == null) {
		router = new MethodlessRouter<T>();
		routers.put(method, router);
	}

	return router;
}
 
Example #19
Source File: RouterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllowedMethods() {
	assertEquals(9, router.allAllowedMethods().size());

	Set<HttpMethod> methods = router.allowedMethods("/articles");
	assertEquals(2, methods.size());
	assertTrue(methods.contains(GET));
	assertTrue(methods.contains(POST));
}
 
Example #20
Source File: HttpTestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple DELETE request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to DELETE (http://$host:$host/$path)
 */
public void sendDeleteRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
			HttpMethod.DELETE, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #21
Source File: RouterTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllowedMethods() {
	assertEquals(9, router.allAllowedMethods().size());

	Set<HttpMethod> methods = router.allowedMethods("/articles");
	assertEquals(2, methods.size());
	assertTrue(methods.contains(GET));
	assertTrue(methods.contains(POST));
}
 
Example #22
Source File: Router.java    From flink with Apache License 2.0 5 votes vote down vote up
private MethodlessRouter<T> getMethodlessRouter(HttpMethod method) {
	if (method == null) {
		return anyMethodRouter;
	}

	MethodlessRouter<T> router = routers.get(method);
	if (router == null) {
		router = new MethodlessRouter<T>();
		routers.put(method, router);
	}

	return router;
}
 
Example #23
Source File: AbstractHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = TestingRestfulGateway.newBuilder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #24
Source File: HttpTestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple DELETE request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to DELETE (http://$host:$host/$path)
 */
public void sendDeleteRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
			HttpMethod.DELETE, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #25
Source File: HttpTestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple GET request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to GET (http://$host:$host/$path)
 */
public void sendGetRequest(String path, Duration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
			HttpMethod.GET, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #26
Source File: HttpTestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple DELETE request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to DELETE (http://$host:$host/$path)
 */
public void sendDeleteRequest(String path, Duration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
			HttpMethod.DELETE, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #27
Source File: HttpTestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple PATCH request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to PATCH (http://$host:$host/$path)
 */
public void sendPatchRequest(String path, Duration timeout) throws TimeoutException, InterruptedException {
	if (!path.startsWith("/")) {
		path = "/" + path;
	}

	HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
		HttpMethod.PATCH, path);
	getRequest.headers().set(HttpHeaders.Names.HOST, host);
	getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);

	sendRequest(getRequest, timeout);
}
 
Example #28
Source File: Router.java    From flink with Apache License 2.0 5 votes vote down vote up
private MethodlessRouter<T> getMethodlessRouter(HttpMethod method) {
	if (method == null) {
		return anyMethodRouter;
	}

	MethodlessRouter<T> router = routers.get(method);
	if (router == null) {
		router = new MethodlessRouter<T>();
		routers.put(method, router);
	}

	return router;
}
 
Example #29
Source File: AbstractHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = new TestingRestfulGateway.Builder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #30
Source File: RouterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllowedMethods() {
	assertEquals(9, router.allAllowedMethods().size());

	Set<HttpMethod> methods = router.allowedMethods("/articles");
	assertEquals(2, methods.size());
	assertTrue(methods.contains(GET));
	assertTrue(methods.contains(POST));
}