Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 5 additions & 7 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ src/main/java/com/auth0/client/LoggingOptions.java
# TokenProvider interface (shared between Fern-generated Management API and auth0-provided Authentication API)
src/main/java/com/auth0/client/mgmt/TokenProvider.java

# Test infrastructure from auth0-real
src/test/java/com/auth0/net/
src/test/java/com/auth0/exception/
src/test/java/com/auth0/utils/
src/test/java/com/auth0/json/
# Shared test infrastructure for the Authentication API tests (manually maintained from auth0-real)
src/test/java/com/auth0/client/MockServer.java
src/test/java/com/auth0/client/RecordedRequestMatcher.java
src/test/java/com/auth0/client/UrlMatcher.java
src/test/java/com/auth0/AssertsUtil.java
src/test/resources/auth/
src/test/resources/keys/
src/test/resources/mgmt/
src/test/resources/mockito-extensions/
src/test/java/com/auth0/client/legacy/

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/auth0/AssertsUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.auth0;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.function.Executable;

public class AssertsUtil {

public static <T extends Throwable> T verifyThrows(Class<T> expectedType, Executable executable) {
return assertThrows(expectedType, executable);
}

public static <T extends Throwable> T verifyThrows(Class<T> expectedType, Executable executable, String message) {
T result = assertThrows(expectedType, executable);
assertThat(result.getMessage(), is(message));
return result;
}
}
361 changes: 361 additions & 0 deletions src/test/java/com/auth0/client/MockServer.java

Large diffs are not rendered by default.

156 changes: 156 additions & 0 deletions src/test/java/com/auth0/client/RecordedRequestMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.auth0.client;

import com.auth0.net.client.HttpMethod;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.RecordedRequest;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

public class RecordedRequestMatcher extends TypeSafeDiagnosingMatcher<RecordedRequest> {

private static final int METHOD_PATH = 0;
private static final int HEADER = 1;
private static final int QUERY_PARAMETER = 2;
private static final int QUERY_PARAMETER_PRESENCE = 3;

private final int checkingOption;
private final String first;
private final String second;

private RecordedRequestMatcher(String first, String second, int checkingOption) {
this.checkingOption = checkingOption;
this.first = first;
this.second = second;
}

@Override
protected boolean matchesSafely(RecordedRequest item, Description mismatchDescription) {
if (item == null) {
mismatchDescription.appendText("was null");
return false;
}

switch (checkingOption) {
default:
case METHOD_PATH:
return matchesMethodAndPath(item, mismatchDescription);
case HEADER:
return matchesHeader(item, mismatchDescription);
case QUERY_PARAMETER:
return matchesQueryParameter(item, mismatchDescription);
case QUERY_PARAMETER_PRESENCE:
return hasQueryParameter(item, mismatchDescription);
}
}

private boolean matchesMethodAndPath(RecordedRequest item, Description mismatchDescription) {
if (!item.getMethod().equalsIgnoreCase(first)) {
mismatchDescription.appendText("method was ").appendValue(item.getMethod());
return false;
}
String path = item.getPath();
boolean hasQuery = path.indexOf("?") > 0;
if (hasQuery) {
path = path.substring(0, path.indexOf("?"));
}
if (!path.equals(second)) {
mismatchDescription.appendText("path was ").appendValue(path);
return false;
}

return true;
}

private boolean matchesHeader(RecordedRequest item, Description mismatchDescription) {
String value = item.getHeader(first);
if (value != null && !value.equals(second) || value == null && second != null) {
mismatchDescription.appendText(first).appendText(" header was ").appendValue(value);
return false;
}

return true;
}

private boolean matchesQueryParameter(RecordedRequest item, Description mismatchDescription) {
HttpUrl requestUrl = item.getRequestUrl();
if (requestUrl.querySize() == 0) {
mismatchDescription.appendText(" query was empty");
return false;
}

String queryParamValue = requestUrl.queryParameter(first);
if (second.equals(queryParamValue)) {
return true;
}

mismatchDescription.appendValueList(
"Query parameters were {", ", ", "}.", requestUrl.query().split("&"));
return false;
}

private boolean hasQueryParameter(RecordedRequest item, Description mismatchDescription) {
String path = item.getPath();
boolean hasQuery = path.indexOf("?") > 0;
if (!hasQuery) {
mismatchDescription.appendText(" query was empty");
return false;
}

String query = path.substring(path.indexOf("?") + 1);
String[] parameters = query.split("&");
for (String p : parameters) {
if (p.startsWith(String.format("%s=", first))) {
return true;
}
}
mismatchDescription.appendValueList("Query parameters were {", ", ", "}.", parameters);
return false;
}

@Override
public void describeTo(Description description) {
switch (checkingOption) {
default:
case METHOD_PATH:
description
.appendText("A request with method ")
.appendValue(first)
.appendText(" and path ")
.appendValue(second);
break;
case HEADER:
description
.appendText("A request containing header ")
.appendValue(first)
.appendText(" with value ")
.appendValue(second);
break;
case QUERY_PARAMETER:
description
.appendText("A request containing query parameter ")
.appendValue(first)
.appendText(" with value ")
.appendValue(second);
break;
case QUERY_PARAMETER_PRESENCE:
description.appendText("A request containing query parameter ").appendValue(first);
break;
}
}

public static RecordedRequestMatcher hasMethodAndPath(HttpMethod method, String path) {
return new RecordedRequestMatcher(method.toString(), path, METHOD_PATH);
}

public static RecordedRequestMatcher hasHeader(String name, String value) {
return new RecordedRequestMatcher(name, value, HEADER);
}

public static RecordedRequestMatcher hasQueryParameter(String name, String value) {
return new RecordedRequestMatcher(name, value, QUERY_PARAMETER);
}

public static RecordedRequestMatcher hasQueryParameter(String name) {
return new RecordedRequestMatcher(name, null, QUERY_PARAMETER_PRESENCE);
}
}
154 changes: 154 additions & 0 deletions src/test/java/com/auth0/client/UrlMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.auth0.client;

import java.util.Arrays;
import okhttp3.HttpUrl;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

public class UrlMatcher extends TypeSafeDiagnosingMatcher<String> {

private static final int BASE_URL = 0;
private static final int QUERY_PARAMETER = 1;
private static final int ENCODED_QUERY = 2;
private final int checkingOption;

private String encodedQueryContains;
private String scheme;
private String host;
private String path;
private String paramKey;
private String paramValue;

private UrlMatcher(String scheme, String host, String path) {
this.checkingOption = BASE_URL;
this.scheme = scheme;
this.host = host;
this.path = path;
}

private UrlMatcher(String paramKey, String paramValue) {
this.checkingOption = QUERY_PARAMETER;
this.paramKey = paramKey;
this.paramValue = paramValue;
}

private UrlMatcher(String encodedQueryContains) {
this.checkingOption = ENCODED_QUERY;
this.encodedQueryContains = encodedQueryContains;
}

@Override
protected boolean matchesSafely(String item, Description mismatchDescription) {
if (item == null) {
mismatchDescription.appendText("was null");
return false;
}
HttpUrl url = HttpUrl.parse(item);
if (url == null) {
mismatchDescription.appendText("was not a valid url");
return false;
}

switch (checkingOption) {
default:
case BASE_URL:
return matchesBaseUrl(url, mismatchDescription);
case QUERY_PARAMETER:
return matchesParameter(url, mismatchDescription);
case ENCODED_QUERY:
return matchesEncodedQuery(url, mismatchDescription);
}
}

private boolean matchesEncodedQuery(HttpUrl url, Description mismatchDescription) {
if (!url.encodedQuery().contains(encodedQueryContains)) {
mismatchDescription.appendText("encoded query was ").appendValue(url.encodedQuery());
return false;
}

return true;
}

private boolean matchesBaseUrl(HttpUrl url, Description mismatchDescription) {
if (!url.scheme().equals(scheme)) {
mismatchDescription.appendText("scheme was ").appendValue(url.scheme());
return false;
}
if (!url.host().equals(host)) {
mismatchDescription.appendText("host was ").appendValue(url.host());
return false;
}
if (path == null) {
return true;
}
if (path.startsWith("/")) {
path = path.substring(1);
}
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
if (!url.pathSegments().equals(Arrays.asList(path.split("/")))) {
StringBuilder sb = new StringBuilder();
for (String p : url.pathSegments()) {
sb.append(p).append("/");
}
sb.deleteCharAt(sb.length() - 1);
mismatchDescription.appendText("path was ").appendValue(sb.toString());
return false;
}

return true;
}

private boolean matchesParameter(HttpUrl url, Description mismatchDescription) {
String value = url.queryParameter(paramKey);
if (value != null && !value.equals(paramValue) || value == null && paramValue != null) {
mismatchDescription.appendText("value was ").appendValue(value);
return false;
}

return true;
}

@Override
public void describeTo(Description description) {
switch (checkingOption) {
default:
case BASE_URL:
description
.appendText("An url with scheme ")
.appendValue(scheme)
.appendText(", host ")
.appendValue(host)
.appendText("and path ")
.appendValue(path);
break;
case QUERY_PARAMETER:
description
.appendText("An url with the query parameter ")
.appendValue(paramKey)
.appendText(" with value ")
.appendValue(paramValue);
break;
case ENCODED_QUERY:
description.appendText("An url with encoded query containing ").appendValue(encodedQueryContains);
break;
}
}

public static UrlMatcher isUrl(String scheme, String host, String path) {
return new UrlMatcher(scheme, host, path);
}

public static UrlMatcher isUrl(String scheme, String host) {
return new UrlMatcher(scheme, host, null);
}

public static UrlMatcher hasQueryParameter(String key, String value) {
return new UrlMatcher(key, value);
}

public static UrlMatcher encodedQueryContains(String text) {
return new UrlMatcher(text);
}
}
Loading
Loading