Skip to content

Commit

Permalink
Add helper methods and test coverage to FindProxyDirective
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaldassari committed Aug 14, 2023
1 parent 915852a commit 6de38eb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/main/java/com/mabl/net/proxy/FindProxyDirective.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mabl.net.proxy;

import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -13,6 +14,7 @@
public class FindProxyDirective {
private static final String CONNECTION_TYPES_UNION = Arrays.stream(ConnectionType.values()).map(ConnectionType::name).collect(Collectors.joining("|"));
private static final Pattern RESULT_PATTERN = Pattern.compile(String.format("(%s)(?:\\s+([^\\s;]+))?", CONNECTION_TYPES_UNION, Pattern.CASE_INSENSITIVE));
private static final String HOST_PORT_DELIMITER = ":";
private final ConnectionType connectionType;
private final Optional<String> proxyHostAndPort;

Expand Down Expand Up @@ -65,10 +67,44 @@ public boolean isProxy() {
return !isDirect();
}

/**
* Get the proxy address associated with this directive.
*
* @return the proxy address, or null if the connection type is {@link ConnectionType#DIRECT}.
*/
public InetSocketAddress proxyAddress() {
return proxyHostAndPort.map(hostAndPort -> {
final String[] hostPortParts = hostAndPort.split(HOST_PORT_DELIMITER);
final String host = hostPortParts[0];
final int port = Integer.parseInt(hostPortParts[1]);
return InetSocketAddress.createUnresolved(host, port);
}).orElse(null);
}

/**
* Gets the proxy host component of the directive, e.g. "192.168.1.1"
*
* @return the proxy host for this directive, or null if the connection type is {@link ConnectionType#DIRECT}.
*/
public String proxyHost() {
return Optional.ofNullable(proxyAddress()).map(InetSocketAddress::getHostString)
.orElse(null);
}

/**
* Gets the proxy port component of the directive, e.g. 8080.
*
* @return the proxy port for this directive, or null if the connection type is {@link ConnectionType#DIRECT}.
*/
public Integer proxyPort() {
return Optional.ofNullable(proxyAddress()).map(InetSocketAddress::getPort)
.orElse(null);
}

/**
* Gets the proxy and host component of the directive, e.g. "10.1.1.1:8080"
*
* @return the proxy:host for this directive.
* @return the proxy:host for this directive, or null if the connection type is {@link ConnectionType#DIRECT}.
*/
public String proxyHostAndPort() {
return proxyHostAndPort.orElse(null);
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/mabl/net/proxy/FindProxyDirectiveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.mabl.net.proxy;

import org.junit.Test;

import java.net.InetSocketAddress;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class FindProxyDirectiveTest {
@Test
public void direct() throws Exception {
final FindProxyDirective directive = FindProxyDirective.parse("DIRECT");
assertTrue(directive.isDirect());
assertFalse(directive.isProxy());
assertEquals(ConnectionType.DIRECT, directive.connectionType());
assertNull(directive.proxyHostAndPort());
assertNull(directive.proxyHost());
assertNull(directive.proxyPort());
assertEquals("DIRECT", directive.toString());
assertEquals(directive, FindProxyDirective.parse(" DIRECT "));
}

@Test
public void proxy() throws Exception {
final FindProxyDirective directive = FindProxyDirective.parse("PROXY 10.0.0.1:8080");
assertFalse(directive.isDirect());
assertTrue(directive.isProxy());
assertEquals(ConnectionType.PROXY, directive.connectionType());
assertEquals("10.0.0.1:8080", directive.proxyHostAndPort());
assertEquals("10.0.0.1", directive.proxyHost());
assertEquals(new Integer(8080), directive.proxyPort());
assertEquals(InetSocketAddress.createUnresolved("10.0.0.1", 8080), directive.proxyAddress());
assertEquals("10.0.0.1", directive.proxyAddress().getHostString());
assertEquals(8080, directive.proxyAddress().getPort());
assertEquals("PROXY 10.0.0.1:8080", directive.toString());
assertEquals(directive, FindProxyDirective.parse(" PROXY 10.0.0.1:8080 "));
}
}

0 comments on commit 6de38eb

Please sign in to comment.