File: UnitTests.hs

package info (click to toggle)
haskell-wai-cors 0.2.7-4
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 136 kB
  • sloc: haskell: 479; javascript: 35; makefile: 6
file content (144 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (4)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UnicodeSyntax #-}

-- |
-- Module: UnitTests
-- Description: Unit Tests for wai-cors
-- Copyright: © 2015-2018 Lars Kuhtz <lakuhtz@gmail.com>.
-- License: MIT
-- Maintainer: Lars Kuhtz <lakuhtz@gmail.com>
-- Stability: experimental
--
module Main
( main

, post
, patch
, delete
, put
, get
, options
, head
) where

import Network.Wai.Middleware.Cors
import qualified Network.HTTP.Types as HTTP
import qualified Network.Wai as WAI
import Network.Wai.Test

import Prelude hiding (head)

import Test.Tasty
import Test.Tasty.HUnit

-- -------------------------------------------------------------------------- --
-- Unit tests

main ∷ IO ()
main = defaultMain tests

tests ∷ TestTree
tests = testGroup "unit tests"
    [ testCase "Origins any" test_originsAny
    , testCase "Origins null" test_originsNull
    , testCase "Any Origin" test_anyOrigin
    , testCase "Require Origin" test_requireOrigin
    , testCase "Missing Require Origin" test_missingRequireOrigin
    , testCase "Vary Origin Header" test_varyOriginHeader
    , testCase "Vary Origin No Header" test_varyOriginNoHeader
    ]

test_originsAny ∷ Assertion
test_originsAny = corsSession policy $ do
    resp ← request get
    assertHeader "Access-Control-Allow-Origin" "*" resp
    assertStatus 200 resp
  where
    policy = simpleCorsResourcePolicy
        { corsOrigins = Nothing
        }

test_originsNull ∷ Assertion
test_originsNull = corsSession policy $ do
    resp ← request get
    assertHeader "Access-Control-Allow-Origin" "null" resp
    assertStatus 200 resp
  where
    policy = simpleCorsResourcePolicy
        { corsOrigins = Just (["null"], False)
        }

test_missingRequireOrigin ∷ Assertion
test_missingRequireOrigin = corsSession policy $ do
    resp ← request $ defaultRequest { WAI.requestMethod = "GET" }
    assertStatus 400 resp
  where
    policy = simpleCorsResourcePolicy
        { corsRequireOrigin = True
        }

test_requireOrigin ∷ Assertion
test_requireOrigin = corsSession policy $ do
    resp ← request get
    assertStatus 200 resp
  where
    policy = simpleCorsResourcePolicy
        { corsRequireOrigin = True
        }

test_anyOrigin ∷ Assertion
test_anyOrigin = corsSession policy $ do
    resp ← request get
    assertStatus 200 resp
  where
    policy = simpleCorsResourcePolicy
        { corsOrigins = Nothing
        }

test_varyOriginHeader ∷ Assertion
test_varyOriginHeader = corsSession policy $ do
    resp ← request put
    assertStatus 200 resp
    assertHeader "Vary" "Origin" resp
  where
    policy = simpleCorsResourcePolicy
        { corsOrigins = Just (["null"], False)
        , corsVaryOrigin = True
        }

test_varyOriginNoHeader ∷ Assertion
test_varyOriginNoHeader = corsSession policy $ do
    resp ← request put
    assertStatus 200 resp
    assertNoHeader "Vary" resp
  where
    policy = simpleCorsResourcePolicy
        { corsOrigins = Nothing
        , corsVaryOrigin = True
        }

-- -------------------------------------------------------------------------- --
-- Test Requests

corsSession ∷ CorsResourcePolicy → Session () → Assertion
corsSession policy session = runSession session . cors (const $ Just policy) $ app

corsRequest ∷ WAI.Request
corsRequest = WAI.defaultRequest
    { WAI.pathInfo = ["cors"]
    , WAI.requestHeaders = [("Origin", "null")]
    }

get, post, put, patch, delete, head, options ∷ WAI.Request
get = corsRequest { WAI.requestMethod = "GET" }
post = corsRequest { WAI.requestMethod = "POST" }
put = corsRequest { WAI.requestMethod = "PUT" }
patch = corsRequest { WAI.requestMethod = "PATCH" }
delete = corsRequest { WAI.requestMethod = "DELETE" }
head = corsRequest { WAI.requestMethod = "HEAD" }
options = corsRequest { WAI.requestMethod = "OPTIONS" }

app ∷ WAI.Application
app _ respond = respond $ WAI.responseLBS HTTP.status200 [] "Success"