-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMatchEndpointsTest.java
More file actions
152 lines (130 loc) · 5.71 KB
/
Copy pathMatchEndpointsTest.java
File metadata and controls
152 lines (130 loc) · 5.71 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
package helpers;
import dev.aikido.agent_api.background.Endpoint;
import dev.aikido.agent_api.context.RouteMetadata;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.List;
import static dev.aikido.agent_api.helpers.patterns.MatchEndpoints.matchEndpoints;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class MatchEndpointsTest {
private RouteMetadata sampleRouteMetadata(String url, String method, String route) {
if (route == null) {
route = "/posts/:number";
} if(method == null) {
method = "POST";
} if(url == null) {
url = "http://localhost:4000/posts/3";
}
return new RouteMetadata(route, url, method);
}
@Test
void testInvalidUrlAndNoRoute() {
assertNull(matchEndpoints(new RouteMetadata(null, "abc", null), Collections.emptyList()));
}
@Test
void testNoUrlAndNoRoute() {
assertNull(matchEndpoints(new RouteMetadata(null, null, "GET"), Collections.emptyList()));
}
@Test
void testNoMethod() {
assertNull(matchEndpoints(new RouteMetadata("/posts/:id", "http://localhost:4000/posts/3", null), Collections.emptyList()));
}
@Test
void testItReturnsUndefinedIfNothingFound() {
assertNull(matchEndpoints(sampleRouteMetadata(null, null, null), Collections.emptyList()));
}
@Test
void testItReturnsEndpointBasedOnRoute() {
List<Endpoint> endpoints = List.of(
new Endpoint("POST", "/posts/:number",
10 /*maxRequests*/, 1000 /*windowSizeInMS*/,
List.of(), false, false, true));
assertEquals(endpoints, matchEndpoints(sampleRouteMetadata(null, null, null), endpoints));
}
@Test
void testItReturnsEndpointBasedOnRelativeUrl() {
List<Endpoint> endpoints = List.of(
new Endpoint("POST", "/posts/:number",
10 /*maxRequests*/, 1000 /*windowSizeInMS*/,
List.of(), false, false, true));
assertEquals(endpoints, matchEndpoints(sampleRouteMetadata("/posts/3", "POST", "/posts/:number"), endpoints));
}
@Test
void testItReturnsEndpointBasedOnWildcard() {
List<Endpoint> endpoints = List.of(
new Endpoint("*", "/posts/*",
10 /*maxRequests*/, 1000 /*windowSizeMS*/,
List.of(), false, false, true));
assertEquals(endpoints, matchEndpoints(sampleRouteMetadata(null, null, null), endpoints));
}
@Test
void testItReturnsEndpointBasedOnWildcardWithRelativeUrl() {
List<Endpoint> endpoints = List.of(
new Endpoint("*", "/posts/*",
10 /*maxRequests*/, 1000 /*windowSizeMs*/,
List.of(), false, false, true));
assertEquals(endpoints, matchEndpoints(sampleRouteMetadata(null, null, "/posts/3"), endpoints));
}
@Test
void testItFavorsMoreSpecificWildcard() {
List<Endpoint> endpoints = List.of(
new Endpoint("*", "/posts/*", 10, 1000, List.of(), false, false, true),
new Endpoint("*", "/posts/*/comments/*", 10, 1000, List.of(), false, false, true)
);
List<Endpoint> expected = List.of(
endpoints.get(1),
endpoints.get(0)
);
assertEquals(expected, matchEndpoints(sampleRouteMetadata("http://localhost:4000/posts/3/comments/10", null, null), endpoints));
}
@Test
void testItMatchesWildcardRouteWithSpecificMethod() {
List<Endpoint> endpoints = List.of(
new Endpoint("POST", "/posts/*/comments/*", 10, 1000, List.of(), false, false, true)
);
assertEquals(endpoints, matchEndpoints(sampleRouteMetadata("http://localhost:4000/posts/3/comments/10", null, null), endpoints));
}
@Test
void testItPrefersSpecificRouteOverWildcard() {
List<Endpoint> endpoints = List.of(
new Endpoint("*", "/api/*", 20, 60000, List.of(), false, false, true),
new Endpoint("POST", "/api/coach", 100, 60000, List.of(), false, false, true)
);
List<Endpoint> expected = List.of(
endpoints.get(1),
endpoints.get(0)
);
assertEquals(expected, matchEndpoints(sampleRouteMetadata("http://localhost:4000/api/coach", null, "/api/coach"), endpoints));
}
@Test
void testItPrefersSpecificMethodOverWildcardFirstCase() {
RouteMetadata routeMetadata = sampleRouteMetadata(
"http://localhost:4000/api/test", "POST", "/api/test"
);
List<Endpoint> endpoints = List.of(
new Endpoint("*", "/api/test", 20, 60000, List.of(), false, false, true),
new Endpoint("POST", "/api/test", 100, 60000, List.of(), false, false, true)
);
List<Endpoint> expected = List.of(
endpoints.get(1),
endpoints.get(0)
);
assertEquals(expected, matchEndpoints(routeMetadata, endpoints));
}
@Test
void testItPrefersSpecificMethodOverWildcardSecondCase() {
RouteMetadata routeMetadata = sampleRouteMetadata(
"http://localhost:4000/api/test", "POST", "/api/test"
);
List<Endpoint> endpoints = List.of(
new Endpoint("POST", "/api/test", 100, 60000, List.of(), false, false, true),
new Endpoint("*", "/api/test", 20, 60000, List.of(), false, false, true)
);
List<Endpoint> expected = List.of(
endpoints.get(0),
endpoints.get(1)
);
assertEquals(expected, matchEndpoints(routeMetadata, endpoints));
}
}