4 import Data.List (isSuffixOf)
5 import Data.Maybe (fromJust)
6 import Data.String.Utils (split)
15 import Test.HUnit (Assertion, assertEqual)
16 import Test.Framework (Test, testGroup)
17 import Test.Framework.Providers.HUnit (testCase)
18 import Text.Regex.Posix ((=~))
20 -- Distinguish between URLs (Strings) and URIs as provided by the
21 -- Network.URI module.
25 -- | Is this URI's scheme plain HTTP?
27 http uri = (uriScheme uri) == "http:"
29 -- | Is this URI's scheme (secure) HTTPS?
31 https uri = (uriScheme uri) == "https:"
33 -- | Does this URI use an HTTP-compatible port?
34 http_port :: URI -> Bool
39 (uriPort auth) `elem` ["", ":80"]
41 parse_result = uriAuthority uri
44 make_https :: URL -> URL
47 Nothing -> url -- Shrug?
50 show $ uri { uriScheme = "https:" }
52 url -- Leave non-http URLs alone.
54 parse_result = parseURIReference url
58 add_trailing_slash :: URL -> URL
59 add_trailing_slash url =
61 Nothing -> url -- Shrug?
63 let old_path = uriPath uri in
64 if isSuffixOf "/" old_path then
65 url -- It already had a trailing slash
67 show $ uri { uriPath = old_path ++ "/" }
69 parse_result = parseURIReference url
72 -- | Does this URI use an HTTPS-compatible port?
73 https_port :: URI -> Bool
78 (uriPort auth) `elem` ["", ":443"]
80 parse_result = uriAuthority uri
83 -- | Does this URL have one of the LWN hostnames?
84 lwn_host :: URI -> Bool
89 (uriRegName auth) `elem` ["lwn.net", "www.lwn.net"]
91 parse_result = uriAuthority uri
94 -- | Is the protocol portion of this URI one of the ones that LWN
96 lwn_proto :: URI -> Bool
98 ((http u) && (http_port u)) || ((https u) && (https_port u))
100 -- | Is the server/port to which we're connecting an LWN server?
101 lwn_server :: URI -> Bool
103 lwn_proto u && lwn_host u
105 -- | Is this URI's path for an LWN article?
106 lwn_article_path :: URI -> Bool
107 lwn_article_path uri =
108 path =~ "^/current(/bigpage)?/?$" ||
109 path =~ "^/Articles/[0-9]+(/bigpage)?/?$"
113 -- | Is this URI's query one that the LWN uses? The only query string
114 -- that the LWN articles use is the printable page one.
115 lwn_query :: URI -> Bool
117 query `elem` ["", "?format=printable"]
121 -- | Combine all of the other URI tests to determine if this 'URL'
122 -- belongs to an LWN article.
123 is_lwn_url :: URL -> Bool
127 Just uri -> (lwn_server uri) && (lwn_article_path uri) && (lwn_query uri)
129 parse_result = parseAbsoluteURI s
133 filename :: URL -> Maybe String
138 let components = split "/" (uriPath uri) in
139 -- Reverse them so that the filename comes first for easier
141 let reverse_components = reverse components in
142 case reverse_components of
146 parse_result = parseURIReference url
150 make_absolute_uri :: URI -> Maybe URI
151 make_absolute_uri relative_uri =
152 relativeTo relative_uri base_uri
154 base_auth = URIAuth { uriUserInfo = "",
155 uriRegName = "lwn.net",
157 base_uri = URI { uriScheme = "https:",
158 uriAuthority = Just base_auth,
164 make_absolute_url :: URL -> Maybe URL
165 make_absolute_url relative_url =
169 let abs_result = make_absolute_uri relative_uri in
172 Just abs_uri -> Just $ show abs_uri
174 parse_result = parseURIReference relative_url
176 -- | Like 'make_absolute_url', except returns its input instead of
177 -- 'Nothing' if the absolution fails.
178 try_make_absolute_url :: URL -> URL
179 try_make_absolute_url url =
180 case make_absolute_url url of
182 Just abs_url -> abs_url
184 -- | A List of LWN URLs to use during testing.
186 lwn_urls = [ proto ++ www ++ "lwn.net" ++ path ++ bigpage ++ query |
187 proto <- ["http://", "https://"],
189 bigpage <- ["", "/bigpage"],
190 query <- ["", "?format=printable"],
191 path <- [ "/current",
193 "/Articles/502371" ] ]
195 test_lwn_urls_matched :: Assertion
196 test_lwn_urls_matched =
197 assertEqual "All LWN URLs matched" True (all is_lwn_url lwn_urls)
199 test_http_uris_matched :: Assertion
200 test_http_uris_matched =
201 assertEqual (url ++ " is HTTP") True (http uri)
203 url = "http://lwn.net/Articles/500844/bigpage"
204 uri = fromJust $ parseAbsoluteURI url
206 test_https_uris_matched :: Assertion
207 test_https_uris_matched =
208 assertEqual (url ++ " is HTTPS") True (https uri)
210 url = "https://lwn.net/Articles/500844/bigpage"
211 uri = fromJust $ parseAbsoluteURI url
214 test_bare_filename_parsed :: Assertion
215 test_bare_filename_parsed =
216 assertEqual "Filename is 'example.jpg'" "example.jpg" actual_result
219 actual_result = fromJust $ filename url
221 test_absolute_filename_parsed :: Assertion
222 test_absolute_filename_parsed =
223 assertEqual "Filename is 'example.jpg'" "example.jpg" actual_result
225 url = "http://lwn.net/one/two/example.jpg"
226 actual_result = fromJust $ filename url
228 test_relative_filename_parsed :: Assertion
229 test_relative_filename_parsed =
230 assertEqual "Filename is 'example.jpg'" "example.jpg" actual_result
232 url = "/one/two/example.jpg"
233 actual_result = fromJust $ filename url
236 test_empty_url_conversion :: Assertion
237 test_empty_url_conversion =
238 assertEqual "'' converted to lwn.net" expected actual
240 expected = "https://lwn.net/"
241 actual = fromJust $ make_absolute_url ""
244 test_normal_url_conversion :: Assertion
245 test_normal_url_conversion =
246 assertEqual "Image URL is made absolute" expected actual
248 url = "/images/2012/lcj-coughlan-lattimer-sm.jpg"
249 expected = "https://lwn.net/images/2012/lcj-coughlan-lattimer-sm.jpg"
250 actual = fromJust $ make_absolute_url url
254 test_make_https :: Assertion
256 assertEqual "HTTP URL is made HTTPS" expected actual
258 url = "http://lwn.net/current"
259 expected = "https://lwn.net/current"
260 actual = make_https url
263 test_add_trailing_slash :: Assertion
264 test_add_trailing_slash =
265 assertEqual "Trailing slashes get added" expected actual
267 url = "https://lwn.net/current"
268 expected = "https://lwn.net/current/"
269 actual = add_trailing_slash url
274 testGroup "URI Tests" [
276 testGroup "URI Matching" [
277 testCase "HTTP URIs matched" test_http_uris_matched,
278 testCase "HTTPS URIs matched" test_https_uris_matched,
279 testCase "LWN URLs matched" test_lwn_urls_matched ],
281 testGroup "Filename Parsing" [
282 testCase "Bare filename parsed" test_bare_filename_parsed,
283 testCase "Absolute filename parsed" test_absolute_filename_parsed,
284 testCase "Relative filename parsed" test_relative_filename_parsed ],
286 testGroup "Relative -> Absolute Conversion" [
287 testCase "Empty URL converted to lwn.net" test_empty_url_conversion,
288 testCase "Normal URL made absolute" test_normal_url_conversion ],
290 testGroup "URL Mangling" [
291 testCase "HTTP URLs are made HTTPS" test_make_https,
292 testCase "Trailing slashes get added" test_add_trailing_slash ]