Skip to content

Commit d57e05b

Browse files
committed
few more unit tests
1 parent 8e5d5b0 commit d57e05b

4 files changed

Lines changed: 233 additions & 1 deletion

File tree

coverage-report/src/test/java/org/jooby/DownloadFeature.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class DownloadFeature extends ServerFeature {
2020

2121
get("/location", (req, rsp) -> rsp.download("name", req.param("file").value()));
2222

23+
get("/file", (req, rsp) -> rsp.download(DownloadFeature.class.getName().replace('.', '/') + ".json"));
24+
2325
get("/fs", (req, rsp) -> rsp.download(new File(req.param("file").value())));
2426

2527
get("/favicon.ico", (req, rsp) -> rsp.download("favicon.ico",
@@ -105,4 +107,14 @@ public void downladInputStream() throws Exception {
105107
.header("Content-Type", "image/x-icon");
106108
}
107109

110+
@Test
111+
public void download() throws Exception {
112+
request()
113+
.get("/file")
114+
.expect(200)
115+
.header("Content-Disposition", "attachment; filename=\"downloadfeature.json\"; filename*=utf-8''downloadfeature.json")
116+
.header("Content-Length", "3")
117+
.header("Content-Type", "application/json;charset=utf-8");
118+
}
119+
108120
}

jooby/src/test/java/org/jooby/RequestForwardingTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ public void path() throws Exception {
4949
});
5050
}
5151

52+
@Test
53+
public void port() throws Exception {
54+
new MockUnit(Request.class)
55+
.expect(unit -> {
56+
Request req = unit.get(Request.class);
57+
expect(req.port()).andReturn(80);
58+
})
59+
.run(unit -> {
60+
assertEquals(80, new Request.Forwarding(unit.get(Request.class)).port());
61+
});
62+
}
63+
64+
@Test
65+
public void matches() throws Exception {
66+
new MockUnit(Request.class)
67+
.expect(unit -> {
68+
Request req = unit.get(Request.class);
69+
expect(req.matches("/x")).andReturn(true);
70+
})
71+
.run(unit -> {
72+
assertEquals(true, new Request.Forwarding(unit.get(Request.class)).matches("/x"));
73+
});
74+
}
75+
5276
@Test
5377
public void cpath() throws Exception {
5478
new MockUnit(Request.class)

jooby/src/test/java/org/jooby/RouteNamespaceTest.java renamed to jooby/src/test/java/org/jooby/RouteGroupTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.jooby.Route.Group;
1010
import org.junit.Test;
1111

12-
public class RouteNamespaceTest {
12+
public class RouteGroupTest {
1313

1414
@Test
1515
public void all() {
@@ -200,6 +200,15 @@ public void name() {
200200
assertEquals("/x", ns.routes().iterator().next().name());
201201
}
202202

203+
@Test
204+
public void namens() {
205+
Group ns = new Route.Group("/ns", "/prefix");
206+
ns.patch("/p", (req, rsp, chain) -> {
207+
}).name("x");
208+
209+
assertEquals("/prefix/x", ns.routes().iterator().next().name());
210+
}
211+
203212
@Test
204213
public void consumes() {
205214
Group ns = new Route.Group("/ns");
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package org.jooby.internal;
2+
3+
import static org.easymock.EasyMock.expect;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.fail;
6+
7+
import java.io.IOException;
8+
import java.nio.charset.Charset;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.Locale;
11+
import java.util.Optional;
12+
13+
import org.jooby.Err;
14+
import org.jooby.Route;
15+
import org.jooby.spi.NativeRequest;
16+
import org.jooby.test.MockUnit;
17+
import org.jooby.test.MockUnit.Block;
18+
import org.junit.Test;
19+
20+
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableMap;
22+
import com.google.inject.Injector;
23+
24+
public class RequestImplTest {
25+
26+
private Block accept = unit -> {
27+
NativeRequest req = unit.get(NativeRequest.class);
28+
expect(req.header("Accept")).andReturn(Optional.of("*/*"));
29+
};
30+
31+
private Block contentType = unit -> {
32+
NativeRequest req = unit.get(NativeRequest.class);
33+
expect(req.header("Content-Type")).andReturn(Optional.empty());
34+
};
35+
36+
private Block acceptLan = unit -> {
37+
NativeRequest req = unit.get(NativeRequest.class);
38+
expect(req.header("Accept-Language")).andReturn(Optional.empty());
39+
};
40+
41+
private Block locale = unit -> {
42+
Injector injector = unit.get(Injector.class);
43+
expect(injector.getInstance(Locale.class)).andReturn(Locale.getDefault());
44+
};
45+
46+
private Block charset = unit -> {
47+
Injector injector = unit.get(Injector.class);
48+
expect(injector.getInstance(Charset.class)).andReturn(StandardCharsets.UTF_8);
49+
};
50+
51+
@Test
52+
public void defaults() throws Exception {
53+
new MockUnit(Injector.class, NativeRequest.class, Route.class)
54+
.expect(accept)
55+
.expect(locale)
56+
.expect(acceptLan)
57+
.expect(contentType)
58+
.expect(charset)
59+
.run(unit -> {
60+
new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), "/", 8080,
61+
unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of());
62+
});
63+
}
64+
65+
@Test
66+
public void matches() throws Exception {
67+
new MockUnit(Injector.class, NativeRequest.class, Route.class)
68+
.expect(accept)
69+
.expect(locale)
70+
.expect(acceptLan)
71+
.expect(contentType)
72+
.expect(charset)
73+
.expect(unit -> {
74+
Route route = unit.get(Route.class);
75+
expect(route.path()).andReturn("/path/x");
76+
})
77+
78+
.run(unit -> {
79+
RequestImpl req = new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class),
80+
"/", 8080,
81+
unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of());
82+
assertEquals(true, req.matches("/path/**"));
83+
});
84+
}
85+
86+
@Test
87+
public void lang() throws Exception {
88+
new MockUnit(Injector.class, NativeRequest.class, Route.class)
89+
.expect(accept)
90+
.expect(locale)
91+
.expect(unit -> {
92+
NativeRequest req = unit.get(NativeRequest.class);
93+
expect(req.header("Accept-Language")).andReturn(Optional.of("en"));
94+
})
95+
.expect(contentType)
96+
.expect(charset)
97+
.run(unit -> {
98+
RequestImpl req = new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class),
99+
"/", 8080,
100+
unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of());
101+
assertEquals(Locale.ENGLISH, req.locale());
102+
});
103+
}
104+
105+
@Test
106+
public void files() throws Exception {
107+
IOException cause = new IOException("intentional err");
108+
new MockUnit(Injector.class, NativeRequest.class, Route.class)
109+
.expect(accept)
110+
.expect(locale)
111+
.expect(acceptLan)
112+
.expect(contentType)
113+
.expect(charset)
114+
.expect(unit -> {
115+
NativeRequest req = unit.get(NativeRequest.class);
116+
expect(req.files("f")).andThrow(cause);
117+
})
118+
.run(unit -> {
119+
try {
120+
new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), "/", 8080,
121+
unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()).param("f");
122+
fail("expecting error");
123+
} catch (Err ex) {
124+
assertEquals(400, ex.statusCode());
125+
assertEquals(cause, ex.getCause());
126+
}
127+
});
128+
}
129+
130+
@Test
131+
public void paramNames() throws Exception {
132+
IOException cause = new IOException("intentional err");
133+
new MockUnit(Injector.class, NativeRequest.class, Route.class)
134+
.expect(accept)
135+
.expect(locale)
136+
.expect(acceptLan)
137+
.expect(contentType)
138+
.expect(charset)
139+
.expect(unit -> {
140+
Route route = unit.get(Route.class);
141+
expect(route.vars()).andReturn(ImmutableMap.of());
142+
143+
NativeRequest req = unit.get(NativeRequest.class);
144+
expect(req.paramNames()).andThrow(cause);
145+
})
146+
.run(unit -> {
147+
try {
148+
new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), "/", 8080,
149+
unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()).params();
150+
fail("expecting error");
151+
} catch (Err ex) {
152+
assertEquals(400, ex.statusCode());
153+
assertEquals(cause, ex.getCause());
154+
}
155+
});
156+
}
157+
158+
@Test
159+
public void params() throws Exception {
160+
IOException cause = new IOException("intentional err");
161+
new MockUnit(Injector.class, NativeRequest.class, Route.class)
162+
.expect(accept)
163+
.expect(locale)
164+
.expect(acceptLan)
165+
.expect(contentType)
166+
.expect(charset)
167+
.expect(unit -> {
168+
Route route = unit.get(Route.class);
169+
expect(route.vars()).andReturn(ImmutableMap.of());
170+
171+
NativeRequest req = unit.get(NativeRequest.class);
172+
expect(req.files("p")).andReturn(ImmutableList.of());
173+
expect(req.params("p")).andThrow(cause);
174+
})
175+
.run(unit -> {
176+
try {
177+
new RequestImpl(unit.get(Injector.class), unit.get(NativeRequest.class), "/", 8080,
178+
unit.get(Route.class), ImmutableMap.of(), ImmutableMap.of()).param("p");
179+
fail("expecting error");
180+
} catch (Err ex) {
181+
assertEquals(400, ex.statusCode());
182+
assertEquals(cause, ex.getCause());
183+
}
184+
});
185+
}
186+
187+
}

0 commit comments

Comments
 (0)