packages feed

http-directory-0.1.9: test/Spec.hs

import Data.Maybe
import Data.Text (pack)
import Test.Hspec
import Network.HTTP.Directory

main :: IO ()
main = hspec $ parallel spec

spec :: Spec
spec = do
  describe "httpDirectory" $ do
    it "empty httpbin" $ do
      mgr <- httpManager
      fs <- httpDirectory mgr "http://httpbin.org/"
      fs `shouldBe` []

    it "fedora releases" $ do
      fs <- httpDirectory' "https://dl.fedoraproject.org/pub/fedora/linux/releases/"
      null fs `shouldBe` False

    it "my src hackage" $ do
      fs <- httpDirectory' "https://hackage.haskell.org/package/http-directory/src/"
      null fs `shouldBe` False

    it "self hackage" $ do
      fs <- httpDirectory' "https://hackage.haskell.org/package/http-directory"
      null fs `shouldBe` False

    it "self hackage" $ do
      fs <- httpDirectory' "https://mirrors.kernel.org/"
      null fs `shouldBe` False

    it "404" $
      httpDirectory' "https://httpbin.org/404"
      `shouldThrow` anyException

  describe "httpExists" $ do
    it "google" $ do
      mgr <- httpManager
      exists <- httpExists mgr "https://google.com/"
      exists `shouldBe` True

    it "404" $ do
      mgr <- httpManager
      exists <- httpExists mgr "https://httpbin.org/404"
      exists `shouldBe` False

    it "domain" $ do
      mgr <- httpManager
      httpExists mgr "http://nowhereparticular"
      `shouldThrow` anyException

  describe "httpExists'" $ do
    it "google" $ do
      exists <- httpExists' "https://google.com/"
      exists `shouldBe` True

    it "404" $ do
      exists <- httpExists' "https://httpbin.org/404"
      exists `shouldBe` False

    it "domain" $ do
      httpExists' "http://nowhereparticular"
      `shouldThrow` anyException

  describe "httpFileSize" $ do
    it "httpbin/get" $ do
      mgr <- httpManager
      msize <- httpFileSize mgr "https://httpbin.org/get"
      isJust msize `shouldBe` True

    it "httpbin/0B" $ do
      mgr <- httpManager
      msize <- httpFileSize mgr "https://httpbin.org/bytes/0"
      msize `shouldBe` Just 0

    it "httpbin/64B" $ do
      mgr <- httpManager
      msize <- httpFileSize mgr "https://httpbin.org/bytes/64"
      msize `shouldBe` Just 64

    it "cabal" $ do
      mgr <- httpManager
      msize <- httpFileSize mgr "https://raw.githubusercontent.com/juhp/http-directory/master/http-directory.cabal"
      isJust msize `shouldBe` True

  describe "httpFileSize'" $ do
    it "httpbin/get" $ do
      msize <- httpFileSize' "https://httpbin.org/get"
      isJust msize `shouldBe` True

    it "httpbin/0B" $ do
      msize <- httpFileSize' "https://httpbin.org/bytes/0"
      msize `shouldBe` Just 0

    it "httpbin/64B" $ do
      msize <- httpFileSize' "https://httpbin.org/bytes/64"
      msize `shouldBe` Just 64

    it "cabal" $ do
      msize <- httpFileSize' "https://raw.githubusercontent.com/juhp/http-directory/master/http-directory.cabal"
      isJust msize `shouldBe` True

  describe "httpLastModified" $ do
    it "httpbin" $ do
      mgr <- httpManager
      mtime <- httpLastModified mgr "https://haskell.org/"
      isJust mtime `shouldBe` True

    it "httpbin'" $ do
      mtime <- httpLastModified' "https://haskell.org/"
      isJust mtime `shouldBe` True

  -- https://github.com/postmanlabs/httpbin/issues/617
  describe "httpRedirect" $ do
    it "httpbingo" $ do
      mgr <- httpManager
      mredir <- httpRedirect mgr "http://httpbingo.org/relative-redirect/1"
      isJust mredir `shouldBe` True

    it "httpbingo'" $ do
      mredir <- httpRedirect' "http://httpbingo.org/relative-redirect/1"
      isJust mredir `shouldBe` True

    it "3 redirs" $ do
      mgr <- httpManager
      redirs <- httpRedirects mgr "http://httpbingo.org/relative-redirect/2"
      length redirs `shouldBe` 2

  describe "isHttpUrl" $ do
    it "http" $
      all isHttpUrl
        ["http://dl.fedoraproject.org", "https://haskell.org/"]
          `shouldBe` True

    it "non-http" $
      any isHttpUrl
         ["mailto:one@where", "somefile", "an.iso", "package.tgz"]
          `shouldBe` False

  describe "trailingSlash" $ do
    it "add" $
      trailingSlash "http://example.com/dir" `shouldBe` "http://example.com/dir/"

    it "idempotent" $
      trailingSlash "http://example.com/" `shouldBe` "http://example.com/"

    it "remove" $
      noTrailingSlash (pack "abc/") `shouldBe` pack "abc"

    it "remove all" $
      noTrailingSlash (pack "abc/def//") `shouldBe` pack "abc/def"

  describe "+/+" $ do
    it "join" $
      "http://example.com/dir" +/+ "file" `shouldBe` "http://example.com/dir/file"

    it "join/" $
      "http://example.com/dir" +/+ "/file" `shouldBe` "http://example.com/dir/file"

    it "/join" $
      "http://example.com/dir/" +/+ "file" `shouldBe` "http://example.com/dir/file"

    it "/join/" $
      "http://example.com/dir/" +/+ "/file" `shouldBe` "http://example.com/dir/file"

    it "join /" $
      "http://example.com/dir" +/+ "file/" `shouldBe` "http://example.com/dir/file/"

    it "identity" $
      "http://example.com/dir" +/+ "" `shouldBe` "http://example.com/dir"

    it "/identity" $
      "http://example.com/dir/" +/+ "" `shouldBe` "http://example.com/dir/"