Skip to content

Commit

Permalink
Coverage for proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
fedinskiy committed May 18, 2022
1 parent 6c77867 commit 6ead928
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 0 deletions.
27 changes: 27 additions & 0 deletions http/rest-client-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,31 @@
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<profiles>
<!-- Skipped on Windows as does not support Linux Containers / Testcontainers -->
<profile>
<id>skip-tests-on-windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.ts.http.restclient.reactive.proxy;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.smallrye.mutiny.Uni;

@RegisterRestClient
@RegisterClientHeaders
public interface ProxyClient {

@GET
@Path("/")
@Produces(MediaType.TEXT_HTML)
Uni<String> getSite();

@GET
@Path("/example.txt")
@Produces(MediaType.TEXT_PLAIN)
Uni<String> getText();

@GET
@Path("/auth")
@Produces(MediaType.TEXT_PLAIN)
Uni<String> getAuthorized();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.ts.http.restclient.reactive.proxy;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RestClient;

import io.smallrye.mutiny.Uni;

@Path("/proxied")
public class ProxyResource {

@Inject
@RestClient
ProxyClient client;

@GET
@Path("/")
@Produces(MediaType.TEXT_HTML)
public Uni<String> getRoot() {
return client.getSite();
}

@GET
@Path("/banned")
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> getBanned() {
return client.getText();
}

@GET
@Path("/authorization")
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> getAuthorized() {
return client.getAuthorized();
}

}
6 changes: 6 additions & 0 deletions http/rest-client-reactive/src/main/resources/proxy.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient".url=http://example.com
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient".proxy-address=localhost:8090
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient".proxy-user=proxyuser
quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient".proxy-password=proxypassword
quarkus.rest-client.logging.scope=request-response
quarkus.log.category."org.jboss.resteasy.reactive.client.logging".level=DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.quarkus.ts.http.restclient.reactive;

import java.util.Base64;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.Container;
import io.quarkus.test.services.QuarkusApplication;
import io.restassured.response.Response;

@QuarkusScenario
public class ProxyIT {
private static final String USER = "proxyuser";
private static final String PASSWORD = "proxypassword";

@Container(image = "quay.io/quarkusqeteam/proxy", port = 8090, expectedLog = "Configuration complete; ready for start up")
static RestService proxy = new RestService();

@QuarkusApplication
static RestService proxyApp = new RestService()
.withProperties("proxy.properties")
.withProperty("quarkus.rest-client.\"io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient\".proxy-user", USER)
.withProperty("quarkus.rest-client.\"io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient\".proxy-password",
PASSWORD)
.withProperty("quarkus.rest-client.\"io.quarkus.ts.http.restclient.reactive.proxy.ProxyClient\".proxy-address",
() -> proxy.getHost().replace("http://", "") + ":" + proxy.getPort());

@Test
void getThrough() {
Response proxied = proxyApp.given().with().get("/proxied/");
Assertions.assertEquals(HttpStatus.SC_OK, proxied.statusCode());
Assertions.assertTrue(proxied.body().asString().contains("Example Domain"));
}

@Test
void banned() {
Response banned = proxyApp.given().with().get("/proxied/banned");
Assertions.assertEquals(HttpStatus.SC_OK, banned.statusCode());
Assertions.assertEquals("Reading is prohibited by corporate policy!",
banned.body().asString());
}

@Test
/*
* Nginx returns content of Proxy-Auth Header.
* We check, that this content is made according to the specification.
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#proxy_authentication
*/
void authorization() {
Response response = proxyApp.given().with().get("/proxied/authorization");
Assertions.assertEquals(HttpStatus.SC_OK, response.statusCode());
String authorizationType = "Basic";
String credentials = encode(USER + ":" + PASSWORD);
String header = response.body().asString();
Assertions.assertEquals(authorizationType + " " + credentials, header);
}

private static String encode(String source) {
return Base64.getEncoder().encodeToString(source.getBytes());
}
}
2 changes: 2 additions & 0 deletions http/rest-client-reactive/src/test/resources/proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM nginx:1.21
COPY nginx.conf /etc/nginx/nginx.conf
11 changes: 11 additions & 0 deletions http/rest-client-reactive/src/test/resources/proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This folder contains everything for building and deploying proxy container, based on nginx
It is based on these resources:
* http://nginx.org/en/docs/beginners_guide.html#proxy
* http://nginx.org/en/docs/http/ngx_http_proxy_module.html
* https://www.baeldung.com/nginx-forward-proxy

By default, the container listens on port 8090.

Script run.sh is mainly for debugging

Script deploy.sh is for building and deploying to quay.io
3 changes: 3 additions & 0 deletions http/rest-client-reactive/src/test/resources/proxy/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker build -t nginx-proxy .
docker tag nginx-proxy quay.io/quarkusqeteam/proxy:latest
docker push quay.io/quarkusqeteam/proxy:latest
16 changes: 16 additions & 0 deletions http/rest-client-reactive/src/test/resources/proxy/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
events {}
http {
server {
listen 8090;
resolver 8.8.8.8;
location / {
proxy_pass http://$host;
}
location ~ \.(txt)$ {
return 203 "Reading is prohibited by corporate policy!";
}
location /auth {
return 200 $http_proxy_authorization;
}
}
}
1 change: 1 addition & 0 deletions http/rest-client-reactive/src/test/resources/proxy/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker build -t nginx-proxy . && docker run -p 8090:8090 nginx-proxy

0 comments on commit 6ead928

Please sign in to comment.