From e1e0cea494c3c08a96302dbc4020e0913105d8af Mon Sep 17 00:00:00 2001 From: Kam Ting Hoi <40143583+Strengthless@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:10:17 +0800 Subject: [PATCH] fix: range request with 0 rows and 0 offset return status 416 (#2991) --- CHANGELOG.md | 6 ++++++ src/PostgREST/RangeQuery.hs | 6 +++--- test/spec/Feature/Query/RangeSpec.hs | 9 +++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 524f870cfd..69ea3ba9ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +### Fixed + + - #2824, Fix range request with 0 rows and 0 offset return status 416 - @strengthless + ## [11.2.1] - 2023-10-03 ### Fixed diff --git a/src/PostgREST/RangeQuery.hs b/src/PostgREST/RangeQuery.hs index c042a3735c..215dba62b0 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 && lower /= upper = status416 -- 416 Range Not Satisfiable - | (1 + upper - lower) < total = status206 -- 206 Partial Content - | otherwise = status200 -- 200 OK + | lower >= total && lower /= upper && lower /= 0 = 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 8a983fbcb1..0aaee3a2c7 100644 --- a/test/spec/Feature/Query/RangeSpec.hs +++ b/test/spec/Feature/Query/RangeSpec.hs @@ -413,6 +413,15 @@ spec = do matchHeader "Content-Range" "10-14/*" simpleStatus r `shouldBe` ok200 + it "does not throw error when offset is 0 and and total is 0" $ + request methodGet "/rpc/getitemrange?min=0&max=0" + (rangeHdrsWithCount $ ByteRangeFromTo 0 1) mempty + `shouldRespondWith` + [json|[]|] + { matchStatus = 200 + , matchHeaders = ["Content-Range" <:> "*/0"] + } + context "of invalid range" $ do it "fails with 416 for offside range" $ request methodGet "/items"