From dd1495037b0bfb2cb8f395a73cec3fc7135a44f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Mickevi=C4=8Dius?= Date: Wed, 9 Mar 2016 14:57:17 +0200 Subject: [PATCH] #19983 Add withoutSizeLimit overrides for Scala API. --- .../akka/http/scaladsl/model/HttpEntity.scala | 17 ++++++++++++- .../http/scaladsl/model/HttpEntitySpec.scala | 24 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala b/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala index 8ade900787..e78fb19492 100644 --- a/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala +++ b/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala @@ -126,6 +126,11 @@ sealed trait RequestEntity extends HttpEntity with jm.RequestEntity with Respons */ def withSizeLimit(maxBytes: Long): RequestEntity + /** + * See [[HttpEntity#withoutSizeLimit]]. + */ + def withoutSizeLimit: RequestEntity + def transformDataBytes(transformer: Flow[ByteString, ByteString, Any]): RequestEntity } @@ -142,6 +147,11 @@ sealed trait ResponseEntity extends HttpEntity with jm.ResponseEntity { */ def withSizeLimit(maxBytes: Long): ResponseEntity + /** + * See [[HttpEntity#withoutSizeLimit]] + */ + def withoutSizeLimit: ResponseEntity + def transformDataBytes(transformer: Flow[ByteString, ByteString, Any]): ResponseEntity } /* An entity that can be used for requests, responses, and body parts */ @@ -153,6 +163,11 @@ sealed trait UniversalEntity extends jm.UniversalEntity with MessageEntity with */ def withSizeLimit(maxBytes: Long): UniversalEntity + /** + * See [[HttpEntity#withoutSizeLimit]] + */ + def withoutSizeLimit: UniversalEntity + def contentLength: Long def contentLengthOption: Option[Long] = Some(contentLength) @@ -228,7 +243,7 @@ object HttpEntity { if (contentType == this.contentType) this else copy(contentType = contentType) override def withSizeLimit(maxBytes: Long): UniversalEntity = - if (data.length <= maxBytes) this + if (data.length <= maxBytes || isKnownEmpty) this else HttpEntity.Default(contentType, data.length, limitableByteSource(Source.single(data))) withSizeLimit maxBytes override def withoutSizeLimit: UniversalEntity = diff --git a/akka-http-core/src/test/scala/akka/http/scaladsl/model/HttpEntitySpec.scala b/akka-http-core/src/test/scala/akka/http/scaladsl/model/HttpEntitySpec.scala index ed39994384..c6838706a2 100755 --- a/akka-http-core/src/test/scala/akka/http/scaladsl/model/HttpEntitySpec.scala +++ b/akka-http-core/src/test/scala/akka/http/scaladsl/model/HttpEntitySpec.scala @@ -149,6 +149,28 @@ class HttpEntitySpec extends FreeSpec with MustMatchers with BeforeAndAfterAll { entity.toString must include(entity.productPrefix) } } + "support withoutSizeLimit" - { + "Strict" in { + HttpEntity.Empty.withoutSizeLimit + withReturnType[UniversalEntity](Strict(tpe, abc).withoutSizeLimit) + withReturnType[RequestEntity](Strict(tpe, abc).asInstanceOf[RequestEntity].withoutSizeLimit) + withReturnType[ResponseEntity](Strict(tpe, abc).asInstanceOf[ResponseEntity].withoutSizeLimit) + } + "Default" in { + withReturnType[Default](Default(tpe, 11, source(abc, de, fgh, ijk)).withoutSizeLimit) + withReturnType[RequestEntity](Default(tpe, 11, source(abc, de, fgh, ijk)).asInstanceOf[RequestEntity].withoutSizeLimit) + withReturnType[ResponseEntity](Default(tpe, 11, source(abc, de, fgh, ijk)).asInstanceOf[ResponseEntity].withoutSizeLimit) + } + "CloseDelimited" in { + withReturnType[CloseDelimited](CloseDelimited(tpe, source(abc, de, fgh, ijk)).withoutSizeLimit) + withReturnType[ResponseEntity](CloseDelimited(tpe, source(abc, de, fgh, ijk)).asInstanceOf[ResponseEntity].withoutSizeLimit) + } + "Chunked" in { + withReturnType[Chunked](Chunked(tpe, source(Chunk(abc), Chunk(fgh), Chunk(ijk), LastChunk)).withoutSizeLimit) + withReturnType[RequestEntity](Chunked(tpe, source(Chunk(abc), Chunk(fgh), Chunk(ijk), LastChunk)).asInstanceOf[RequestEntity].withoutSizeLimit) + withReturnType[ResponseEntity](Chunked(tpe, source(Chunk(abc), Chunk(fgh), Chunk(ijk), LastChunk)).asInstanceOf[ResponseEntity].withoutSizeLimit) + } + } } def source[T](elems: T*) = Source(elems.toList) @@ -159,6 +181,8 @@ class HttpEntitySpec extends FreeSpec with MustMatchers with BeforeAndAfterAll { Await.result(future, 250.millis) } + def withReturnType[T](expr: T) = expr + def strictifyTo(strict: Strict): Matcher[HttpEntity] = equal(strict).matcher[Strict].compose(x ⇒ Await.result(x.toStrict(250.millis), 250.millis))