This repository was archived by the owner on May 11, 2026. It is now read-only.
forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorsTest.java
More file actions
128 lines (106 loc) · 4.51 KB
/
Copy pathCorsTest.java
File metadata and controls
128 lines (106 loc) · 4.51 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
package io.jooby;
import com.google.common.collect.Lists;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.Arrays;
import java.util.function.Consumer;
import static com.typesafe.config.ConfigValueFactory.fromAnyRef;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CorsTest {
@Test
public void defaults() {
cors(cors -> {
assertEquals(true, cors.anyOrigin());
assertEquals(Arrays.asList("*"), cors.getOrigin());
assertEquals(true, cors.getUseCredentials());
assertEquals(true, cors.allowMethod("get"));
assertEquals(true, cors.allowMethod("post"));
assertEquals(Arrays.asList("GET", "POST"), cors.getMethods());
assertEquals(true, cors.allowHeader("X-Requested-With"));
assertEquals(true, cors.allowHeader("Content-Type"));
assertEquals(true, cors.allowHeader("Accept"));
assertEquals(true, cors.allowHeader("Origin"));
assertEquals(true, cors.allowHeader("X-Requested-With", "Content-Type", "Accept", "Origin"));
assertEquals(Arrays.asList("X-Requested-With", "Content-Type", "Accept", "Origin"),
cors.getHeaders());
assertEquals(Duration.ofMinutes(30), cors.getMaxAge());
assertEquals(Arrays.asList(), cors.getExposedHeaders());
assertEquals(false, cors.setUseCredentials(false).getUseCredentials());
});
}
@Test
public void origin() {
cors(baseconf().withValue("origin", fromAnyRef("*")), cors -> {
assertEquals(true, cors.anyOrigin());
assertEquals(true, cors.allowOrigin("http://foo.com"));
});
cors(baseconf().withValue("origin", fromAnyRef("http://*.com")), cors -> {
assertEquals(false, cors.anyOrigin());
assertEquals(true, cors.allowOrigin("http://foo.com"));
assertEquals(true, cors.allowOrigin("http://bar.com"));
});
cors(baseconf().withValue("origin", fromAnyRef("http://foo.com")), cors -> {
assertEquals(false, cors.anyOrigin());
assertEquals(true, cors.allowOrigin("http://foo.com"));
assertEquals(false, cors.allowOrigin("http://bar.com"));
});
}
@Test
public void allowedMethods() {
cors(baseconf().withValue("methods", fromAnyRef("GET")), cors -> {
assertEquals(true, cors.allowMethod("GET"));
assertEquals(true, cors.allowMethod("get"));
assertEquals(false, cors.allowMethod("POST"));
});
cors(baseconf().withValue("methods", fromAnyRef(asList("get", "post"))), cors -> {
assertEquals(true, cors.allowMethod("GET"));
assertEquals(true, cors.allowMethod("get"));
assertEquals(true, cors.allowMethod("POST"));
});
}
@Test
public void requestHeaders() {
cors(baseconf().withValue("headers", fromAnyRef("*")), cors -> {
assertEquals(true, cors.anyHeader());
assertEquals(true, cors.allowHeader("Custom-Header"));
});
cors(baseconf().withValue("headers", fromAnyRef(asList("X-Requested-With", "*"))),
cors -> {
assertEquals(true, cors.allowHeader("X-Requested-With"));
assertEquals(true, cors.anyHeader());
});
cors(
baseconf().withValue("headers",
fromAnyRef(asList("X-Requested-With", "Content-Type", "Accept", "Origin"))),
cors -> {
assertEquals(false, cors.anyHeader());
assertEquals(true, cors.allowHeader("X-Requested-With"));
assertEquals(true, cors.allowHeader("Content-Type"));
assertEquals(true, cors.allowHeader("Accept"));
assertEquals(true, cors.allowHeader("Origin"));
assertEquals(true,
cors.allowHeaders(asList("X-Requested-With", "Content-Type", "Accept", "Origin")));
assertEquals(false,
cors.allowHeaders(asList("X-Requested-With", "Content-Type", "Custom")));
});
}
private void cors(final Config conf, final Consumer<Cors> callback) {
callback.accept(Cors.from(conf));
}
private void cors(final Consumer<Cors> callback) {
callback.accept(new Cors());
}
private Config baseconf() {
Config config = ConfigFactory.empty()
.withValue("credentials", fromAnyRef(true))
.withValue("maxAge", fromAnyRef("30m"))
.withValue("origin", fromAnyRef(Lists.newArrayList()))
.withValue("exposedHeaders", fromAnyRef(Lists.newArrayList("X")))
.withValue("methods", fromAnyRef(Lists.newArrayList()))
.withValue("headers", fromAnyRef(Lists.newArrayList()));
return config;
}
}