Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP proxy configuration #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add proxy test
marc92w committed Oct 25, 2023
commit 49576c8be793b0d9fca4012c6ced766b3d261a8a
93 changes: 93 additions & 0 deletions src/test/java/de/taimos/httputils/ProxyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package de.taimos.httputils;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

/**
* Copyright 2023 Cinovo AG<br>
* <br>
*
* @author mweise
*/
public class ProxyTest {
private WireMockServer targetService;
private WireMock target;

private WireMockServer proxyingService;
private WireMock proxy;

@Before
public void init() {
String bindAddress = "127.0.0.1";
this.targetService = new WireMockServer(wireMockConfig().dynamicPort().bindAddress(bindAddress).stubCorsEnabled(true));
this.targetService.start();
this.target = WireMock.create().port(this.targetService.port()).build();

this.proxyingService = new WireMockServer(wireMockConfig().dynamicPort().bindAddress(bindAddress));
this.proxyingService.start();
this.proxy = WireMock.create().port(this.proxyingService.port()).build();

WireMock.configureFor(this.targetService.port());
}

@After
public void stop() {
this.targetService.stop();
this.proxyingService.stop();
}

@Test
public void testWithoutHttpProxy() {
String targetPath = "/direct";
this.target.register(
get(urlEqualTo(targetPath))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("Content")));


this.proxy.register(get(urlEqualTo(targetPath)).atPriority(10).willReturn(aResponse().proxiedFrom(this.targetService.baseUrl())));

String targetUrl = this.targetService.url(targetPath);
HTTPRequest request = WS.url(targetUrl); // direct access
try (HTTPResponse response = request.get()) {
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("Content", response.getResponseAsString());
this.proxy.verifyThat(0, getRequestedFor(urlMatching(targetPath)));
this.target.verifyThat(1, getRequestedFor(urlMatching(targetPath)));
}
}

@Test
public void testWithHttpProxy() {
String targetPath = "/proxied";

this.target.register(
get(urlEqualTo(targetPath))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("Proxied content")));

this.proxy.register(get(urlEqualTo(targetPath)).atPriority(10).willReturn(aResponse().proxiedFrom(this.targetService.baseUrl())));

String targetUrl = this.targetService.url(targetPath);
HTTPRequest request = WS.url(targetUrl).proxy("localhost", this.proxyingService.port());
try (HTTPResponse response = request.get()) {
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("Proxied content", response.getResponseAsString());
this.proxy.verifyThat(1, getRequestedFor(urlMatching(targetPath)));
this.target.verifyThat(1, getRequestedFor(urlMatching(targetPath)));
}
}
}