This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Valid all public prefixes work with PayID
Signed-off-by: sappenin <sappenin@gmail.com>
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
payid/java/src/test/java/org/payid/PayIdPublicSuffixesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.payid; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableList.Builder; | ||
import okhttp3.HttpUrl; | ||
import org.immutables.value.Value; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Validates that all public suffixes work properly in a {@link PayId}. | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class PayIdPublicSuffixesTest { | ||
|
||
private ValidPublicTldTestVector validPublicTldTestVector; | ||
|
||
public PayIdPublicSuffixesTest(final ValidPublicTldTestVector validPublicTldTestVector) { | ||
this.validPublicTldTestVector = Objects.requireNonNull(validPublicTldTestVector); | ||
} | ||
|
||
/** | ||
* The data for this test... | ||
*/ | ||
@Parameters | ||
public static Collection<ValidPublicTldTestVector> data() throws URISyntaxException, MalformedURLException { | ||
final Scanner scanner; | ||
try { | ||
URL url = new URL("https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat"); | ||
scanner = new Scanner(url.openStream()); | ||
// read from your scanner | ||
} catch (IOException ex) { | ||
// there was some connection problem, or the file did not exist on the server, | ||
// or your URL was not in the right format. | ||
// think about what to do now, and put it here. | ||
throw new RuntimeException(ex.getMessage(), ex); | ||
} | ||
|
||
final Builder<ValidPublicTldTestVector> vectors = ImmutableList.builder(); | ||
while (scanner.hasNextLine()) { | ||
String line = scanner.nextLine(); | ||
if (line != null && line.trim().length() != 0 && !line.startsWith("//")) { | ||
String host = HttpUrl.parse("https://example." + line.trim()).host(); | ||
vectors.add( | ||
ValidPublicTldTestVector.builder() | ||
.tld(host) | ||
.expectedPayId("alice$" + line.trim()) | ||
.build() | ||
); | ||
} | ||
} | ||
|
||
return vectors.build(); | ||
} | ||
|
||
@Test | ||
public void testPublicTlds() { | ||
final PayId payId = PayId.of("payid:alice$" + validPublicTldTestVector.tld()); | ||
|
||
assertThat(payId).isNotNull(); | ||
assertThat(payId.account()).isEqualTo("alice"); | ||
assertThat(payId.host()).isEqualTo(validPublicTldTestVector.tld()); | ||
assertThat(payId.toString()).isEqualTo("payid:alice$" + validPublicTldTestVector.tld()); | ||
} | ||
|
||
@Value.Immutable | ||
@JsonSerialize(as = ImmutableValidPublicTldTestVector.class) | ||
@JsonDeserialize(as = ImmutableValidPublicTldTestVector.class) | ||
public interface ValidPublicTldTestVector { | ||
|
||
static ImmutableValidPublicTldTestVector.Builder builder() { | ||
return ImmutableValidPublicTldTestVector.builder(); | ||
} | ||
|
||
String tld(); | ||
|
||
String expectedPayId(); | ||
} | ||
} |