Debian Codegraph @

test/URI/ByteStringTests.hs

raw
1 {-# LANGUAGE GADTs #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4
5 module URI.ByteStringTests (tests) where
6
7 -------------------------------------------------------------------------------
8 import qualified Blaze.ByteString.Builder as BB
9 import Control.Applicative (Const (..))
10 import Data.ByteString (ByteString)
11 import qualified Data.ByteString.Char8 as B8
12 import Data.Either
13 import Data.Function.Compat ((&))
14 import Data.Functor.Identity (Identity (..))
15 import qualified Data.Map.Strict as M
16 import Data.Monoid
17 import Hedgehog
18 import qualified Hedgehog.Gen as Gen
19 import Safe (readMay)
20 import Test.Tasty
21 import Test.Tasty.HUnit
22 import Test.Tasty.Hedgehog
23 -------------------------------------------------------------------------------
24
25 -------------------------------------------------------------------------------
26 import URI.ByteString
27 import URI.ByteString.Generators
28 -------------------------------------------------------------------------------
29 import URI.ByteStringQQTests ()
30 import Prelude
31
32 infixr 4 .~
33
34 (.~) :: ((a -> Identity b) -> s -> Identity t) -> b -> s -> t
35 (.~) l b s = runIdentity (l (const (Identity b)) s)
36
37 infixl 9 ^.
38
39 (^.) :: s -> ((a -> Const a a) -> s -> Const a s) -> a
40 s ^. l = getConst (l Const s)
41
42 tests :: TestTree
43 tests =
44 testGroup
45 "URI.Bytestring"
46 [ parseUriTests,
47 uriParseErrorInstancesTests,
48 lensTests,
49 serializeURITests,
50 normalizeURITests
51 ]
52
53 -------------------------------------------------------------------------------
54 parseUriTests :: TestTree
55 parseUriTests =
56 testGroup
57 "parseUri"
58 [ testParses "http://www.example.org/" $
59 URI
60 (Scheme "http")
61 (Just (Authority Nothing (Host "www.example.org") Nothing))
62 "/"
63 mempty
64 Nothing,
65 testParseHost "http://www.example.org" "www.example.org",
66 -- IPV4
67 testParseHost "http://192.168.1.1" "192.168.1.1",
68 -- IPV6
69 testParseHost "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]" "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210",
70 testParseHost "http://[1080:0:0:0:8:800:200C:417A]" "1080:0:0:0:8:800:200C:417A",
71 testParseHost "http://[3ffe:2a00:100:7031::1]" "3ffe:2a00:100:7031::1",
72 testParseHost "http://[::192.9.5.5]" "::192.9.5.5",
73 testParseHost "http://[::FFFF:129.144.52.38]" "::FFFF:129.144.52.38",
74 testParseHost "http://[2010:836B:4179::836B:4179]" "2010:836B:4179::836B:4179",
75 testParseHost "http://[2010:836B:4179::836B:4179]" "2010:836B:4179::836B:4179",
76 -- IPVFuture
77 testParseHost "http://[v1.fe80::a+en1]" "v1.fe80::a+en1",
78 testParses "https://user:pass:wo%[email protected]?foo=bar&foo=baz+quux#frag" $
79 URI
80 (Scheme "https")
81 (Just (Authority (Just (UserInfo "user" "pass:wo rd")) (Host "www.example.org") Nothing))
82 ""
83 (Query [("foo", "bar"), ("foo", "baz+quux")])
84 (Just "frag"),
85 testParses "https://[email protected]" $
86 URI
87 (Scheme "https")
88 (Just (Authority (Just (UserInfo "user" "")) (Host "www.example.org") Nothing))
89 ""
90 (Query [])
91 Nothing,
92 testParses "https://@www.example.org" $
93 URI
94 (Scheme "https")
95 (Just (Authority (Just (UserInfo "" "")) (Host "www.example.org") Nothing))
96 ""
97 (Query [])
98 Nothing,
99 testParses "https://::@www.example.org" $
100 URI
101 (Scheme "https")
102 (Just (Authority (Just (UserInfo "" ":")) (Host "www.example.org") Nothing))
103 ""
104 (Query [])
105 Nothing,
106 -- trailing &
107 testParses "http://www.example.org?foo=bar&" $
108 URI
109 (Scheme "http")
110 (Just (Authority Nothing (Host "www.example.org") Nothing))
111 ""
112 (Query [("foo", "bar")])
113 Nothing,
114 testParses
115 "http://www.google.com:80/aclk?sa=l&ai=CChPOVvnoU8fMDI_QsQeE4oGwDf664-EF7sq01HqV1MMFCAAQAigDUO3VhpcDYMnGqYvApNgPoAGq3vbiA8gBAaoEKE_QQwekDUoMeW9IQghV4HRuzL_l-7vVjlML559kix6XOcC1c4Tb9xeAB76hiR2QBwGoB6a-Gw&sig=AOD64_3Ulyu0DcDsc1AamOIxq63RF9u4zQ&rct=j&q=&ved=0CCUQ0Qw&adurl=http://www.aruba.com/where-to-stay/hotels-and-resorts%3Ftid%3D122"
116 URI
117 { uriScheme = Scheme {schemeBS = "http"},
118 uriAuthority = Just Authority {authorityUserInfo = Nothing, authorityHost = Host {hostBS = "www.google.com"}, authorityPort = Just (Port 80)},
119 uriPath = "/aclk",
120 uriQuery =
121 Query
122 { queryPairs =
123 [ ("sa", "l"),
124 ("ai", "CChPOVvnoU8fMDI_QsQeE4oGwDf664-EF7sq01HqV1MMFCAAQAigDUO3VhpcDYMnGqYvApNgPoAGq3vbiA8gBAaoEKE_QQwekDUoMeW9IQghV4HRuzL_l-7vVjlML559kix6XOcC1c4Tb9xeAB76hiR2QBwGoB6a-Gw"),
125 ("sig", "AOD64_3Ulyu0DcDsc1AamOIxq63RF9u4zQ"),
126 ("rct", "j"),
127 ("q", ""),
128 ("ved", "0CCUQ0Qw"),
129 ("adurl", "http://www.aruba.com/where-to-stay/hotels-and-resorts?tid=122")
130 ]
131 },
132 uriFragment = Nothing
133 },
134 testParseFailure "$$$$://www.example.org/" (MalformedScheme NonAlphaLeading),
135 testParses "http://www.example.org/foo#bar" $
136 URI
137 (Scheme "http")
138 (Just (Authority Nothing (Host "www.example.org") Nothing))
139 "/foo"
140 mempty
141 (Just "bar"),
142 testParses "http://www.example.org/foo#" $
143 URI
144 (Scheme "http")
145 (Just (Authority Nothing (Host "www.example.org") Nothing))
146 "/foo"
147 mempty
148 (Just ""),
149 testParseFailure "http://www.example.org/foo#bar#baz" MalformedFragment,
150 testParseFailure "https://www.example.org?listParam[]=foo,bar" MalformedQuery,
151 testParsesLax "https://www.example.org?listParam[]=foo,bar" $
152 URI
153 (Scheme "https")
154 (Just (Authority Nothing (Host "www.example.org") Nothing))
155 ""
156 (Query [("listParam[]", "foo,bar")])
157 Nothing,
158 testParses "https://www.example.org?listParam%5B%5D=foo,bar" $
159 URI
160 (Scheme "https")
161 (Just (Authority Nothing (Host "www.example.org") Nothing))
162 ""
163 (Query [("listParam[]", "foo,bar")])
164 Nothing,
165 testParses "https://www.example.org#only-fragment" $
166 URI
167 (Scheme "https")
168 (Just (Authority Nothing (Host "www.example.org") Nothing))
169 ""
170 (Query [])
171 (Just "only-fragment"),
172 testParses "https://www.example.org/weird%20path" $
173 URI
174 (Scheme "https")
175 (Just (Authority Nothing (Host "www.example.org") Nothing))
176 "/weird path"
177 (Query [])
178 Nothing,
179 parseTestURI strictURIParserOptions "http://www.example.org/." $
180 Right $
181 URI
182 (Scheme "http")
183 (Just (Authority Nothing (Host "www.example.org") Nothing))
184 "/."
185 (Query [])
186 Nothing,
187 parseTestURI strictURIParserOptions "http:/." $
188 Right $
189 URI
190 (Scheme "http")
191 Nothing
192 "/."
193 (Query [])
194 Nothing,
195 parseTestURI strictURIParserOptions "file:c:/foo" $
196 Right $
197 URI
198 (Scheme "file")
199 Nothing
200 "c:/foo"
201 (Query [])
202 Nothing,
203 parseTestURI strictURIParserOptions "file:///foo/%F" $
204 Left MalformedPath,
205 parseTestURI strictURIParserOptions "file:///foo/?foo=%F" $
206 Left MalformedQuery,
207 roundtripTestURI strictURIParserOptions "ftp://ftp.is.co.za/rfc/rfc1808.txt",
208 roundtripTestURI strictURIParserOptions "http://www.ietf.org/rfc/rfc2396.txt",
209 roundtripTestURI strictURIParserOptions "mailto:[email protected]",
210 roundtripTestURI strictURIParserOptions "news:comp.infosystems.www.servers.unix",
211 roundtripTestURI strictURIParserOptions "tel:+1-816-555-1212",
212 roundtripTestURI strictURIParserOptions "telnet://192.0.2.16:80/",
213 -- percent encoding in queries and fragments
214 roundtripTestURI strictURIParserOptions "http://example.com/foo#bar%20baz",
215 roundtripTestURI strictURIParserOptions "http://example.com/foo?bar%20baz=quux",
216 roundtripTestURI strictURIParserOptions "http://example.com/foo?bar=baz%20quux",
217 roundtripTestURI strictURIParserOptions "http://example.com/foo?bar=baz%20quux#bar%20baz",
218 -- RFC 3986, Section 4.2
219 parseTestRelativeRef strictURIParserOptions "verysimple" $
220 Right $
221 RelativeRef
222 Nothing
223 "verysimple"
224 (Query [])
225 Nothing,
226 parseTestRelativeRef strictURIParserOptions "this:that/thap/sub?1=2" $
227 Left $
228 MalformedPath,
229 parseTestRelativeRef strictURIParserOptions "./this:that/thap/sub?1=2" $
230 Right $
231 RelativeRef
232 Nothing
233 "./this:that/thap/sub"
234 (Query [("1", "2")])
235 Nothing
236 ]
237
238 -------------------------------------------------------------------------------
239 uriParseErrorInstancesTests :: TestTree
240 uriParseErrorInstancesTests =
241 testGroup
242 "URIParseError instances"
243 [ testProperty "roundtrips between Show and Read" $
244 property $ do
245 parseError <- forAll genURIParseError
246 trippingShow parseError
247 ]
248
249 -------------------------------------------------------------------------------
250 lensTests :: TestTree
251 lensTests =
252 testGroup
253 "lenses"
254 [ testProperty "schemeBSL Lens" $
255 property $ do
256 wrapped <- forAll genScheme
257 Scheme bs' <- forAll genScheme
258 (wrapped ^. schemeBSL) === schemeBS wrapped
259 (wrapped & schemeBSL .~ bs') === wrapped {schemeBS = bs'},
260 testProperty "hostBSL Lens" $
261 property $ do
262 wrapped <- forAll genHost
263 Host bs' <- forAll genHost
264 (wrapped ^. hostBSL) === hostBS wrapped
265 (wrapped & hostBSL .~ bs') === wrapped {hostBS = bs'},
266 testProperty "portNumberL Lens" $
267 property $ do
268 wrapped <- forAll genPort
269 Port n' <- forAll genPort
270 (wrapped ^. portNumberL) === portNumber wrapped
271 (wrapped & portNumberL .~ n') === wrapped {portNumber = n'},
272 testProperty "queryPairsL Lens" $
273 property $ do
274 wrapped <- forAll genQuery
275 Query ps' <- forAll genQuery
276 wrapped ^. queryPairsL === queryPairs wrapped
277 (wrapped & queryPairsL .~ ps') === wrapped {queryPairs = ps'},
278 testProperty "authorityUserInfoL Lens" $
279 property $ do
280 authority <- forAll genAuthority
281 userInfo <- forAll (Gen.maybe genUserInfo)
282 (authority ^. authorityUserInfoL === authorityUserInfo authority)
283 (authority & authorityUserInfoL .~ userInfo) === authority {authorityUserInfo = userInfo},
284 testProperty "authorityHostL Lens" $
285 property $ do
286 authority <- forAll genAuthority
287 host <- forAll genHost
288 (authority ^. authorityHostL === authorityHost authority)
289 (authority & authorityHostL .~ host) === authority {authorityHost = host},
290 testProperty "authorityPortL Lens" $
291 property $ do
292 authority <- forAll genAuthority
293 port <- forAll (Gen.maybe genPort)
294 (authority ^. authorityPortL === authorityPort authority)
295 (authority & authorityPortL .~ port) === authority {authorityPort = port},
296 testProperty "uiUsernameL Lens" $
297 property $ do
298 ui <- forAll genUserInfo
299 bs <- forAll genBS
300 (ui ^. uiUsernameL === uiUsername ui)
301 (ui & uiUsernameL .~ bs) === ui {uiUsername = bs},
302 testProperty "uiPasswordL Lens" $
303 property $ do
304 ui <- forAll genUserInfo
305 bs <- forAll genBS
306 (ui ^. uiPasswordL === uiPassword ui)
307 (ui & uiPasswordL .~ bs) === ui {uiPassword = bs},
308 testProperty "uriSchemeL Lens" $
309 property $ do
310 uri <- forAll genAbsoluteURIRef
311 x <- forAll genScheme
312 uri ^. uriSchemeL === uriScheme uri
313 (uri & uriSchemeL .~ x) === uri {uriScheme = x},
314 testProperty "authorityL Lens on URI" $
315 property $ do
316 uri <- forAll genAbsoluteURIRef
317 x <- forAll (Gen.maybe genAuthority)
318 uri ^. authorityL === uriAuthority uri
319 (uri & authorityL .~ x) === uri {uriAuthority = x},
320 testProperty "pathL Lens on URI" $
321 property $ do
322 uri <- forAll genAbsoluteURIRef
323 x <- forAll genBS
324 uri ^. pathL === uriPath uri
325 (uri & pathL .~ x) === uri {uriPath = x},
326 testProperty "queryL Lens on URI" $
327 property $ do
328 uri <- forAll genAbsoluteURIRef
329 x <- forAll genQuery
330 uri ^. queryL === uriQuery uri
331 (uri & queryL .~ x) === uri {uriQuery = x},
332 testProperty "fragmentL Lens on URI" $
333 property $ do
334 uri <- forAll genAbsoluteURIRef
335 x <- forAll (Gen.maybe genBS)
336 uri ^. fragmentL === uriFragment uri
337 (uri & fragmentL .~ x) === uri {uriFragment = x},
338 testProperty "authorityL Lens on relative ref" $
339 property $ do
340 rr <- forAll genRelativeURIRef
341 x <- forAll (Gen.maybe genAuthority)
342 (rr ^. authorityL === rrAuthority rr)
343 (rr & authorityL .~ x) === rr {rrAuthority = x},
344 testProperty "pathL Lens on relative ref" $
345 property $ do
346 rr <- forAll genRelativeURIRef
347 x <- forAll genBS
348 rr ^. pathL === rrPath rr
349 (rr & pathL .~ x) === rr {rrPath = x},
350 testProperty "queryL Lens on relative ref" $
351 property $ do
352 rr <- forAll genRelativeURIRef
353 x <- forAll genQuery
354 rr ^. queryL === rrQuery rr
355 (rr & queryL .~ x) === rr {rrQuery = x},
356 testProperty "fragmentL Lens on relative ref" $
357 property $ do
358 rr <- forAll genRelativeURIRef
359 x <- forAll (Gen.maybe genBS)
360 rr ^. fragmentL === rrFragment rr
361 (rr & fragmentL .~ x) === rr {rrFragment = x}
362 ]
363
364 -------------------------------------------------------------------------------
365 testParses :: ByteString -> URI -> TestTree
366 testParses = testParses' strictURIParserOptions
367
368 -------------------------------------------------------------------------------
369 testParseHost :: ByteString -> ByteString -> TestTree
370 testParseHost uri expectedHost =
371 testParses uri $
372 URI
373 (Scheme "http")
374 (Just (Authority Nothing (Host expectedHost) Nothing))
375 mempty
376 mempty
377 Nothing
378
379 -------------------------------------------------------------------------------
380 testParsesLax :: ByteString -> URI -> TestTree
381 testParsesLax = testParses' laxURIParserOptions
382
383 -------------------------------------------------------------------------------
384 testParses' :: URIParserOptions -> ByteString -> URI -> TestTree
385 testParses' opts s u =
386 testGroup
387 "testParses'"
388 [ parseTestURI opts s $ Right u,
389 parseTestRelativeRef opts (makeRelativeRefBS s) $ Right (makeRelativeRefTyped u)
390 ]
391
392 -------------------------------------------------------------------------------
393 makeRelativeRefTyped :: URI -> RelativeRef
394 makeRelativeRefTyped (URI _ a p q f) = RelativeRef a p q f
395
396 -------------------------------------------------------------------------------
397 makeRelativeRefBS :: ByteString -> ByteString
398 makeRelativeRefBS s = B8.tail x
399 where
400 (_, x) = B8.break (== ':') s
401
402 -------------------------------------------------------------------------------
403 testParseFailure :: ByteString -> URIParseError -> TestTree
404 testParseFailure s = parseTestURI strictURIParserOptions s . Left
405
406 -------------------------------------------------------------------------------
407 parseTestURI ::
408 URIParserOptions ->
409 ByteString ->
410 Either URIParseError URI ->
411 TestTree
412 parseTestURI opts s r = testCase (B8.unpack s) $ parseURI opts s @?= r
413
414 -------------------------------------------------------------------------------
415 roundtripTestURI ::
416 URIParserOptions ->
417 ByteString ->
418 TestTree
419 roundtripTestURI opts s =
420 testCase (B8.unpack s) $ (parseURI opts s >>= return . serializeURIRef') @?= Right s
421
422 -------------------------------------------------------------------------------
423 parseTestRelativeRef ::
424 URIParserOptions ->
425 ByteString ->
426 Either URIParseError RelativeRef ->
427 TestTree
428 parseTestRelativeRef opts s r =
429 testCase (B8.unpack s) $ parseRelativeRef opts s @?= r
430
431 -------------------------------------------------------------------------------
432 serializeURITests :: TestTree
433 serializeURITests =
434 testGroup
435 "serializeURIRef"
436 [ testCase "renders userinfo correctly" $ do
437 let ui = UserInfo "user" "pass"
438 let uri =
439 URI
440 (Scheme "http")
441 (Just (Authority (Just ui) (Host "www.example.org") (Just port)))
442 "/"
443 (Query [("foo", "bar")])
444 (Just "somefragment")
445 let res = BB.toLazyByteString (serializeURIRef uri)
446 res @?= "http://user:[email protected]:123/?foo=bar#somefragment",
447 testCase "encodes decoded paths" $ do
448 let uri =
449 URI
450 (Scheme "http")
451 (Just (Authority Nothing (Host "www.example.org") (Just port)))
452 "/weird path"
453 (Query [])
454 Nothing
455 let res = BB.toLazyByteString (serializeURIRef uri)
456 res @?= "http://www.example.org:123/weird%20path",
457 testCase "encodes relative refs" $ do
458 let ui = UserInfo "user" "pass"
459 let uri =
460 RelativeRef
461 (Just (Authority (Just ui) (Host "www.example.org") (Just port)))
462 "/"
463 (Query [("foo", "bar")])
464 (Just "somefragment")
465 let res = BB.toLazyByteString (serializeURIRef uri)
466 res @?= "//user:[email protected]:123/?foo=bar#somefragment"
467 ]
468 where
469 port = Port 123
470
471 -------------------------------------------------------------------------------
472 normalizeURITests :: TestTree
473 normalizeURITests =
474 testGroup
475 "normalization"
476 [ testCase "downcase schema" $ do
477 normalizeURIBS o {unoDowncaseScheme = True} "hTtP://example.org"
478 @?= "http://example.org",
479 testCase "downcase host" $ do
480 normalizeURIBS o {unoDowncaseHost = True} "http://ExAmPlE.org"
481 @?= "http://example.org",
482 testCase "drop default port http" $ do
483 normalizeURIBS o {unoDropDefPort = True} "http://example.org:80"
484 @?= "http://example.org",
485 testCase "drop default port https" $ do
486 normalizeURIBS o {unoDropDefPort = True} "https://example.org:443"
487 @?= "https://example.org",
488 testCase "drop default port no port" $ do
489 normalizeURIBS o {unoDropDefPort = True} "http://example.org"
490 @?= "http://example.org",
491 testCase "drop default port nondefault" $ do
492 normalizeURIBS o {unoDropDefPort = True} "http://example.org:8000"
493 @?= "http://example.org:8000",
494 testCase "drop default unknown schema" $ do
495 normalizeURIBS o {unoDropDefPort = True} "bogus://example.org:9999"
496 @?= "bogus://example.org:9999",
497 testCase "user-extensable port defaulting hit" $ do
498 normalizeURIBS
499 o
500 { unoDropDefPort = True,
501 unoDefaultPorts = M.singleton (Scheme "ftp") (Port 21)
502 }
503 "ftp://example.org:21"
504 @?= "ftp://example.org",
505 testCase "user-extensable port defaulting off" $ do
506 normalizeURIBS
507 o
508 { unoDropDefPort = False,
509 unoDefaultPorts = M.singleton (Scheme "ftp") (Port 21)
510 }
511 "ftp://example.org:21"
512 @?= "ftp://example.org:21",
513 testCase "user-extensable port defaulting miss" $ do
514 normalizeURIBS
515 o
516 { unoDropDefPort = True,
517 unoDefaultPorts = M.singleton (Scheme "ftp") (Port 21)
518 }
519 "http://example.org:80"
520 @?= "http://example.org:80",
521 testCase "slash empty path" $ do
522 normalizeURIBS o {unoSlashEmptyPath = True} "http://example.org"
523 @?= "http://example.org/",
524 testCase "slash empty path with nonempty path" $ do
525 normalizeURIBS o {unoSlashEmptyPath = True} "http://example.org/foo/bar"
526 @?= "http://example.org/foo/bar",
527 testCase "drop redundant slashes" $ do
528 normalizeURIBS o {unoDropExtraSlashes = True} "http://example.org/foo//bar///baz"
529 @?= "http://example.org/foo/bar/baz",
530 testCase "sort params" $ do
531 normalizeURIBS o {unoSortParameters = True} "http://example.org/foo?zulu=1&charlie=&alpha=1"
532 @?= "http://example.org/foo?alpha=1&charlie=&zulu=1",
533 testCase "remove dot segments" $ do
534 normalizeURIBS o {unoRemoveDotSegments = True} "http://example.org/a/b/c/./../../g"
535 @?= "http://example.org/a/g",
536 testCase "percent encoding is upcased automatically" $ do
537 normalizeURIBS o "http://example.org/a?foo%3abar=baz"
538 @?= "http://example.org/a?foo%3Abar=baz",
539 testCase "aggressive normalization retains slashes (issue 41)" $ do
540 normalizeURIBS aggressiveNormalization "http://example.org/"
541 @?= "http://example.org/"
542 ]
543 where
544 o = noNormalization
545 normalizeURIBS opts bs = case parseURI laxURIParserOptions bs of
546 Right x -> normalizeURIRef' opts x
547 Left e -> error ("Test error, " <> show bs <> " did not parse: " <> show e)
548
549 trippingShow ::
550 ( Show a,
551 Read a,
552 Eq a,
553 MonadTest m
554 ) =>
555 a ->
556 m ()
557 trippingShow a = tripping a show readMay