Skip to content

Commit

Permalink
use websocket text/plain as default content-type fix #504
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Oct 15, 2016
1 parent 6b04308 commit 119438c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.jooby.MediaType;
import org.jooby.test.ServerFeature;
import org.junit.After;
import org.junit.Before;
Expand All @@ -29,7 +30,7 @@ public class OnByteArrayMessageFeature extends ServerFeature {
ws.close();
});
});
});
}).produces(MediaType.octetstream);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.jooby.MediaType;
import org.jooby.test.ServerFeature;
import org.junit.After;
import org.junit.Before;
Expand All @@ -30,8 +31,7 @@ public class OnByteBufferMessageFeature extends ServerFeature {
ws.close();
});
});

});
}).produces(MediaType.octetstream);

}

Expand Down
22 changes: 22 additions & 0 deletions jooby-banner/src/test/java/org/jooby/banner/BannerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jooby.banner;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;

import javax.inject.Provider;

import org.jooby.Env;
import org.jooby.test.MockUnit;
Expand All @@ -14,6 +17,9 @@

import com.github.lalyos.jfiglet.FigletFont;
import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.name.Names;
import com.typesafe.config.Config;

import javaslang.control.Try.CheckedRunnable;
Expand All @@ -34,6 +40,7 @@ public void configure() throws Exception {
new MockUnit(Env.class, Config.class, Binder.class, Logger.class)
.expect(conf("app", "1.0.0"))
.expect(log("app"))
.expect(banner())
.expect(onStart)
.run(unit -> {
new Banner(banner)
Expand All @@ -50,6 +57,7 @@ public void print() throws Exception {
.expect(onStart)
.expect(convertOnLine(banner, "speed"))
.expect(print(banner, "1.0.0"))
.expect(banner())
.run(unit -> {
new Banner(banner)
.configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
Expand All @@ -67,6 +75,7 @@ public void font() throws Exception {
.expect(onStart)
.expect(convertOnLine(banner, "myfont"))
.expect(print(banner, "1.0.0"))
.expect(banner())
.run(unit -> {
new Banner(banner)
.font("myfont")
Expand All @@ -85,6 +94,7 @@ public void defprint() throws Exception {
.expect(onStart)
.expect(convertOnLine(banner, "speed"))
.expect(print(banner, "1.0.0"))
.expect(banner())
.run(unit -> {
new Banner()
.configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
Expand Down Expand Up @@ -121,4 +131,16 @@ private Block conf(final String name, final String v) {
expect(conf.getString("application.version")).andReturn(v);
};
}

@SuppressWarnings("unchecked")
private Block banner() {
return unit -> {

LinkedBindingBuilder<String> lbb = unit.mock(LinkedBindingBuilder.class);
expect(lbb.toProvider(isA(Provider.class))).andReturn(lbb);

Binder binder = unit.get(Binder.class);
expect(binder.bind(Key.get(String.class, Names.named("application.banner")))).andReturn(lbb);
};
}
}
16 changes: 14 additions & 2 deletions jooby/src/main/java/org/jooby/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,8 @@ public Collection map(final Mapper<?> mapper) {
class Definition implements Props<Definition> {

private static final SourceProvider SRC = SourceProvider.DEFAULT_INSTANCE
.plusSkippedClasses(Definition.class, Jooby.class, Collection.class, Group.class);
.plusSkippedClasses(Definition.class, Jooby.class, Collection.class, Group.class,
javaslang.collection.List.class, Routes.class);

/**
* Route's name.
Expand Down Expand Up @@ -1173,6 +1174,15 @@ public boolean glob() {
return cpattern.glob();
}

/**
* Source information (where the route was defined).
*
* @return Source information (where the route was defined).
*/
public Route.Source source() {
return new RouteSourceImpl(declaringClass, line);
}

/**
* Recreate a route path and apply the given variables.
*
Expand Down Expand Up @@ -2229,7 +2239,9 @@ static String normalize(final String path) {
}

/**
* @return Source information.
* Source information (where the route was defined).
*
* @return Source information (where the route was defined).
*/
Route.Source source();

Expand Down
4 changes: 2 additions & 2 deletions jooby/src/main/java/org/jooby/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,13 @@ class Definition {
* Defines the media types that the methods of a resource class or can consumes. Default is:
* {@literal *}/{@literal *}.
*/
private MediaType consumes = MediaType.all;
private MediaType consumes = MediaType.plain;

/**
* Defines the media types that the methods of a resource class or can produces. Default is:
* {@literal *}/{@literal *}.
*/
private MediaType produces = MediaType.all;
private MediaType produces = MediaType.plain;

/** A path pattern. */
private String pattern;
Expand Down
2 changes: 1 addition & 1 deletion jooby/src/main/java/org/jooby/internal/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void connect(final Injector injector, final Request req, final NativeWebS

ws.onTextMessage(message -> Try
.run(() -> messageCallback.invoke(
new MutantImpl(injector.getInstance(ParserExecutor.class),
new MutantImpl(injector.getInstance(ParserExecutor.class), consumes,
new StrParamReferenceImpl("body", "message", ImmutableList.of(message)))))
.onFailure(this::handleErr));

Expand Down
8 changes: 8 additions & 0 deletions jooby/src/test/java/org/jooby/RouteDefinitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ public void attrs() throws Exception {
assertEquals(Route.class, r.attr("type"));
}

@Test
public void src() throws Exception {
Function<String, Route.Definition> route = path -> new Route.Definition("*", path, () -> null);
Route.Definition r = route.apply("/");

assertEquals("org.jooby.RouteDefinitionTest:321", r.source().toString());
}

@Test
public void glob() throws Exception {
Function<String, Route.Definition> route = path -> new Route.Definition("*", path, () -> null);
Expand Down
4 changes: 2 additions & 2 deletions jooby/src/test/java/org/jooby/WebSocketDefinitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public void toStr() {
});

assertEquals("WS /pattern\n" +
" consume: */*\n" +
" produces: */*\n", def.toString());
" consume: text/plain\n" +
" produces: text/plain\n", def.toString());
}

@Test
Expand Down
34 changes: 17 additions & 17 deletions jooby/src/test/java/org/jooby/internal/AppPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void print() {
" GET {complete}/ [*/*] [*/*] (/anonymous)\n" +
" GET / [*/*] [*/*] (/anonymous)\n" +
" GET /home [*/*] [*/*] (/anonymous)\n" +
" WS /ws [*/*] [*/*]\n" +
" WS /ws [text/plain] [text/plain]\n" +
"\n" +
"listening on:\n" +
" http://localhost:8080/", setup);
Expand All @@ -58,11 +58,11 @@ public void printHttps() {
.toString();
assertEquals(" GET / [*/*] [*/*] (/anonymous)\n" +
" GET /home [*/*] [*/*] (/anonymous)\n" +
" WS /ws [*/*] [*/*]\n" +
" WS /ws [text/plain] [text/plain]\n" +
"\n" +
"listening on:" +
"\n http://localhost:8080/" +
"\n https://localhost:8443/", setup);
"listening on:\n" +
" http://localhost:8080/\n" +
" https://localhost:8443/", setup);
}

@Test
Expand All @@ -76,11 +76,11 @@ public void printHttp2() {
.toString();
assertEquals(" GET / [*/*] [*/*] (/anonymous)\n" +
" GET /home [*/*] [*/*] (/anonymous)\n" +
" WS /ws [*/*] [*/*]\n" +
" WS /ws [text/plain] [text/plain]\n" +
"\n" +
"listening on:" +
"\n http://localhost:8080/ +h2" +
"\n https://localhost:8443/ +h2", setup);
"listening on:\n" +
" http://localhost:8080/ +h2\n" +
" https://localhost:8443/ +h2", setup);
}

@Test
Expand All @@ -95,11 +95,11 @@ public void printHttp2Https() {
.toString();
assertEquals(" GET / [*/*] [*/*] (/anonymous)\n" +
" GET /home [*/*] [*/*] (/anonymous)\n" +
" WS /ws [*/*] [*/*]\n" +
" WS /ws [text/plain] [text/plain]\n" +
"\n" +
"listening on:" +
"\n http://localhost:8080/" +
"\n https://localhost:8443/ +h2", setup);
"listening on:\n" +
" http://localhost:8080/\n" +
" https://localhost:8443/ +h2", setup);
}

@Test
Expand All @@ -113,10 +113,10 @@ public void printHttp2ClearText() {
.toString();
assertEquals(" GET / [*/*] [*/*] (/anonymous)\n" +
" GET /home [*/*] [*/*] (/anonymous)\n" +
" WS /ws [*/*] [*/*]\n" +
" WS /ws [text/plain] [text/plain]\n" +
"\n" +
"listening on:" +
"\n http://localhost:8080/ +h2", setup);
"listening on:\n" +
" http://localhost:8080/ +h2", setup);
}

private Config config(final String path) {
Expand All @@ -136,7 +136,7 @@ public void printWithPath() {
.toString();
assertEquals(" GET / [*/*] [*/*] (/anonymous)\n" +
" GET /home [*/*] [*/*] (/anonymous)\n" +
" WS /ws [*/*] [*/*]\n" +
" WS /ws [text/plain] [text/plain]\n" +
"\n" +
"listening on:\n" +
" http://localhost:8080/app", setup);
Expand Down

0 comments on commit 119438c

Please sign in to comment.