Skip to content

Commit f3ddef7

Browse files
committed
Move test coverage to 95%
* fix some minor bugs * small refactors * finish session API * reduce number of threads while running tests
1 parent 257585e commit f3ddef7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1634
-250
lines changed

jooby/src/main/java/org/jooby/Body.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public interface Parser {
6565
*/
6666
List<MediaType> types();
6767

68+
/**
69+
* Test if the HTTP request body or parameter can be converted to the given type.
70+
*
71+
* @param type The candidate Type.
72+
* @return True if the converter can read the HTTP request body.
73+
*/
74+
default boolean canParse(@Nonnull final Class<?> type) {
75+
return canParse(TypeLiteral.get(type));
76+
}
77+
6878
/**
6979
* Test if the HTTP request body or parameter can be converted to the given type.
7080
*
@@ -73,6 +83,31 @@ public interface Parser {
7383
*/
7484
boolean canParse(@Nonnull TypeLiteral<?> type);
7585

86+
/**
87+
* Attempt to read a message from HTTP request body.
88+
* <p>
89+
* For text format (json, yaml, xml, etc.) a converter usually call to
90+
* {@link Body.Reader#text(Body.Reader.Text)} in order to apply correct charset and close
91+
* resources.
92+
* </p>
93+
*
94+
* <p>
95+
* For binary format a converter usually call to {@link Body.Reader#bytes(Body.Reader.Bytes)}
96+
* in order to close resources.
97+
* </p>
98+
*
99+
* @param type A type of message.
100+
* @param reader A read context.
101+
* @param <T> Target type.
102+
* @return A body message.
103+
* @throws Exception If read operation fail.
104+
*/
105+
@Nonnull
106+
default <T> T parse(@Nonnull final Class<T> type, @Nonnull final Body.Reader reader)
107+
throws Exception {
108+
return parse(TypeLiteral.get(type), reader);
109+
}
110+
76111
/**
77112
* Attempt to read a message from HTTP request body.
78113
* <p>

jooby/src/main/java/org/jooby/Cookie.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.crypto.Mac;
2828
import javax.crypto.spec.SecretKeySpec;
2929

30+
import com.google.common.base.MoreObjects;
3031
import com.google.common.io.BaseEncoding;
3132

3233
/**
@@ -188,6 +189,17 @@ public boolean httpOnly() {
188189
return cookie.httpOnly().orElse(Boolean.FALSE);
189190
}
190191

192+
@Override
193+
public String toString() {
194+
return MoreObjects.toStringHelper(this)
195+
.add("name", name())
196+
.add("value", value())
197+
.add("domain", domain())
198+
.add("path", path())
199+
.add("maxAge", maxAge())
200+
.add("secure", secure())
201+
.toString();
202+
}
191203
};
192204
}
193205

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
import org.jooby.internal.AssetFormatter;
4646
import org.jooby.internal.AssetHandler;
47-
import org.jooby.internal.FallbackBodyConverter;
47+
import org.jooby.internal.BuiltinBodyConverter;
4848
import org.jooby.internal.RoutePattern;
4949
import org.jooby.internal.Server;
5050
import org.jooby.internal.TypeConverters;
@@ -1916,11 +1916,11 @@ public void configure(final Binder binder) {
19161916
// Singleton routes
19171917
singletonRoutes.forEach(routeClass -> binder.bind(routeClass).in(Scopes.SINGLETON));
19181918

1919-
formatterBinder.addBinding().toInstance(FallbackBodyConverter.formatReader);
1920-
formatterBinder.addBinding().toInstance(FallbackBodyConverter.formatStream);
1921-
formatterBinder.addBinding().toInstance(FallbackBodyConverter.formatString);
1919+
formatterBinder.addBinding().toInstance(BuiltinBodyConverter.formatReader);
1920+
formatterBinder.addBinding().toInstance(BuiltinBodyConverter.formatStream);
1921+
formatterBinder.addBinding().toInstance(BuiltinBodyConverter.formatAny);
19221922

1923-
parserBinder.addBinding().toInstance(FallbackBodyConverter.parseString);
1923+
parserBinder.addBinding().toInstance(BuiltinBodyConverter.parseString);
19241924

19251925
// err
19261926
if (err == null) {

jooby/src/main/java/org/jooby/MediaType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public static Matcher matcher(final @Nonnull List<MediaType> acceptable) {
562562
*/
563563
public static @Nonnull Optional<MediaType> byFile(@Nonnull final File file) {
564564
requireNonNull(file, "A file is required.");
565-
return byPath(file.getAbsolutePath());
565+
return byPath(file.getName());
566566
}
567567

568568
/**

jooby/src/main/java/org/jooby/Request.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ default Verb verb() {
291291
}
292292

293293
/**
294-
* @return The value of the <code>Content-Type</code> header. Default is: {@literal*}/{@literal*}.
294+
* @return The <code>Content-Type</code> header. Default is: {@literal*}/{@literal*}.
295295
*/
296296
@Nonnull
297297
MediaType type();

jooby/src/main/java/org/jooby/Response.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,16 @@ default void download(final @Nonnull String location) throws Exception {
392392
* @throws Exception If something goes wrong.
393393
*/
394394
default void download(final String filename, final @Nonnull String location) throws Exception {
395-
MediaType type = MediaType.byPath(filename).orElse(MediaType.octetstream);
396395
InputStream stream = getClass()
397396
.getResourceAsStream(location.startsWith("/") ? location : "/" + location);
398397
if (stream == null) {
399398
throw new FileNotFoundException(location);
400399
}
400+
// handle type
401+
MediaType type = MediaType.byPath(filename).orElse(MediaType.byPath(location)
402+
.orElse(MediaType.octetstream));
403+
type(type().orElseGet(() -> type));
404+
401405
if (type.isText()) {
402406
download(filename, new InputStreamReader(stream, charset()));
403407
} else {

jooby/src/main/java/org/jooby/Route.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public Definition(final @Nonnull String verb, final @Nonnull String pattern,
439439
RouteMatcher matcher = compiledPattern.matcher(verb.name() + path);
440440
if (matcher.matches()) {
441441
List<MediaType> result = MediaType.matcher(accept).filter(this.produces);
442-
if (canConsume(contentType) && result.size() > 0) {
442+
if (result.size() > 0 && canConsume(contentType)) {
443443
// keep accept when */*
444444
List<MediaType> produces = result.size() == 1 && result.get(0).name().equals("*/*")
445445
? accept : this.produces;

jooby/src/main/java/org/jooby/Session.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public void save(final Session session, final SaveReason reason) {
254254
}
255255

256256
@Override
257-
public Session get(final String id) {
257+
public Session get(final Session.Builder builder) {
258258
return null;
259259
}
260260

@@ -266,12 +266,12 @@ public void delete(final String id) {
266266
/**
267267
* Get a session by ID (if any).
268268
*
269-
* @param id Session ID.
269+
* @param builder A session builder.
270270
* @return A session or <code>null</code>.
271271
* @throws Exception If something goes wrong.
272272
*/
273273
@Nullable
274-
Session get(@Nonnull String id) throws Exception;
274+
Session get(@Nonnull Session.Builder builder) throws Exception;
275275

276276
/**
277277
* Save/persist a session.
@@ -301,6 +301,21 @@ default String generateID(final long seed) {
301301
}
302302
}
303303

304+
interface Builder {
305+
306+
String sessionId();
307+
308+
Builder set(final String name, final Object value);
309+
310+
Builder set(final Map<String, Object> attributes);
311+
312+
Builder createdAt(long createdAt);
313+
314+
Builder accessedAt(long accessedAt);
315+
316+
Session build();
317+
}
318+
304319
/** Logger logs, man. */
305320
Logger log = LoggerFactory.getLogger(Session.class);
306321

jooby/src/main/java/org/jooby/View.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public interface Engine extends Body.Formatter {
4545
List<MediaType> HTML = ImmutableList.of(MediaType.html);
4646

4747
default String name() {
48-
return getClass().getSimpleName().toLowerCase();
48+
String name = getClass().getName();
49+
return name.substring(Math.max(-1, name.lastIndexOf('.')) + 1) .toLowerCase();
4950
}
5051

5152
@Override

jooby/src/main/java/org/jooby/internal/BodyConverterSelector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public List<MediaType> viewableTypes() {
8888
return viewableTypes;
8989
}
9090

91-
public Optional<Body.Parser> forRead(final TypeLiteral<?> type,
91+
public Optional<Body.Parser> parser(final TypeLiteral<?> type,
9292
final Iterable<MediaType> types) {
9393
requireNonNull(type, "Type literal is required.");
9494
requireNonNull(types, "Types are required.");
@@ -110,7 +110,7 @@ public Optional<Body.Parser> forRead(final TypeLiteral<?> type,
110110
return Optional.empty();
111111
}
112112

113-
public Optional<Body.Formatter> forWrite(final Object message,
113+
public Optional<Body.Formatter> formatter(final Object message,
114114
final Iterable<MediaType> types) {
115115
requireNonNull(message, "A message is required.");
116116
requireNonNull(types, "Types are required.");

0 commit comments

Comments
 (0)