Debian Codegraph @

test/ClassicSpec.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE CPP #-}
3
4 module ClassicSpec where
5
6 #if __GLASGOW_HASKELL__ < 709
7 import Control.Applicative
8 #endif
9 import Network.HTTP
10 import Network.Stream
11 import System.IO
12 import Test.Hspec
13
14 spec :: Spec
15 spec = do
16 describe "cgiApp" $ do
17 it "accepts POST" $ do
18 let url = "http://127.0.0.1:2345/cgi-bin/echo-env/pathinfo?query=foo"
19 bdy <- rspBody <$> sendPOST url "foo bar.\nbaz!\n"
20 ans <- readFileAscii "test/data/post"
21 bdy `shouldBe` ans
22 it "causes 500 if the CGI script does not exist" $ do
23 let url = "http://127.0.0.1:2345/cgi-bin/broken"
24 sc <- rspCode <$> sendPOST url "foo bar.\nbaz!\n"
25 sc `shouldBe` (5,0,0)
26 describe "fileApp" $ do
27 it "returns index.html for /" $ do
28 let url = "http://127.0.0.1:2345/"
29 bdy <- rspBody <$> sendGET url
30 ans <- readFileAscii "test/html/index.html"
31 bdy `shouldBe` ans
32 it "works with files that lack a file extension" $ do
33 let url = "http://127.0.0.1:2345/no_extension"
34 bdy <- rspBody <$> sendGET url
35 ans <- readFileAscii "test/html/no_extension"
36 bdy `shouldBe` ans
37 it "returns 400 if not exist" $ do
38 let url = "http://127.0.0.1:2345/dummy"
39 sc <- rspCode <$> sendGET url
40 sc `shouldBe` (4,0,4)
41 it "returns Japanese HTML if language is specified" $ do
42 let url = "http://127.0.0.1:2345/ja/"
43 bdy <- rspBody <$> sendGETwH url [Header HdrAcceptLanguage "ja, en;q=0.7"]
44 ans <- readFileAscii "test/html/ja/index.html.ja"
45 bdy `shouldBe` ans
46 it "returns English HTML even if language is specified" $ do
47 let url = "http://127.0.0.1:2345/"
48 bdy <- rspBody <$> sendGETwH url [Header HdrAcceptLanguage "ja, en;q=0.7"]
49 ans <- readFileAscii "test/html/index.html"
50 bdy `shouldBe` ans
51 it "returns 304 if not changed" $ do
52 let url = "http://127.0.0.1:2345/"
53 hdr <- rspHeaders <$> sendGET url
54 let Just lm = lookupHeader HdrLastModified hdr
55 sc <- rspCode <$> sendGETwH url [Header HdrIfModifiedSince lm]
56 sc `shouldBe` (3,0,4)
57 it "can handle partial request" $ do
58 let url = "http://127.0.0.1:2345/"
59 ans = "html>\n<html"
60 bdy <- rspBody <$> sendGETwH url [Header HdrRange "bytes=10-20"]
61 bdy `shouldBe` ans
62 it "can handle HEAD" $ do
63 let url = "http://127.0.0.1:2345/"
64 sc <- rspCode <$> sendHEAD url
65 sc `shouldBe` (2,0,0)
66 it "returns 404 for HEAD if not exist" $ do
67 let url = "http://127.0.0.1:2345/dummy"
68 sc <- rspCode <$> sendHEAD url
69 sc `shouldBe` (4,0,4)
70 it "can handle HEAD even if language is specified" $ do
71 let url = "http://127.0.0.1:2345/ja/"
72 sc <- rspCode <$> sendHEADwH url [Header HdrAcceptLanguage "ja, en;q=0.7"]
73 sc `shouldBe` (2,0,0)
74 it "returns 304 for HEAD if not modified" $ do
75 let url = "http://127.0.0.1:2345/"
76 hdr <- rspHeaders <$> sendHEAD url
77 let Just lm = lookupHeader HdrLastModified hdr
78 sc <- rspCode <$> sendHEADwH url [Header HdrIfModifiedSince lm]
79 sc `shouldBe` (3,0,4)
80 it "redirects to dir/ if trailing slash is missing" $ do
81 let url = "http://127.0.0.1:2345/redirect"
82 rsp <- sendGET url
83 let sc = rspCode rsp
84 hdr = rspHeaders rsp
85 Just lm = lookupHeader HdrLocation hdr
86 sc `shouldBe` (3,0,1)
87 lm `shouldBe` "//127.0.0.1:2345/redirect/"
88
89 ----------------------------------------------------------------
90
91 sendGET :: String -> IO (Response String)
92 sendGET url = sendGETwH url []
93
94 sendGETwH :: String -> [Header] -> IO (Response String)
95 sendGETwH url hdr = unResult $ simpleHTTP $ (getRequest url) { rqHeaders = hdr }
96
97 sendHEAD :: String -> IO (Response String)
98 sendHEAD url = sendHEADwH url []
99
100 sendHEADwH :: String -> [Header] -> IO (Response String)
101 sendHEADwH url hdr = unResult $ simpleHTTP $ (headRequest url) { rqHeaders = hdr }
102
103 sendPOST :: String -> String -> IO (Response String)
104 sendPOST url body = unResult $ simpleHTTP $ postRequestWithBody url "Text/Plain" body
105
106 unResult :: IO (Result (Response String)) -> IO (Response String)
107 unResult action = do
108 res <- action
109 case res of
110 Right rsp -> return rsp
111 Left _ -> error "Connection error"
112
113 readFileAscii :: FilePath -> IO String
114 readFileAscii name = do
115 h <- openFile name ReadMode
116 hSetEncoding h latin1
117 hGetContents h