Skip to content

Commit

Permalink
[closes #4956] Extend netty-http test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
avano committed Jun 19, 2023
1 parent 0f3c8fd commit 980b760
Show file tree
Hide file tree
Showing 14 changed files with 885 additions and 0 deletions.
147 changes: 147 additions & 0 deletions integration-tests/netty-http/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-build-parent-it</artifactId>
<version>3.0.0-SNAPSHOT</version>
<relativePath>../../poms/build-parent-it/pom.xml</relativePath>
</parent>

<artifactId>camel-quarkus-integration-test-netty-http</artifactId>
<name>Camel Quarkus :: Integration Tests :: Netty HTTP</name>
<description>Integration tests for Camel Quarkus Netty HTTP extension</description>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-netty-http</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jaxb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<!-- test dependencies - camel-quarkus -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>virtualDependencies</id>
<activation>
<property>
<name>!noVirtualDependencies</name>
</property>
</activation>
<dependencies>
<!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-mock-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-netty-http-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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.quarkus.component.netty.http;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import io.netty.handler.codec.http.FullHttpResponse;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Response;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.netty.http.NettyHttpMessage;
import org.eclipse.microprofile.config.ConfigProvider;

@Path("/netty/http")
public class NettyHttpResource {
@Inject
ProducerTemplate producerTemplate;

@GET
@Path("/getRequest/{method}/{hName}/{hValue}/{body}")
public String getRequest(@PathParam("method") String method, @PathParam("hName") String headerName,
@PathParam("hValue") String headerValue,
@PathParam("body") String body) {
return producerTemplate.requestBodyAndHeaders(
"netty-http:http://localhost:{{camel.netty-http.port}}/request",
body,
Map.of(Exchange.HTTP_METHOD, method, headerName, headerValue),
String.class);
}

@GET
@Path("/getResponse")
public String getResponse() {
FullHttpResponse response = producerTemplate
.send("netty-http:http://localhost:{{camel.netty-http.port}}/response", ex -> {
}).getIn().getBody(NettyHttpMessage.class).getHttpResponse();
return response.status().reasonPhrase() + " " + response.status().code();
}

@GET
@Path("/auth/{path}/{user}/{password}")
public Response auth(@PathParam("path") String path, @PathParam("user") String user,
@PathParam("password") String password) {
FullHttpResponse response = producerTemplate
.send("netty-http:http://localhost:{{camel.netty-http.port}}/" + path,
ex -> ex.getIn().setHeaders(getAuthHeaders(user, password)))
.getIn().getBody(NettyHttpMessage.class).getHttpResponse();

return Response.status(response.status().code()).build();
}

@GET
@Path("/wildcard/{path}")
public Response wildcard(@PathParam("path") String path) {
final String body = producerTemplate.requestBodyAndHeader(
"netty-http:http://localhost:{{camel.netty-http.port}}/" + path,
null, Exchange.HTTP_METHOD, "GET", String.class);
return Response.ok(body).build();
}

@GET
@Path("/proxy")
public String proxy() {
final int proxyPort = ConfigProvider.getConfig().getValue("camel.netty-http.proxyPort", Integer.class);
final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxyPort));

final int port = ConfigProvider.getConfig().getValue("camel.netty-http.port", Integer.class);
final String url = "http://localhost:" + port + "/proxy";

try {
final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(proxy);
return new String(connection.getInputStream().readAllBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@GET
@Path("/rest/{method}")
public String rest(@PathParam("method") String method) {
return producerTemplate.requestBodyAndHeader(
"netty-http:http://localhost:{{camel.netty-http.restPort}}/rest",
null,
Exchange.HTTP_METHOD, method,
String.class);
}

@GET
@Path("/rest/pojo/{type}")
public String restPojo(@PathParam("type") String type) {
final String body;
final String contentType;
if ("json".equals(type)) {
body = "{\"firstName\":\"John\", \"lastName\":\"Doe\"}";
contentType = "application/json";
} else {
body = "<user firstName=\"John\" lastName=\"Doe\"/>";
contentType = "text/xml";
}
return producerTemplate.requestBodyAndHeaders(
"netty-http:http://localhost:{{camel.netty-http.restPort}}/rest/" + type,
body,
Map.of(Exchange.HTTP_METHOD, "POST", "Content-Type", contentType),
String.class);
}

private Map<String, Object> getAuthHeaders(String user, String password) {
Map<String, Object> headers = new HashMap<>();
headers.put(Exchange.HTTP_METHOD, "GET");
if (!"null".equals(user)) {
headers.put("Authorization", "Basic " + Base64.getEncoder().encodeToString((user + ":" + password).getBytes()));
}
return headers;
}
}
Loading

0 comments on commit 980b760

Please sign in to comment.