Skip to content

Commit

Permalink
Add more FindProxyResult helper methods and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaldassari committed Aug 13, 2023
1 parent 6f07557 commit 915852a
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 14 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/mabl/net/proxy/FindProxyDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ public ConnectionType connectionType() {
return connectionType;
}

/**
* Tests whether this directive has connection type {@link ConnectionType#DIRECT}.
*
* @return true if this directive has a direct connection type; false otherwise.
*/
public boolean isDirect() {
return connectionType == ConnectionType.DIRECT;
}

/**
* Tests whether this directive has connection type other than {@link ConnectionType#DIRECT}.
*
* @return true if this directive has a proxy connection type; false if the connection type is direct.
*/
public boolean isProxy() {
return !isDirect();
}

/**
* Gets the proxy and host component of the directive, e.g. "10.1.1.1:8080"
*
Expand Down
51 changes: 37 additions & 14 deletions src/main/java/com/mabl/net/proxy/FindProxyResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

Expand All @@ -23,20 +25,6 @@ private FindProxyResult(final List<FindProxyDirective> directives) {
this.directives = Collections.unmodifiableList(directives);
}

@Override
public Iterator<FindProxyDirective> iterator() {
return directives.iterator();
}

/**
* Gets the number of proxy directives contained in this result.
*
* @return the number of directives.
*/
public int size() {
return directives.size();
}

/**
* Gets all proxy directives contained in this result.
*
Expand All @@ -55,6 +43,17 @@ public FindProxyDirective first() {
return directives.get(0);
}

/**
* Finds the first proxy directive in this result with a connection type other than {@link ConnectionType#DIRECT}.
*
* @return the first directive with a non-direct connection type, if any.
*/
public Optional<FindProxyDirective> firstProxy() {
return directives.stream()
.filter(directive -> directive.connectionType() != ConnectionType.DIRECT)
.findFirst();
}

/**
* Gets the proxy directive with the given index.
*
Expand All @@ -65,6 +64,21 @@ public FindProxyDirective get(final int index) {
return directives.get(index);
}

@Override
public Iterator<FindProxyDirective> iterator() {
return directives.iterator();
}

/**
* Creates a normalized copy of this {@link FindProxyResult} by removing all non-unique proxy directives
* while maintaining the original relative ordering of the directives.
*
* @return a normalized copy of this result.
*/
public FindProxyResult normalize() {
return new FindProxyResult(new ArrayList<>(new LinkedHashSet<>(directives)));
}

/**
* Gets a random proxy directive from this result.
*
Expand All @@ -74,6 +88,15 @@ public FindProxyDirective random() {
return get(random.nextInt(size()));
}

/**
* Gets the number of proxy directives contained in this result.
*
* @return the number of directives.
*/
public int size() {
return directives.size();
}

@Override
public String toString() {
return directives.stream()
Expand Down
144 changes: 144 additions & 0 deletions src/test/java/com/mabl/net/proxy/FindProxyResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.mabl.net.proxy;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

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

public class FindProxyResultTest {
@Test
public void all() throws Exception {
final FindProxyResult result = FindProxyResult.parse("PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080; DIRECT");
final List<FindProxyDirective> directives = result.all();

final FindProxyDirective directive1 = directives.get(0);
assertEquals(ConnectionType.PROXY, directive1.connectionType());
assertEquals("10.0.0.1:8080", directive1.proxyHostAndPort());

final FindProxyDirective directive2 = directives.get(1);
assertEquals(ConnectionType.SOCKS, directive2.connectionType());
assertEquals("10.0.0.1:1080", directive2.proxyHostAndPort());

final FindProxyDirective directive3 = directives.get(2);
assertEquals(ConnectionType.DIRECT, directive3.connectionType());
assertNull(directive3.proxyHostAndPort());
}

@Test
public void first() throws Exception {
final FindProxyResult result = FindProxyResult.parse("PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080; DIRECT");
final FindProxyDirective first = result.first();
assertEquals(ConnectionType.PROXY, first.connectionType());
assertEquals("10.0.0.1:8080", first.proxyHostAndPort());
}

@Test
public void firstProxyWithProxyPresent() throws Exception {
final FindProxyResult result = FindProxyResult.parse("DIRECT; PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080");
final Optional<FindProxyDirective> maybeFirstProxy = result.firstProxy();
assertTrue(maybeFirstProxy.isPresent());
final FindProxyDirective firstProxy = maybeFirstProxy.get();
assertEquals(ConnectionType.PROXY, firstProxy.connectionType());
assertEquals("10.0.0.1:8080", firstProxy.proxyHostAndPort());
}

@Test
public void firstProxyWithNoProxyPresent() throws Exception {
final FindProxyResult result = FindProxyResult.parse("DIRECT");
final Optional<FindProxyDirective> maybeFirstProxy = result.firstProxy();
assertFalse(maybeFirstProxy.isPresent());
}

@Test
public void get() throws Exception {
final FindProxyResult result = FindProxyResult.parse("PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080; DIRECT");

final FindProxyDirective directive1 = result.get(0);
assertEquals(ConnectionType.PROXY, directive1.connectionType());
assertEquals("10.0.0.1:8080", directive1.proxyHostAndPort());

final FindProxyDirective directive2 = result.get(1);
assertEquals(ConnectionType.SOCKS, directive2.connectionType());
assertEquals("10.0.0.1:1080", directive2.proxyHostAndPort());

final FindProxyDirective directive3 = result.get(2);
assertEquals(ConnectionType.DIRECT, directive3.connectionType());
assertNull(directive3.proxyHostAndPort());
}

@Test(expected = PacInterpreterException.class)
public void invalid() throws Exception {
FindProxyResult.parse("FOO");
fail("Parsing should have failed");
}

@Test
public void iterator() throws Exception {
final FindProxyResult result = FindProxyResult.parse("PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080; DIRECT");
final List<FindProxyDirective> directives = new ArrayList<>(result.size());
result.iterator().forEachRemaining(directives::add);

final FindProxyDirective directive1 = directives.get(0);
assertEquals(ConnectionType.PROXY, directive1.connectionType());
assertEquals("10.0.0.1:8080", directive1.proxyHostAndPort());

final FindProxyDirective directive2 = directives.get(1);
assertEquals(ConnectionType.SOCKS, directive2.connectionType());
assertEquals("10.0.0.1:1080", directive2.proxyHostAndPort());

final FindProxyDirective directive3 = directives.get(2);
assertEquals(ConnectionType.DIRECT, directive3.connectionType());
assertNull(directive3.proxyHostAndPort());
}

@Test
public void normalize() throws Exception {
final FindProxyResult result = FindProxyResult.parse("PROXY 10.0.0.1:8080; PROXY 10.0.0.1:8080; DIRECT; SOCKS 10.0.0.1:1080; DIRECT; DIRECT");
assertEquals(6, result.size());

final FindProxyResult normalized = result.normalize();
assertEquals(3, normalized.size());

final FindProxyDirective directive1 = normalized.get(0);
assertEquals(ConnectionType.PROXY, directive1.connectionType());
assertEquals("10.0.0.1:8080", directive1.proxyHostAndPort());

final FindProxyDirective directive2 = normalized.get(1);
assertEquals(ConnectionType.DIRECT, directive2.connectionType());
assertNull(directive2.proxyHostAndPort());

final FindProxyDirective directive3 = normalized.get(2);
assertEquals(ConnectionType.SOCKS, directive3.connectionType());
assertEquals("10.0.0.1:1080", directive3.proxyHostAndPort());
}

@Test
public void random() throws Exception {
final FindProxyResult result = FindProxyResult.parse("PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080; DIRECT");
final Set<FindProxyDirective> directives = new HashSet<>(result.all());
for (int ii = 0; ii < 10; ii++) {
assertTrue(directives.contains(result.random()));
}
}

@Test
public void resultToString() throws Exception {
final FindProxyResult result = FindProxyResult.parse("PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080; DIRECT ");
assertEquals("PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080; DIRECT", result.toString());
}

@Test
public void size() throws Exception {
final FindProxyResult result = FindProxyResult.parse("PROXY 10.0.0.1:8080; SOCKS 10.0.0.1:1080; DIRECT");
assertEquals(3, result.size());
}
}

0 comments on commit 915852a

Please sign in to comment.