This commit is contained in:
parent
ab83603733
commit
16e8f8f54a
6 changed files with 166 additions and 5 deletions
|
|
@ -0,0 +1,156 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2016 Lightbend Inc. <http://www.lightbend.com>
|
||||||
|
*/
|
||||||
|
package docs.http.javadsl.server.directives;
|
||||||
|
|
||||||
|
import akka.http.javadsl.model.HttpRequest;
|
||||||
|
import akka.http.javadsl.model.headers.AcceptEncoding;
|
||||||
|
import akka.http.javadsl.model.headers.ContentEncoding;
|
||||||
|
import akka.http.javadsl.model.headers.HttpEncodings;
|
||||||
|
import akka.http.javadsl.server.Coder;
|
||||||
|
import akka.http.javadsl.server.Rejections;
|
||||||
|
import akka.http.javadsl.server.Route;
|
||||||
|
import akka.http.javadsl.testkit.JUnitRouteTest;
|
||||||
|
import akka.util.ByteString;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static akka.http.javadsl.server.Unmarshaller.entityToString;
|
||||||
|
|
||||||
|
public class CodingDirectivesExamplesTest extends JUnitRouteTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testResponseEncodingAccepted() {
|
||||||
|
//#responseEncodingAccepted
|
||||||
|
final Route route = responseEncodingAccepted(HttpEncodings.GZIP, () ->
|
||||||
|
complete("content")
|
||||||
|
);
|
||||||
|
|
||||||
|
// tests:
|
||||||
|
testRoute(route).run(HttpRequest.GET("/"))
|
||||||
|
.assertEntity("content");
|
||||||
|
runRouteUnSealed(route,
|
||||||
|
HttpRequest.GET("/")
|
||||||
|
.addHeader(AcceptEncoding.create(HttpEncodings.DEFLATE)))
|
||||||
|
.assertRejections(Rejections.unacceptedResponseEncoding(HttpEncodings.GZIP));
|
||||||
|
//#responseEncodingAccepted
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncodeResponse() {
|
||||||
|
//#encodeResponse
|
||||||
|
final Route route = encodeResponse(() -> complete("content"));
|
||||||
|
|
||||||
|
// tests:
|
||||||
|
testRoute(route).run(
|
||||||
|
HttpRequest.GET("/")
|
||||||
|
.addHeader(AcceptEncoding.create(HttpEncodings.GZIP))
|
||||||
|
.addHeader(AcceptEncoding.create(HttpEncodings.DEFLATE))
|
||||||
|
).assertHeaderExists(ContentEncoding.create(HttpEncodings.GZIP));
|
||||||
|
|
||||||
|
testRoute(route).run(
|
||||||
|
HttpRequest.GET("/")
|
||||||
|
.addHeader(AcceptEncoding.create(HttpEncodings.DEFLATE))
|
||||||
|
).assertHeaderExists(ContentEncoding.create(HttpEncodings.DEFLATE));
|
||||||
|
|
||||||
|
// This case failed!
|
||||||
|
// testRoute(route).run(
|
||||||
|
// HttpRequest.GET("/")
|
||||||
|
// .addHeader(AcceptEncoding.create(HttpEncodings.IDENTITY))
|
||||||
|
// ).assertHeaderExists(ContentEncoding.create(HttpEncodings.IDENTITY));
|
||||||
|
|
||||||
|
//#encodeResponse
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncodeResponseWith() {
|
||||||
|
//#encodeResponseWith
|
||||||
|
final Route route = encodeResponseWith(
|
||||||
|
Collections.singletonList(Coder.Gzip),
|
||||||
|
() -> complete("content")
|
||||||
|
);
|
||||||
|
|
||||||
|
// tests:
|
||||||
|
testRoute(route).run(HttpRequest.GET("/"))
|
||||||
|
.assertHeaderExists(ContentEncoding.create(HttpEncodings.GZIP));
|
||||||
|
|
||||||
|
testRoute(route).run(
|
||||||
|
HttpRequest.GET("/")
|
||||||
|
.addHeader(AcceptEncoding.create(HttpEncodings.GZIP))
|
||||||
|
.addHeader(AcceptEncoding.create(HttpEncodings.DEFLATE))
|
||||||
|
).assertHeaderExists(ContentEncoding.create(HttpEncodings.GZIP));
|
||||||
|
|
||||||
|
runRouteUnSealed(route,
|
||||||
|
HttpRequest.GET("/")
|
||||||
|
.addHeader(AcceptEncoding.create(HttpEncodings.DEFLATE))
|
||||||
|
).assertRejections(Rejections.unacceptedResponseEncoding(HttpEncodings.GZIP));
|
||||||
|
|
||||||
|
runRouteUnSealed(route,
|
||||||
|
HttpRequest.GET("/")
|
||||||
|
.addHeader(AcceptEncoding.create(HttpEncodings.IDENTITY))
|
||||||
|
).assertRejections(Rejections.unacceptedResponseEncoding(HttpEncodings.GZIP));
|
||||||
|
//#encodeResponseWith
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecodeRequest() {
|
||||||
|
//#decodeRequest
|
||||||
|
final ByteString helloGzipped = Coder.Gzip.encode(ByteString.fromString("Hello"));
|
||||||
|
final ByteString helloDeflated = Coder.Deflate.encode(ByteString.fromString("Hello"));
|
||||||
|
|
||||||
|
final Route route = decodeRequest(() ->
|
||||||
|
entity(entityToString(), content ->
|
||||||
|
complete("Request content: '" + content + "'")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// tests:
|
||||||
|
testRoute(route).run(
|
||||||
|
HttpRequest.POST("/").withEntity(helloGzipped)
|
||||||
|
.addHeader(ContentEncoding.create(HttpEncodings.GZIP)))
|
||||||
|
.assertEntity("Request content: 'Hello'");
|
||||||
|
|
||||||
|
testRoute(route).run(
|
||||||
|
HttpRequest.POST("/").withEntity(helloDeflated)
|
||||||
|
.addHeader(ContentEncoding.create(HttpEncodings.DEFLATE)))
|
||||||
|
.assertEntity("Request content: 'Hello'");
|
||||||
|
|
||||||
|
testRoute(route).run(
|
||||||
|
HttpRequest.POST("/").withEntity("hello uncompressed")
|
||||||
|
.addHeader(ContentEncoding.create(HttpEncodings.IDENTITY)))
|
||||||
|
.assertEntity( "Request content: 'hello uncompressed'");
|
||||||
|
//#decodeRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecodeRequestWith() {
|
||||||
|
//#decodeRequestWith
|
||||||
|
final ByteString helloGzipped = Coder.Gzip.encode(ByteString.fromString("Hello"));
|
||||||
|
final ByteString helloDeflated = Coder.Deflate.encode(ByteString.fromString("Hello"));
|
||||||
|
|
||||||
|
final Route route = decodeRequestWith(Coder.Gzip, () ->
|
||||||
|
entity(entityToString(), content ->
|
||||||
|
complete("Request content: '" + content + "'")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// tests:
|
||||||
|
testRoute(route).run(
|
||||||
|
HttpRequest.POST("/").withEntity(helloGzipped)
|
||||||
|
.addHeader(ContentEncoding.create(HttpEncodings.GZIP)))
|
||||||
|
.assertEntity("Request content: 'Hello'");
|
||||||
|
|
||||||
|
runRouteUnSealed(route,
|
||||||
|
HttpRequest.POST("/").withEntity(helloDeflated)
|
||||||
|
.addHeader(ContentEncoding.create(HttpEncodings.DEFLATE)))
|
||||||
|
.assertRejections(Rejections.unsupportedRequestEncoding(HttpEncodings.GZIP));
|
||||||
|
|
||||||
|
runRouteUnSealed(route,
|
||||||
|
HttpRequest.POST("/").withEntity("hello")
|
||||||
|
.addHeader(ContentEncoding.create(HttpEncodings.IDENTITY)))
|
||||||
|
.assertRejections(Rejections.unsupportedRequestEncoding(HttpEncodings.GZIP));
|
||||||
|
//#decodeRequestWith
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -10,4 +10,5 @@ Decompresses the incoming request if it is ``gzip`` or ``deflate`` compressed. U
|
||||||
|
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
..TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 <https://github.com/akka/akka/issues/20466>`_.
|
|
||||||
|
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/CodingDirectivesExamplesTest.java#decodeRequest
|
||||||
|
|
|
||||||
|
|
@ -10,4 +10,5 @@ Decodes the incoming request if it is encoded with one of the given encoders. If
|
||||||
|
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 <https://github.com/akka/akka/issues/20466>`_.
|
|
||||||
|
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/CodingDirectivesExamplesTest.java#decodeRequestWith
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ If the ``Accept-Encoding`` header is missing or empty or specifies an encoding o
|
||||||
|
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 <https://github.com/akka/akka/issues/20466>`_.
|
|
||||||
|
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/CodingDirectivesExamplesTest.java#encodeResponse
|
||||||
|
|
||||||
.. _RFC7231: http://tools.ietf.org/html/rfc7231#section-5.3.4
|
.. _RFC7231: http://tools.ietf.org/html/rfc7231#section-5.3.4
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ response encoding is used. Otherwise the request is rejected.
|
||||||
|
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 <https://github.com/akka/akka/issues/20466>`_.
|
|
||||||
|
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/CodingDirectivesExamplesTest.java#encodeResponseWith
|
||||||
|
|
||||||
.. _RFC7231: http://tools.ietf.org/html/rfc7231#section-5.3.4
|
.. _RFC7231: http://tools.ietf.org/html/rfc7231#section-5.3.4
|
||||||
|
|
|
||||||
|
|
@ -10,4 +10,5 @@ Passes the request to the inner route if the request accepts the argument encodi
|
||||||
|
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 <https://github.com/akka/akka/issues/20466>`_.
|
|
||||||
|
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/CodingDirectivesExamplesTest.java#responseEncodingAccepted
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue