77import org .jooby .test .ServerFeature ;
88import org .junit .Test ;
99
10- import com .google .inject .multibindings .Multibinder ;
11-
1210public class ContentNegotiationFeature extends ServerFeature {
1311
1412 @ Path ("/r" )
1513 public static class Resource {
1614
1715 @ Path ("/any" )
1816 @ GET
19- public String any () {
20- return "body" ;
17+ public Result any () {
18+ return Results
19+ .when ("text/html" , () -> View .of ("test" , "this" , "body" ))
20+ .when ("*/*" , () -> "body" );
2121 }
2222
2323 @ Path ("/html" )
2424 @ GET
2525 @ Produces ("text/html" )
26- public String html () {
27- return " body" ;
26+ public View html () {
27+ return View . of ( "test" , "this" , " body") ;
2828 }
2929
3030 @ Path ("/json" )
@@ -38,30 +38,23 @@ public String json() {
3838
3939 {
4040
41- use ((mode , config , binder ) -> {
42- Multibinder .newSetBinder (binder , Body .Formatter .class )
43- .addBinding ().toInstance (BodyConverters .toHtml );
41+ use (BodyConverters .toHtml );
4442
45- Multibinder .newSetBinder (binder , Body .Formatter .class )
46- .addBinding ().toInstance (BodyConverters .toJson );
47- });
43+ use (BodyConverters .toJson );
4844
49- get ("/any" , ( req , resp ) ->
50- resp . format ()
45+ get ("/any" , req ->
46+ Results
5147 .when ("text/html" , () -> View .of ("test" , "this" , "body" ))
52- .when ("*/*" , () -> "body" )
53- .send ());
48+ .when ("*/*" , () -> "body" ));
5449
55- get ("/status" , (req , resp ) ->
56- resp .format ()
57- .when ("*" , () -> Status .NOT_ACCEPTABLE )
58- .send ());
50+ get ("/status" , req ->
51+ Results
52+ .when ("*" , () -> Status .NOT_ACCEPTABLE ));
5953
60- get ("/like" , ( req , resp ) ->
61- resp . format ()
54+ get ("/like" , req ->
55+ Results
6256 .when ("text/html" , () -> View .of ("test" , "this" , "body" ))
63- .when ("application/json" , () -> "body" )
64- .send ());
57+ .when ("application/json" , () -> "body" ));
6558
6659 get ("/html" , (req , resp ) -> resp .send (View .of ("test" , "this" , "body" )))
6760 .produces (MediaType .html );
@@ -85,7 +78,7 @@ public void chromeAccept() throws Exception {
8578 request ()
8679 .get ("/r/any" )
8780 .header ("Accept" , CHROME_ACCEPT )
88- .expect ("<html><body>any : {this=body}</body></html>" );
81+ .expect ("<html><body>test : {this=body}</body></html>" );
8982 }
9083
9184 @ Test
@@ -98,7 +91,7 @@ public void htmlAccept() throws Exception {
9891 request ()
9992 .get ("/r/any" )
10093 .header ("Accept" , "text/html" )
101- .expect ("<html><body>any : {this=body}</body></html>" );
94+ .expect ("<html><body>test : {this=body}</body></html>" );
10295
10396 request ()
10497 .get ("/html" )
@@ -108,7 +101,7 @@ public void htmlAccept() throws Exception {
108101 request ()
109102 .get ("/r/html" )
110103 .header ("Accept" , CHROME_ACCEPT )
111- .expect ("<html><body>html : {this=body}</body></html>" );
104+ .expect ("<html><body>test : {this=body}</body></html>" );
112105
113106 request ()
114107 .get ("/json" )
@@ -216,6 +209,11 @@ public void like() throws Exception {
216209 .get ("/like" )
217210 .header ("Accept" , "application/*+json" )
218211 .expect ("{\" body\" : \" body\" }" );
212+
213+ request ()
214+ .get ("/like" )
215+ .header ("Accept" , "application/xml" )
216+ .expect (406 );
219217 }
220218
221219 @ Test
0 commit comments