packages feed

hs-hath-1.4.0: test/RangeSpec.hs

module RangeSpec ( rangeSpecs ) where

import           Hath.Server.Range ( notModified, requestedPart )

import           Relude

import           Test.Hspec

rangeSpecs :: Spec
rangeSpecs = describe "Conditional requests" $ do
  let etag = "\"0123456789abcdef0123456789abcdef01234567\""

  describe "notModified" $ do
    it "matches the ETag in a list, weak or not, or *" $ do
      notModified etag [ ( "If-None-Match", "\"other\", W/" <> etag ) ] `shouldBe` True
      notModified etag [ ( "If-None-Match", "*" ) ] `shouldBe` True

    it "does not match another ETag, or none"
      $ map (notModified etag) [ [ ( "If-None-Match", "\"other\"" ) ], [] ]
      `shouldBe` [ False, False ]

  describe "requestedPart" $ do
    let part range = requestedPart etag 1000 [ ( "Range", range ) ]

    it "reads the three forms of a byte range" $ do
      part "bytes=100-199" `shouldBe` Just ( 100, 100 )
      part "bytes=900-" `shouldBe` Just ( 900, 100 )
      part "bytes=-10" `shouldBe` Just ( 990, 10 )

    it "cuts a range short at the end of the file" $ part "bytes=990-5000" `shouldBe` Just ( 990, 10 )

    it "sends the whole file for a range that covers it, does not fit, or comes with others"
      $ map part [ "bytes=0-", "bytes=1000-", "bytes=0-1,5-6", "lines=1-2" ]
      `shouldBe` [ Nothing, Nothing, Nothing, Nothing ]

    it "honours If-Range only when it names the ETag" $ do
      requestedPart etag 1000 [ ( "Range", "bytes=0-9" ), ( "If-Range", etag ) ] `shouldBe` Just ( 0, 10 )
      requestedPart etag 1000 [ ( "Range", "bytes=0-9" ), ( "If-Range", "Sat, 01 Jan 2000 00:00:00 GMT" ) ]
        `shouldBe` Nothing