Skip to content

Commit

Permalink
akka#19983 Add withoutSizeLimit overrides for Scala API.
Browse files Browse the repository at this point in the history
  • Loading branch information
2m committed Mar 9, 2016
1 parent 85aa219 commit dd14950
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 */
Expand All @@ -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)

Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))

Expand Down

0 comments on commit dd14950

Please sign in to comment.