This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 5c91d1fb1a5 CAMEL-21741: Add test for MainHttpServer needs to get registered/started explicitly (#17156) 5c91d1fb1a5 is described below commit 5c91d1fb1a585459f1220d38bbcc17f703a17763 Author: Thomas Diesler <tdies...@redhat.com> AuthorDate: Sun Feb 16 16:14:31 2025 +0100 CAMEL-21741: Add test for MainHttpServer needs to get registered/started explicitly (#17156) --- .../http/main/MainHttpServerRouteTest.java | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/MainHttpServerRouteTest.java b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/MainHttpServerRouteTest.java new file mode 100644 index 00000000000..640ee0b0278 --- /dev/null +++ b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/MainHttpServerRouteTest.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.component.platform.http.main; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.test.AvailablePortFinder; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MainHttpServerRouteTest { + + private static final int port = AvailablePortFinder.getNextAvailable(); + + private static CamelContext camelContext; + + @BeforeAll + static void setUp() throws Exception { + + camelContext = new DefaultCamelContext(); + camelContext.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("platform-http:/hello") + .setBody(constant("Hello, Camel!")); + } + }); + + // MainHttpServer needs to get registered/started explicitly + // https://issues.apache.org/jira/browse/CAMEL-21741 + MainHttpServer httpServer = new MainHttpServer(); + httpServer.setPort(port); + + camelContext.addService(httpServer); + camelContext.start(); + } + + @AfterAll + static void tearDown() { + if (camelContext != null) { + camelContext.stop(); + } + } + + @Test + public void routeStatusOk() throws Exception { + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("http://localhost:" + port + "/hello")) + .build(); + + HttpResponse<String> response = HttpClient.newBuilder().build() + .send(request, HttpResponse.BodyHandlers.ofString()); + + assertEquals(200, response.statusCode()); + assertEquals("Hello, Camel!", response.body()); + } +}