Skip to content

Commit

Permalink
Merge branch 'master' of github.com:aristidb/http-types
Browse files Browse the repository at this point in the history
  • Loading branch information
aristidb committed Aug 14, 2012
2 parents 2e86a6c + 267ac16 commit 058e65e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 40 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
@@ -1,5 +1 @@
language: haskell
install:
- cabal update
- cabal install
- cabal install hspec HUnit
25 changes: 25 additions & 0 deletions Network/HTTP/Types/Status.hs
Expand Up @@ -84,6 +84,11 @@ module Network.HTTP.Types.Status
, gatewayTimeout504
, status505
, httpVersionNotSupported505
, statusIsInformational
, statusIsSuccessful
, statusIsRedirection
, statusIsClientError
, statusIsServerError
)
where

Expand Down Expand Up @@ -435,3 +440,23 @@ status505 = Status 505 "HTTP Version Not Supported"
-- | HTTP Version Not Supported 505
httpVersionNotSupported505 :: Status
httpVersionNotSupported505 = status505

-- | Informational class
statusIsInformational :: Status -> Bool
statusIsInformational (Status {statusCode=code}) = code >= 100 && code < 200

-- | Successful class
statusIsSuccessful :: Status -> Bool
statusIsSuccessful (Status {statusCode=code}) = code >= 200 && code < 300

-- | Redirection class
statusIsRedirection :: Status -> Bool
statusIsRedirection (Status {statusCode=code}) = code >= 300 && code < 400

-- | Client Error class
statusIsClientError :: Status -> Bool
statusIsClientError (Status {statusCode=code}) = code >= 400 && code < 500

-- | Server Error class
statusIsServerError :: Status -> Bool
statusIsServerError (Status {statusCode=code}) = code >= 500 && code < 600
2 changes: 1 addition & 1 deletion http-types.cabal
Expand Up @@ -76,4 +76,4 @@ Library
Test-suite runtests
main-is: runtests.hs
type: exitcode-stdio-1.0
build-depends: text, bytestring, base, blaze-builder, array, case-insensitive, QuickCheck, HUnit, hspec >= 1.2
build-depends: text, bytestring, base, blaze-builder, array, case-insensitive, QuickCheck, hspec >= 1.3
70 changes: 35 additions & 35 deletions runtests.hs
@@ -1,48 +1,48 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import Data.Text (Text)
import Debug.Trace
import Network.HTTP.Types
import Test.Hspec.Core
import Test.Hspec.QuickCheck
import Test.Hspec.HUnit
import Test.QuickCheck (Arbitrary (..))
import Test.HUnit
import Test.Hspec
import Test.QuickCheck
import qualified Blaze.ByteString.Builder as Blaze
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import qualified Data.Text as T

main :: IO ()
main = hspec
[ describe "encode/decode path"
[ it "is identity to encode and then decode"
$ property propEncodeDecodePath
, it "does not escape period and dash" $
Blaze.toByteString (encodePath ["foo-bar.baz"] []) @?= "/foo-bar.baz"
]
, describe "encode/decode query"
[ it "is identity to encode and then decode"
$ property propEncodeDecodeQuery
, it "add ? in front of Query if and only if necessary"
$ property propQueryQuestionMark
]
, describe "encode/decode path segments"
[ it "is identity to encode and then decode"
$ property propEncodeDecodePathSegments
]
, describe "encode ByteRanges"
[ it "first 500 bytes" $ renderByteRanges [ByteRangeFromTo 0 499] @?= "bytes=0-499"
, it "second 500 bytes" $ renderByteRanges [ByteRangeFromTo 500 999] @?= "bytes=500-999"
, it "final 500 bytes" $ renderByteRanges [ByteRangeSuffix 500] @?= "bytes=-500"
, it "final 500 bytes (of 1000, absolute)" $ renderByteRanges [ByteRangeFrom 9500] @?= "bytes=9500-"
, it "first and last bytes only" $ renderByteRanges [ByteRangeFromTo 0 0, ByteRangeSuffix 1] @?= "bytes=0-0,-1"
, it "non-canonical second 500 bytes (1)" $ renderByteRanges [ByteRangeFromTo 500 600, ByteRangeFromTo 601 999] @?= "bytes=500-600,601-999"
, it "non-canonical second 500 bytes (2)" $ renderByteRanges [ByteRangeFromTo 500 700, ByteRangeFromTo 601 999] @?= "bytes=500-700,601-999"
]
]
main = hspec $ do
describe "encode/decode path" $ do
it "is identity to encode and then decode" $
property propEncodeDecodePath
it "does not escape period and dash" $
Blaze.toByteString (encodePath ["foo-bar.baz"] []) `shouldBe` "/foo-bar.baz"

describe "encode/decode query" $ do
it "is identity to encode and then decode" $
property propEncodeDecodeQuery
it "add ? in front of Query if and only if necessary" $
property propQueryQuestionMark

describe "encode/decode path segments" $ do
it "is identity to encode and then decode" $
property propEncodeDecodePathSegments

describe "encode ByteRanges" $ do
it "first 500 bytes" $
renderByteRanges [ByteRangeFromTo 0 499] `shouldBe` "bytes=0-499"
it "second 500 bytes" $
renderByteRanges [ByteRangeFromTo 500 999] `shouldBe` "bytes=500-999"
it "final 500 bytes" $
renderByteRanges [ByteRangeSuffix 500] `shouldBe` "bytes=-500"
it "final 500 bytes (of 1000, absolute)" $
renderByteRanges [ByteRangeFrom 9500] `shouldBe` "bytes=9500-"
it "first and last bytes only" $
renderByteRanges [ByteRangeFromTo 0 0, ByteRangeSuffix 1] `shouldBe` "bytes=0-0,-1"
it "non-canonical second 500 bytes (1)" $
renderByteRanges [ByteRangeFromTo 500 600, ByteRangeFromTo 601 999] `shouldBe` "bytes=500-600,601-999"
it "non-canonical second 500 bytes (2)" $
renderByteRanges [ByteRangeFromTo 500 700, ByteRangeFromTo 601 999] `shouldBe` "bytes=500-700,601-999"

propEncodeDecodePath :: ([Text], Query) -> Bool
propEncodeDecodePath (p', q') =
Expand Down Expand Up @@ -71,7 +71,7 @@ propQueryQuestionMark (useQuestionMark, query) = actual == expected
(False, _) -> False
(True, True) -> False
(True, False) -> True

propEncodeDecodePathSegments :: [Text] -> Bool
propEncodeDecodePathSegments p' =
p == decodePathSegments (Blaze.toByteString $ encodePathSegments p)
Expand Down

0 comments on commit 058e65e

Please sign in to comment.