From 427a2516b3fafdd9afaea469943d19c5b100d7f1 Mon Sep 17 00:00:00 2001 From: vi Date: Sat, 23 Aug 2014 19:00:48 +0800 Subject: [PATCH] Isolated tests for predicates defined in the top-level Internal module. --- .../Rules/Internal/ParserSpec.hs | 4 -- .../HTTPSEverywhere/Rules/InternalSpec.hs | 63 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 test/Data/HTTPSEverywhere/Rules/InternalSpec.hs diff --git a/test/Data/HTTPSEverywhere/Rules/Internal/ParserSpec.hs b/test/Data/HTTPSEverywhere/Rules/Internal/ParserSpec.hs index 3aeb1be..1798715 100644 --- a/test/Data/HTTPSEverywhere/Rules/Internal/ParserSpec.hs +++ b/test/Data/HTTPSEverywhere/Rules/Internal/ParserSpec.hs @@ -41,10 +41,6 @@ spec = do length (ruleSetRules effRuleSet) `shouldBe` 2 it "Should parse out one exclusion from the EFF fixture." $ do length (ruleSetExclusions effRuleSet) `shouldBe` 1 - - -- BE WARY: the succeeding may fail due to bugs in the parent module, which - -- has no tests yet. - it "A target in the EFF fixture should match eff.org" $ do let url = unsafeParseURL "http://eff.org/thing" effRuleSet `hasTargetMatching` url `shouldBe` True diff --git a/test/Data/HTTPSEverywhere/Rules/InternalSpec.hs b/test/Data/HTTPSEverywhere/Rules/InternalSpec.hs new file mode 100644 index 0000000..91f7f57 --- /dev/null +++ b/test/Data/HTTPSEverywhere/Rules/InternalSpec.hs @@ -0,0 +1,63 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Data.HTTPSEverywhere.Rules.InternalSpec ( + spec +) where + +import Prelude hiding (unlines) +import Control.Monad (join) +import Data.Functor ((<$>)) +import Data.HTTPSEverywhere.Rules.Internal (hasTargetMatching, hasTriggeringRuleOn, hasExclusionMatching) +import Data.HTTPSEverywhere.Rules.Internal.Types (RuleSet(..), Target(..), Exclusion(..), Rule(..)) +import Data.Maybe (fromJust, catMaybes) +import Data.Text (pack, unpack) +import Data.Text.ICU.Extras (match, findAndReplace) +import Network.URI (parseURI) +import Test.Hspec (Spec, describe, it, shouldBe) + +spec :: Spec +spec = do + let unsafeParseURL = fromJust . parseURI + describe "hasTargetMatching" $ do + it "Should be True for matching targets." $ do + let url = unsafeParseURL "http://eff.org/thing" + fixture `hasTargetMatching` url `shouldBe` True + it "Should be False for mis-matching targets." $ do + let url = unsafeParseURL "http://gnu.org/thing" + fixture `hasTargetMatching` url `shouldBe` False + describe "hasTriggeringRuleOn" $ do + it "Should be 'Just https://...' for matching rules." $ do + let url = unsafeParseURL "http://eff.org/thing" + fixture `hasTriggeringRuleOn` url `shouldBe` parseURI "https://eff.org/thing" + it "Should be Nothing for mis-matching rules." $ do + let url = unsafeParseURL "http://gnu.org/thing" + fixture `hasTriggeringRuleOn` url `shouldBe` Nothing + describe "hasExclusionMatching" $ do + it "Should be True for matching targets." $ do + let url = unsafeParseURL "http://action.eff.org/thing" + fixture `hasExclusionMatching` url `shouldBe` True + it "Should be False for mis-matching targets." $ do + let url = unsafeParseURL "http://eff.org/thing" + fixture `hasExclusionMatching` url `shouldBe` False + +-- We avoid the parser here, for purposes of isolation: + +fixture :: RuleSet +fixture = RuleSet + { ruleSetName = "EFF" + , ruleSetTargets = catMaybes + [ Target . checkURI <$> match "eff\\.org" + , Target . checkURI <$> match ".*\\.eff\\.org" + ] + , ruleSetExclusions = catMaybes + [ Exclusion . checkURI <$> match "^http://action\\.eff\\.org/" + ] + , ruleSetRules = catMaybes + [ Rule . changeURI <$> findAndReplace "^http://eff\\.org/" "https://eff.org/" + , Rule . changeURI <$> findAndReplace "^http://([^/:@]*)\\.eff\\.org/" "https://$1.eff.org/" + ] + , ruleSetCookieRules = + [] + } + where checkURI = (. pack . show) + changeURI f = join . fmap (parseURI . unpack) . checkURI f