diff --git a/CHANGELOG.md b/CHANGELOG.md index a058f1df3..ec77faee4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #2899, Fix `application/vnd.pgrst.array` not accepted as a valid mediatype - @taimoorzaeem - #2524, Fix schema cache and configuration reloading with `NOTIFY` not working on Windows - @diogob, @laurenceisla - #2915, Fix duplicate headers in response - @taimoorzaeem + - #2824, Fix range request with first position same as length return status 206 - @taimoorzaeem ## [11.2.0] - 2023-08-10 diff --git a/src/PostgREST/RangeQuery.hs b/src/PostgREST/RangeQuery.hs index a27a1af5c..c042a3735 100644 --- a/src/PostgREST/RangeQuery.hs +++ b/src/PostgREST/RangeQuery.hs @@ -104,9 +104,9 @@ rangeStatusHeader topLevelRange queryTotal tableTotal = rangeStatus :: Integer -> Integer -> Maybe Integer -> Status rangeStatus _ _ Nothing = status200 rangeStatus lower upper (Just total) - | lower > total = status416 -- 416 Range Not Satisfiable - | (1 + upper - lower) < total = status206 -- 206 Partial Content - | otherwise = status200 -- 200 OK + | lower >= total && lower /= upper = status416 -- 416 Range Not Satisfiable + | (1 + upper - lower) < total = status206 -- 206 Partial Content + | otherwise = status200 -- 200 OK contentRangeH :: (Integral a, Show a) => a -> a -> Maybe a -> Header contentRangeH lower upper total = diff --git a/test/spec/Feature/Query/RangeSpec.hs b/test/spec/Feature/Query/RangeSpec.hs index 5fa2dcf7c..8a983fbcb 100644 --- a/test/spec/Feature/Query/RangeSpec.hs +++ b/test/spec/Feature/Query/RangeSpec.hs @@ -453,3 +453,17 @@ spec = do { matchStatus = 416 , matchHeaders = ["Content-Range" <:> "*/15"] } + + it "refuses a range with first position the same as number of items" $ + request methodGet "/rpc/getitemrange?min=1&max=2" + (rangeHdrsWithCount $ ByteRangeFromTo 1 2) mempty + `shouldRespondWith` + [json| { + "message":"Requested range not satisfiable", + "code":"PGRST103", + "details":"An offset of 1 was requested, but there are only 1 rows.", + "hint":null + }|] + { matchStatus = 416 + , matchHeaders = ["Content-Range" <:> "*/1"] + }