Skip to content

Commit a2c4158

Browse files
committed
Don't expand wild cards when matchType is set explicitly
As a consequence of actually testing this we also fix prefix wildcard expansion so it actually works. Oops. Fixes #14
1 parent f872d06 commit a2c4158

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/outbackcdx/WbCdxApi.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static class Query {
5858
Query(Map<String,String> params) {
5959
accessPoint = params.get("accesspoint");
6060
url = params.get("url");
61-
matchType = MatchType.valueOf(params.getOrDefault("matchType", "exact").toUpperCase());
61+
matchType = MatchType.valueOf(params.getOrDefault("matchType", "default").toUpperCase());
6262
sort = SortType.valueOf(params.getOrDefault("sort", "default").toUpperCase());
6363
closest = params.get("closest");
6464

@@ -72,13 +72,15 @@ static class Query {
7272
}
7373

7474
void expandWildcards() {
75-
if (matchType == MatchType.EXACT) {
75+
if (matchType == MatchType.DEFAULT) {
7676
if (url.endsWith("*")) {
7777
matchType = MatchType.PREFIX;
78-
url = url.substring(url.length() - 1);
78+
url = url.substring(0, url.length() - 1);
7979
} else if (url.startsWith("*.")) {
8080
matchType = MatchType.DOMAIN;
8181
url = url.substring(2);
82+
} else {
83+
matchType = MatchType.EXACT;
8284
}
8385
}
8486
}
@@ -232,7 +234,7 @@ public void close() {
232234
}
233235

234236
enum MatchType {
235-
EXACT, PREFIX, HOST, DOMAIN, RANGE;
237+
DEFAULT, EXACT, PREFIX, HOST, DOMAIN, RANGE;
236238
}
237239

238240
enum SortType {

test/outbackcdx/WbCdxApiTest.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,46 @@
22

33
import org.junit.Test;
44

5+
import java.util.HashMap;
6+
import java.util.Map;
7+
58
import static org.junit.Assert.assertEquals;
69

710
public class WbCdxApiTest {
811
@Test
9-
public void hostFromSurt() throws Exception {
12+
public void hostFromSurt() {
1013
assertEquals("org,example", WbCdxApi.hostFromSurt("org,example)/foo/bar"));
1114
assertEquals("org,example", WbCdxApi.hostFromSurt("org,example"));
1215
}
16+
17+
@Test
18+
public void queryDefaultShouldExpandPrefixWildcards() {
19+
Map<String,String> params = new HashMap<>();
20+
params.put("url", "http://example.org/*");
21+
WbCdxApi.Query query = new WbCdxApi.Query(params);
22+
query.expandWildcards();
23+
assertEquals(WbCdxApi.MatchType.PREFIX, query.matchType);
24+
assertEquals("http://example.org/", query.url);
25+
}
26+
27+
@Test
28+
public void queryDefaultShouldExpandDomainWildcards() {
29+
Map<String,String> params = new HashMap<>();
30+
params.put("url", "*.example.org");
31+
WbCdxApi.Query query = new WbCdxApi.Query(params);
32+
query.expandWildcards();
33+
assertEquals(WbCdxApi.MatchType.DOMAIN, query.matchType);
34+
assertEquals("example.org", query.url);
35+
}
36+
37+
@Test
38+
public void queryExactMatchShouldNotExpandWildcards() {
39+
Map<String,String> params = new HashMap<>();
40+
params.put("url", "http://example.org/*");
41+
params.put("matchType", "exact");
42+
WbCdxApi.Query query = new WbCdxApi.Query(params);
43+
query.expandWildcards();
44+
assertEquals(WbCdxApi.MatchType.EXACT, query.matchType);
45+
assertEquals("http://example.org/*", query.url);
46+
}
1347
}

0 commit comments

Comments
 (0)