-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathFileServerSpec.purs
More file actions
79 lines (71 loc) · 3.12 KB
/
FileServerSpec.purs
File metadata and controls
79 lines (71 loc) · 3.12 KB
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
module Hyper.Node.FileServerSpec where
import Prelude
import Node.Buffer as Buffer
import Control.IxMonad (ibind)
import Control.Monad.Aff.Class (class MonadAff)
import Control.Monad.Eff.Class (liftEff)
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(Tuple))
import Hyper.Middleware (evalMiddleware)
import Hyper.Node.FileServer (fileServer)
import Hyper.Node.Test (TestResponseBody(TestResponseBody))
import Hyper.Response (ResponseEnded, headers, respond, writeStatus)
import Hyper.Status (statusNotFound, statusOK)
import Hyper.Test.TestServer (TestRequest(..), TestResponse(..), defaultRequest, testBody, testHeaders, testServer, testStatus)
import Node.Buffer (BUFFER)
import Node.Encoding (Encoding(UTF8))
import Node.FS (FS)
import Test.Spec (it, Spec, describe)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Assertions.String (shouldContain)
serveFilesAndGet
:: forall m e.
(MonadAff (fs :: FS, buffer :: BUFFER | e) m) =>
String
-> m (TestResponse TestResponseBody ResponseEnded)
serveFilesAndGet path =
{ request: TestRequest (defaultRequest { url = path })
, response: TestResponse Nothing [] []
, components: {}
}
# evalMiddleware app
# testServer
where
app = fileServer "test/Hyper/Node/FileServerSpec" on404 []
on404 = do
body <- liftEff (Buffer.fromString "Not Found" UTF8)
_ <- writeStatus statusNotFound
_ <- headers []
respond body
where bind = ibind
spec :: forall e. Spec (fs :: FS, buffer :: BUFFER | e) Unit
spec =
describe "Hyper.Node.FileServer" do
let assertBody assertion response expected =
case testBody response of
TestResponseBody chunks -> do
body <- liftEff (Buffer.concat chunks >>= Buffer.toString UTF8)
body `assertion` expected
bodyShouldEqual = assertBody shouldEqual
bodyShouldContain = assertBody shouldContain
describe "fileServer" do
it "serves the exactly matched file" do
response <- serveFilesAndGet "some-file.txt"
testStatus response `shouldEqual` Just statusOK
-- TODO: Figure out that this is text/plain
testHeaders response `shouldEqual` [ Tuple "Content-Type" "*/*; charset=utf-8"
, Tuple "Content-Length" "75"
]
response `bodyShouldEqual` "λ Some file contents, med sådana tecken vi använder i Sverige! 🇸🇪\n"
it "returns Nothing when no file matches the request" do
response <- serveFilesAndGet "some-file-that-does-not-exist.txt"
testStatus response `shouldEqual` Just statusNotFound
response `bodyShouldEqual` "Not Found"
it "serves index.html, if exists, when matching directory" do
response <- serveFilesAndGet "/"
testStatus response `shouldEqual` Just statusOK
-- TODO: Figure out that this is text/html
testHeaders response `shouldEqual` [ Tuple "Content-Type" "*/*; charset=utf-8"
, Tuple "Content-Length" "144"
]
response `bodyShouldContain` "<h1>Test File</h1>"