Skip to content

Commit 72ef64c

Browse files
committed
Cross-origin support fix #156
1 parent 115f015 commit 72ef64c

File tree

18 files changed

+1058
-8
lines changed

18 files changed

+1058
-8
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.jooby;
2+
3+
import java.util.Optional;
4+
5+
import org.jooby.test.ServerFeature;
6+
import org.junit.Test;
7+
8+
public class CorsCustomFeature extends ServerFeature {
9+
10+
{
11+
cors(new Cors().withOrigin("http://foo.com")
12+
.withHeaders("*")
13+
.withoutCreds()
14+
.withExposedHeaders("H1")
15+
.withMaxAge(-1));
16+
17+
get("/greeting", req -> "Hello " + req.param("name").value("World") + "!");
18+
}
19+
20+
@Test
21+
public void corsCustom() throws Exception {
22+
request()
23+
.get("/greeting")
24+
.header("Origin", "http://foo.com")
25+
.expect("Hello World!")
26+
.header("Access-Control-Allow-Origin", "http://foo.com")
27+
.header("Access-Control-Allow-Credentials", Optional.empty())
28+
.header("Access-Control-Expose-Headers", "H1")
29+
.header("Vary", "Origin");
30+
}
31+
32+
@Test
33+
public void skipcors() throws Exception {
34+
request()
35+
.get("/greeting")
36+
.header("Origin", "http://bar.com")
37+
.expect("Hello World!")
38+
.header("Access-Control-Allow-Origin", Optional.empty())
39+
.header("Access-Control-Allow-Credentials", Optional.empty());
40+
}
41+
42+
@Test
43+
public void preflight() throws Exception {
44+
request()
45+
.options("/greeting")
46+
.header("Origin", "http://foo.com")
47+
.header("Access-Control-Request-Headers", "Custom")
48+
.header("Access-Control-Request-Method", "GET")
49+
.expect(200)
50+
.header("Access-Control-Allow-Origin", "http://foo.com")
51+
.header("Access-Control-Allow-Headers", "Custom")
52+
.header("Access-Control-Allow-Methods", "GET,POST")
53+
.header("Access-Control-Allow-Credentials", Optional.empty())
54+
.header("Vary", "Origin");
55+
}
56+
57+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jooby;
2+
3+
import java.util.Optional;
4+
5+
import org.jooby.test.ServerFeature;
6+
import org.junit.Test;
7+
8+
public class CorsDisabledFeature extends ServerFeature {
9+
10+
{
11+
cors(new Cors().withOrigin("http://foo.com")
12+
.withHeaders("*")
13+
.withoutCreds()
14+
.withExposedHeaders("H1")
15+
.withMaxAge(-1)
16+
.disabled());
17+
18+
get("/greeting", req -> "Hello " + req.param("name").value("World") + "!");
19+
}
20+
21+
@Test
22+
public void skipcors() throws Exception {
23+
request()
24+
.get("/greeting")
25+
.header("Origin", "http://bar.com")
26+
.expect("Hello World!")
27+
.header("Access-Control-Allow-Origin", Optional.empty())
28+
.header("Access-Control-Allow-Credentials", Optional.empty());
29+
}
30+
31+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.jooby;
2+
3+
import java.util.Optional;
4+
5+
import org.jooby.test.ServerFeature;
6+
import org.junit.Test;
7+
8+
public class CorsFeature extends ServerFeature {
9+
10+
{
11+
cors();
12+
13+
get("/greeting", req -> "Hello " + req.param("name").value("World") + "!");
14+
}
15+
16+
@Test
17+
public void corsdef() throws Exception {
18+
request()
19+
.get("/greeting")
20+
.header("Origin", "http://foo.com")
21+
.expect("Hello World!")
22+
.header("Access-Control-Allow-Origin", "http://foo.com")
23+
.header("Access-Control-Allow-Credentials", true);
24+
}
25+
26+
@Test
27+
public void skipcors() throws Exception {
28+
request()
29+
.get("/greeting")
30+
.expect("Hello World!")
31+
.header("Access-Control-Allow-Origin", Optional.empty())
32+
.header("Access-Control-Allow-Credentials", Optional.empty());
33+
}
34+
35+
@Test
36+
public void corsChromeLocalFile() throws Exception {
37+
request()
38+
.get("/greeting")
39+
.header("Origin", "null")
40+
.expect("Hello World!")
41+
.header("Access-Control-Allow-Origin", "*");
42+
}
43+
44+
@Test
45+
public void preflight() throws Exception {
46+
request()
47+
.options("/greeting")
48+
.header("Origin", "http://foo.com")
49+
.header("Access-Control-Request-Method", "GET")
50+
.expect("")
51+
.expect(200)
52+
.header("Access-Control-Allow-Origin", "http://foo.com")
53+
.header("Access-Control-Allow-Methods", "GET,POST")
54+
.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Accept,Origin")
55+
.header("Access-Control-Allow-Credentials", true)
56+
.header("Access-Control-Max-Age", 1800);
57+
}
58+
59+
@Test
60+
public void preflightAsSimple() throws Exception {
61+
request()
62+
.options("/greeting")
63+
.header("Origin", "http://foo.com")
64+
.expect(405)
65+
.header("Access-Control-Allow-Origin", Optional.empty())
66+
.header("Access-Control-Allow-Methods", Optional.empty())
67+
.header("Access-Control-Allow-Headers", Optional.empty())
68+
.header("Access-Control-Allow-Credentials", Optional.empty())
69+
.header("Access-Control-Max-Age", Optional.empty());
70+
}
71+
72+
@Test
73+
public void preflightMethodNotAllowed() throws Exception {
74+
request()
75+
.options("/greeting")
76+
.header("Origin", "http://foo.com")
77+
.header("Access-Control-Request-Method", "PUT")
78+
.expect(405)
79+
.header("Access-Control-Allow-Origin", Optional.empty())
80+
.header("Access-Control-Allow-Methods", Optional.empty())
81+
.header("Access-Control-Allow-Headers", Optional.empty())
82+
.header("Access-Control-Allow-Credentials", Optional.empty())
83+
.header("Access-Control-Max-Age", Optional.empty());
84+
}
85+
86+
@Test
87+
public void preflightHeaderNotAllowed() throws Exception {
88+
request()
89+
.options("/greeting")
90+
.header("Origin", "http://foo.com")
91+
.header("Access-Control-Request-Method", "GET")
92+
.header("Access-Control-Request-Headers", "Custom-Header")
93+
.expect(405)
94+
.header("Access-Control-Allow-Origin", (String) null)
95+
.header("Access-Control-Allow-Methods", (String) null)
96+
.header("Access-Control-Allow-Headers", (String) null)
97+
.header("Access-Control-Allow-Credentials", (String) null)
98+
.header("Access-Control-Max-Age", (String) null);
99+
}
100+
101+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class MutantIsSetFeature extends ServerFeature {
7+
8+
{
9+
get("/isset/param", req -> req.param("noset").isSet());
10+
11+
get("/isset/params", req -> req.params().isSet());
12+
13+
post("/isset/body", req -> req.body().isSet());
14+
}
15+
16+
@Test
17+
public void param() throws Exception {
18+
request()
19+
.get("/isset/param")
20+
.expect("false");
21+
}
22+
23+
@Test
24+
public void params() throws Exception {
25+
request()
26+
.get("/isset/params")
27+
.expect("false");
28+
}
29+
30+
@Test
31+
public void body() throws Exception {
32+
request()
33+
.post("/isset/body")
34+
.expect("false");
35+
}
36+
37+
}

coverage-report/src/test/resources/logback.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
<configuration>
33
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
44
<encoder>
5-
<pattern>%-5p [%d{ISO8601}] [%thread] %msg%n</pattern>
5+
<pattern>%-5p [%d{ISO8601}] [%thread] %logger: %msg%n</pattern>
66
</encoder>
77
</appender>
88

9+
<logger name="org.apache" level="INFO" />
10+
911
<root level="INFO">
1012
<appender-ref ref="STDOUT" />
1113
</root>

0 commit comments

Comments
 (0)