Skip to content

Commit

Permalink
Merge pull request #213 from softwaremill/allow-one-length-ranges
Browse files Browse the repository at this point in the history
Allow ranges of length one.
  • Loading branch information
adamw authored Aug 29, 2022
2 parents fecff15 + bf365b2 commit 8dc9b68
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions core/src/main/scala/sttp/model/headers/Range.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ case class Range(start: Option[Long], end: Option[Long], unit: String) {
def toContentRange(fileSize: Long, unit: String = ContentRangeUnits.Bytes): ContentRange =
ContentRange(unit, start.zip(end).headOption, Some(fileSize))

val contentLength: Long = end.zip(start).map(r => r._1 - r._2).headOption.getOrElse(0)
val contentLength: Long = end.zip(start).map(r => r._1 - r._2 + 1).headOption.getOrElse(0)

def isValid(contentSize: Long): Boolean =
(start, end) match {
case (Some(_start), Some(_end)) => _start < _end && _end < contentSize
case (Some(_start), Some(_end)) => _start <= _end && _end < contentSize
case (Some(_start), None) => _start < contentSize
case (None, Some(_end)) => _end < contentSize
case _ => false
Expand Down Expand Up @@ -56,7 +56,7 @@ object Range {

private def validateRange(range: Range): Boolean =
(range.start, range.end) match {
case (Some(start), Some(end)) => start < end
case (Some(start), Some(end)) => start <= end
case (Some(_), None) => true
case (None, Some(_)) => true
case _ => false
Expand Down
7 changes: 6 additions & 1 deletion core/src/test/scala/sttp/model/headers/RangeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import sttp.model.ContentRangeUnits

class RangeTest extends AnyFlatSpec with Matchers {

it should "properly parse the Range header of length one" in {
val actual = Range.parse("bytes=10-10")
actual shouldBe Right(List(Range(Some(10), Some(10), ContentRangeUnits.Bytes)))
}

it should "properly parse simplest Range header" in {
val actual = Range.parse("bytes=200-1000")
actual shouldBe Right(List(Range(Some(200), Some(1000), ContentRangeUnits.Bytes)))
Expand Down Expand Up @@ -109,7 +114,7 @@ class RangeTest extends AnyFlatSpec with Matchers {
}

it should "calculate content length" in {
Range(Some(100), Some(200), ContentRangeUnits.Bytes).contentLength shouldBe 100
Range(Some(100), Some(200), ContentRangeUnits.Bytes).contentLength shouldBe 101
}

it should "map RangeValue to content type" in {
Expand Down

0 comments on commit 8dc9b68

Please sign in to comment.