File: UnitTests.hs

package info (click to toggle)
haskell-http 1%3A4000.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: haskell: 4,277; makefile: 3
file content (69 lines) | stat: -rw-r--r-- 2,319 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module UnitTests ( unitTests ) where

import Network.HTTP.Base
import Network.HTTP.Headers
import Network.URI

import Data.Maybe ( fromJust )

import Test.Framework ( testGroup )
import Test.Framework.Providers.HUnit
import Test.HUnit

parseIPv4Address :: Assertion
parseIPv4Address =
    assertEqual "127.0.0.1 address is recognised"
         (Just (URIAuthority {user = Nothing, password = Nothing, host = "127.0.0.1", port = Just 5313}))
         (parseURIAuthority (uriToAuthorityString (fromJust (parseURI "http://127.0.0.1:5313/foo"))))


parseIPv6Address :: Assertion
parseIPv6Address =
    assertEqual "::1 address"
         (Just (URIAuthority {user = Nothing, password = Nothing, host = "::1", port = Just 5313}))
         (parseURIAuthority (uriToAuthorityString (fromJust (parseURI "http://[::1]:5313/foo"))))

customHeaderNameComparison :: Assertion
customHeaderNameComparison =
    assertEqual "custom header name" (HdrCustom "foo") (HdrCustom "Foo")

customHeaderLookup :: Assertion
customHeaderLookup =
    let val = "header value"
        h = Header (HdrCustom "foo") val

    in assertEqual "custom header lookup" (Just val)
        (lookupHeader (HdrCustom "Foo") [h])

caseInsensitiveHeaderParse :: Assertion
caseInsensitiveHeaderParse =
    let expected = [ Header HdrContentType "blah"
                   , Header (HdrCustom "X-Unknown") "unused"
                   ]
        input = [ "content-type: blah"
                , "X-Unknown: unused"
                ]

        match actual =
            length actual == length expected &&
            and [ hdrName a == hdrName b && hdrValue a == hdrValue b
                | (a, b) <- zip expected actual
                ]

    in case parseHeaders input of
        Left _ -> assertFailure "Failed header parse"
        Right actual -> assert (match actual)

unitTests =
    [testGroup "Unit tests"
        [ testGroup "URI parsing"
            [ testCase "Parse IPv4 address" parseIPv4Address
            , testCase "Parse IPv6 address" parseIPv6Address
            ]
        ]
    , testGroup "Header tests"
        [ testCase "Custom header name case-insensitive match" customHeaderNameComparison
        , testCase "Custom header lookup" customHeaderLookup
        , testCase "Case-insensitive parsing" caseInsensitiveHeaderParse
        ]
    ]