4 import Data.Maybe (fromJust)
5 import Data.String.Utils (split)
14 import Test.HUnit (Assertion, assertEqual)
15 import Test.Framework (Test, testGroup)
16 import Test.Framework.Providers.HUnit (testCase)
17 import Text.Regex.Posix ((=~))
19 -- Distinguish between URLs (Strings) and URIs as provided by the
20 -- Network.URI module.
24 -- | Is this URI's scheme plain HTTP?
26 http uri = (uriScheme uri) == "http:"
28 -- | Is this URI's scheme (secure) HTTPS?
30 https uri = (uriScheme uri) == "https:"
32 -- | Does this URI use an HTTP-compatible port?
33 http_port :: URI -> Bool
38 (uriPort auth) `elem` ["", ":80"]
40 parse_result = uriAuthority uri
43 make_https :: URL -> URL
46 Nothing -> url -- Shrug?
49 show $ uri { uriScheme = "https:" }
51 url -- Leave non-http URLs alone.
53 parse_result = parseURIReference url
56 -- | Does this URI use an HTTPS-compatible port?
57 https_port :: URI -> Bool
62 (uriPort auth) `elem` ["", ":443"]
64 parse_result = uriAuthority uri
67 -- | Does this URL have one of the LWN hostnames?
68 lwn_host :: URI -> Bool
73 (uriRegName auth) `elem` ["lwn.net", "www.lwn.net"]
75 parse_result = uriAuthority uri
78 -- | Is the protocol portion of this URI one of the ones that LWN
80 lwn_proto :: URI -> Bool
82 ((http u) && (http_port u)) || ((https u) && (https_port u))
84 -- | Is the server/port to which we're connecting an LWN server?
85 lwn_server :: URI -> Bool
87 lwn_proto u && lwn_host u
89 -- | Is this URI's path for an LWN article?
90 lwn_article_path :: URI -> Bool
91 lwn_article_path uri =
92 path =~ "^/current(/bigpage)?/?$" ||
93 path =~ "^/Articles/[0-9]+(/bigpage)?/?$"
97 -- | Is this URI's query one that the LWN uses? The only query string
98 -- that the LWN articles use is the printable page one.
99 lwn_query :: URI -> Bool
101 query `elem` ["", "?format=printable"]
105 -- | Combine all of the other URI tests to determine if this 'URL'
106 -- belongs to an LWN article.
107 is_lwn_url :: URL -> Bool
111 Just uri -> (lwn_server uri) && (lwn_article_path uri) && (lwn_query uri)
113 parse_result = parseAbsoluteURI s
117 filename :: URL -> Maybe String
122 let components = split "/" (uriPath uri) in
123 -- Reverse them so that the filename comes first for easier
125 let reverse_components = reverse components in
126 case reverse_components of
130 parse_result = parseURIReference url
134 make_absolute_uri :: URI -> Maybe URI
135 make_absolute_uri relative_uri =
136 relativeTo relative_uri base_uri
138 base_auth = URIAuth { uriUserInfo = "",
139 uriRegName = "lwn.net",
141 base_uri = URI { uriScheme = "https:",
142 uriAuthority = Just base_auth,
148 make_absolute_url :: URL -> Maybe URL
149 make_absolute_url relative_url =
153 let abs_result = make_absolute_uri relative_uri in
156 Just abs_uri -> Just $ show abs_uri
158 parse_result = parseURIReference relative_url
160 -- | Like 'make_absolute_url', except returns its input instead of
161 -- 'Nothing' if the absolution fails.
162 try_make_absolute_url :: URL -> URL
163 try_make_absolute_url url =
164 case make_absolute_url url of
166 Just abs_url -> abs_url
168 -- | A List of LWN URLs to use during testing.
170 lwn_urls = [ proto ++ www ++ "lwn.net" ++ path ++ bigpage ++ query |
171 proto <- ["http://", "https://"],
173 bigpage <- ["", "/bigpage"],
174 query <- ["", "?format=printable"],
175 path <- [ "/current",
177 "/Articles/502371" ] ]
179 test_lwn_urls_matched :: Assertion
180 test_lwn_urls_matched =
181 assertEqual "All LWN URLs matched" True (all is_lwn_url lwn_urls)
183 test_http_uris_matched :: Assertion
184 test_http_uris_matched =
185 assertEqual (url ++ " is HTTP") True (http uri)
187 url = "http://lwn.net/Articles/500844/bigpage"
188 uri = fromJust $ parseAbsoluteURI url
190 test_https_uris_matched :: Assertion
191 test_https_uris_matched =
192 assertEqual (url ++ " is HTTPS") True (https uri)
194 url = "https://lwn.net/Articles/500844/bigpage"
195 uri = fromJust $ parseAbsoluteURI url
198 test_bare_filename_parsed :: Assertion
199 test_bare_filename_parsed =
200 assertEqual "Filename is 'example.jpg'" "example.jpg" actual_result
203 actual_result = fromJust $ filename url
205 test_absolute_filename_parsed :: Assertion
206 test_absolute_filename_parsed =
207 assertEqual "Filename is 'example.jpg'" "example.jpg" actual_result
209 url = "http://lwn.net/one/two/example.jpg"
210 actual_result = fromJust $ filename url
212 test_relative_filename_parsed :: Assertion
213 test_relative_filename_parsed =
214 assertEqual "Filename is 'example.jpg'" "example.jpg" actual_result
216 url = "/one/two/example.jpg"
217 actual_result = fromJust $ filename url
220 test_empty_url_conversion :: Assertion
221 test_empty_url_conversion =
222 assertEqual "'' converted to lwn.net" expected actual
224 expected = "https://lwn.net/"
225 actual = fromJust $ make_absolute_url ""
228 test_normal_url_conversion :: Assertion
229 test_normal_url_conversion =
230 assertEqual "Image URL is made absolute" expected actual
232 url = "/images/2012/lcj-coughlan-lattimer-sm.jpg"
233 expected = "https://lwn.net/images/2012/lcj-coughlan-lattimer-sm.jpg"
234 actual = fromJust $ make_absolute_url url
239 testGroup "URI Tests" [
241 testGroup "URI Matching" [
242 testCase "HTTP URIs matched" test_http_uris_matched,
243 testCase "HTTPS URIs matched" test_https_uris_matched,
244 testCase "LWN URLs matched" test_lwn_urls_matched ],
246 testGroup "Filename Parsing" [
247 testCase "Bare filename parsed" test_bare_filename_parsed,
248 testCase "Absolute filename parsed" test_absolute_filename_parsed,
249 testCase "Relative filename parsed" test_relative_filename_parsed ],
251 testGroup "Relative -> Absolute Conversion" [
252 testCase "Empty URL converted to lwn.net" test_empty_url_conversion,
253 testCase "Normal URL made absolute" test_normal_url_conversion ]]