Skip to content

Commit 317619b

Browse files
authored
fix: regression by reverting fix that returned 206 when first-pos=length in Range header
1 parent eb238ad commit 317619b

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1616

1717
### Fixed
1818

19-
- #2824, Fix range request with 0 rows and 0 offset return status 416 - @strengthless
19+
- #2824, Fix regression by reverting fix that returned 206 when first position = length in a `Range` header - @laurenceisla, @strengthless
2020
- #3015, Fix unnecessary count() on RPC returning single - @steve-chavez
2121

2222
## [11.2.1] - 2023-10-03

src/PostgREST/RangeQuery.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ rangeStatusHeader topLevelRange queryTotal tableTotal =
104104
rangeStatus :: Integer -> Integer -> Maybe Integer -> Status
105105
rangeStatus _ _ Nothing = status200
106106
rangeStatus lower upper (Just total)
107-
| lower >= total && lower /= upper && lower /= 0 = status416 -- 416 Range Not Satisfiable
108-
| (1 + upper - lower) < total = status206 -- 206 Partial Content
109-
| otherwise = status200 -- 200 OK
107+
| lower > total = status416 -- 416 Range Not Satisfiable
108+
| (1 + upper - lower) < total = status206 -- 206 Partial Content
109+
| otherwise = status200 -- 200 OK
110110

111111
contentRangeH :: (Integral a, Show a) => a -> a -> Maybe a -> Header
112112
contentRangeH lower upper total =

test/spec/Feature/Query/RangeSpec.hs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ spec = do
145145
it "returns whole range with status 200" $
146146
get "/items" `shouldRespondWith` 200
147147

148+
context "count with an empty body" $ do
149+
it "returns empty body with Content-Range */0" $
150+
request methodGet "/items?id=eq.0"
151+
[("Prefer", "count=exact")] ""
152+
`shouldRespondWith`
153+
[json|[]|]
154+
{ matchHeaders = ["Content-Range" <:> "*/0"] }
155+
148156
context "when I don't want the count" $ do
149157
it "returns range Content-Range with /*" $
150158
request methodGet "/menagerie"
@@ -211,11 +219,24 @@ spec = do
211219
, "Content-Range" <:> "2-4/*" ]
212220
}
213221

214-
it "succeeds if offset equals 0 as a no-op" $
215-
get "/items?select=id&offset=0&order=id"
216-
`shouldRespondWith`
217-
[json|[{"id":1},{"id":2},{"id":3},{"id":4},{"id":5},{"id":6},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12},{"id":13},{"id":14},{"id":15}]|]
218-
{ matchHeaders = ["Content-Range" <:> "0-14/*"] }
222+
context "succeeds if offset equals 0 as a no-op" $ do
223+
it "no items" $ do
224+
get "/items?offset=0&id=eq.0"
225+
`shouldRespondWith`
226+
[json|[]|]
227+
{ matchHeaders = ["Content-Range" <:> "*/*"] }
228+
229+
request methodGet "/items?offset=0&id=eq.0"
230+
[("Prefer", "count=exact")] ""
231+
`shouldRespondWith`
232+
[json|[]|]
233+
{ matchHeaders = ["Content-Range" <:> "*/0"] }
234+
235+
it "one or more items" $
236+
get "/items?select=id&offset=0&order=id"
237+
`shouldRespondWith`
238+
[json|[{"id":1},{"id":2},{"id":3},{"id":4},{"id":5},{"id":6},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12},{"id":13},{"id":14},{"id":15}]|]
239+
{ matchHeaders = ["Content-Range" <:> "0-14/*"] }
219240

220241
it "succeeds if offset is negative as a no-op" $
221242
get "/items?select=id&offset=-4&order=id"
@@ -413,15 +434,6 @@ spec = do
413434
matchHeader "Content-Range" "10-14/*"
414435
simpleStatus r `shouldBe` ok200
415436

416-
it "does not throw error when offset is 0 and and total is 0" $
417-
request methodGet "/rpc/getitemrange?min=0&max=0"
418-
(rangeHdrsWithCount $ ByteRangeFromTo 0 1) mempty
419-
`shouldRespondWith`
420-
[json|[]|]
421-
{ matchStatus = 200
422-
, matchHeaders = ["Content-Range" <:> "*/0"]
423-
}
424-
425437
context "of invalid range" $ do
426438
it "fails with 416 for offside range" $
427439
request methodGet "/items"
@@ -462,17 +474,3 @@ spec = do
462474
{ matchStatus = 416
463475
, matchHeaders = ["Content-Range" <:> "*/15"]
464476
}
465-
466-
it "refuses a range with first position the same as number of items" $
467-
request methodGet "/rpc/getitemrange?min=1&max=2"
468-
(rangeHdrsWithCount $ ByteRangeFromTo 1 2) mempty
469-
`shouldRespondWith`
470-
[json| {
471-
"message":"Requested range not satisfiable",
472-
"code":"PGRST103",
473-
"details":"An offset of 1 was requested, but there are only 1 rows.",
474-
"hint":null
475-
}|]
476-
{ matchStatus = 416
477-
, matchHeaders = ["Content-Range" <:> "*/1"]
478-
}

0 commit comments

Comments
 (0)