forked from auth0/auth0-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlMatcher.java
More file actions
155 lines (133 loc) · 5 KB
/
UrlMatcher.java
File metadata and controls
155 lines (133 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.auth0.client;
import okhttp3.HttpUrl;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import java.util.Arrays;
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);
}
}