-- | Actual test suite
suite :: Spec
suite = do
    describe "NsAction" $
        it "can marshal all NsAction types as valid JSON" $
            case mapMaybe (decode . encode) exampleNsActions of
                (x :: [Value]) -> length x `shouldBe` length exampleNsActions
                y              -> error $ show y ++ " should be a list of valid JSON objects"
    describe "NsRestletConfig" $ do
        it "can create identical NsRestletConfig object from dissimilar origins" $ do
            let a = ("http://example.com:8080", 12345 :: Int, 1000 :: Int, "foo@example.com", "bar")
            let b = NsRestletConfig (fromJust . parseURI $ "http://example.com:8080")
                                    (12345 :: Integer)
                                    (1000 :: Integer)
                                    "foo@example.com"
                                    "bar"
                                    Nothing
                                    Nothing
            toNsRestletConfig a `shouldBe` b
        it "does not accept invalid URLs" $ do
            let a = ("obviously incorrect ha ha", 12345 :: Int, 1000 :: Int, "foo@example.com", "bar")
            evaluate (show $ toNsRestletConfig a) `shouldThrow` errorCall "Maybe.fromJust: Nothing"

    describe "Restlet Requests" $
        it "creates the expected headers" $ do
            let (nsid, nsrole, nsident, nspwd) = (12345 :: Int, 1000 :: Int, "foo@example.com", "bar")

            let a = ("http://example.com:8080", nsid, nsrole, nsident, nspwd)
            let p = "/test"
            r <- makeRequest (toNsRestletConfig a) p
            qPath r `shouldBe` C.pack p
            let hdrs = retrieveHeaders . qHeaders $ r
            liftIO $ putStrLn $ show hdrs
            tryBsMatch "Content-Type" "application/json" hdrs
            tryBsMatch "Accept" "application/json" hdrs
            tryBsMatch "User-Agent" "NsRestlet" hdrs
            tryBsMatch "Authorization" (Prelude.concat ["NLAuth nlauth_account=",
                                                        show nsid,
                                                        ",nlauth_email=",
                                                        nsident,
                                                        ",nlauth_role=",
                                                        show nsrole,
                                                        ",nlauth_signature=",
                                                        nspwd]) hdrs

    describe "Restlet Errors" $ do
        it "should parse GibberishError correctly" $
            interpretError (head exampleRestletErrors) `shouldBe` GibberishError 400 "Unparseable response, expecting JSON." (C.pack "Bad Request")
        it "should parse BeginChunking correctly" $
            interpretError (exampleRestletErrors !! 1) `shouldBe` BeginChunking 400
        it "should parse EndChunking correctly" $
            interpretError (exampleRestletErrors !! 2) `shouldBe` EndChunking 400
        it "should parse NotFound correctly" $
            interpretError (exampleRestletErrors !! 3) `shouldBe` NotFound 404 ""
        it "should parse InvalidSearchFilter correctly" $
            interpretError (exampleRestletErrors !! 4) `shouldBe` InvalidSearchFilter 400 ""
        it "should parse CCProcessorError correctly" $
            interpretError (exampleRestletErrors !! 5) `shouldBe` CCProcessorError 400 ""
        it "should parse ResourceConflict correctly" $
            interpretError (exampleRestletErrors !! 6) `shouldBe` ResourceConflict 400 ""
        it "should parse fall back to UnknownError correctly" $
            interpretError (exampleRestletErrors !! 7) `shouldBe` UnknownError 400 "IDK_LOL" (C.pack "{\"error\": {\"message\": \"IDK_LOL\"}}")

-- | Tries to match header key values
suite :: Spec
suite = do
    describe "NsAction" $
        it "can marshal all NsAction types as valid JSON" $
            case mapMaybe (decode . encode) exampleNsActions of
                (x :: [Value]) -> length x `shouldBe` length exampleNsActions
                y              -> error $ show y ++ " should be a list of valid JSON objects"
    describe "NsRestletConfig" $ do
        it "can create identical NsRestletConfig object from dissimilar origins" $ do
            let a = ("http://example.com:8080", 12345 :: Int, 1000 :: Int, "foo@example.com", "bar")
            let b = NsRestletConfig (fromJust . parseURI $ "http://example.com:8080")
                                    (12345 :: Integer)
                                    (1000 :: Integer)
                                    "foo@example.com"
                                    "bar"
                                    Nothing
                                    Nothing
            toNsRestletConfig a `shouldBe` b
        it "does not accept invalid URLs" $ do
            let a = ("obviously incorrect ha ha", 12345 :: Int, 1000 :: Int, "foo@example.com", "bar")
            evaluate (show $ toNsRestletConfig a) `shouldThrow` errorCall "Maybe.fromJust: Nothing"

    describe "Restlet Requests" $
        it "creates the expected headers" $ do
            let (nsid, nsrole, nsident, nspwd) = (12345 :: Int, 1000 :: Int, "foo@example.com", "bar")

            let a = ("http://example.com:8080", nsid, nsrole, nsident, nspwd)
            let p = "/test"
            r <- makeRequest (toNsRestletConfig a) p
            qPath r `shouldBe` C.pack p
            let hdrs = retrieveHeaders . qHeaders $ r
            liftIO $ putStrLn $ show hdrs
            tryBsMatch "Content-Type" "application/json" hdrs
            tryBsMatch "Accept" "application/json" hdrs
            tryBsMatch "User-Agent" "NsRestlet" hdrs
            tryBsMatch "Authorization" (Prelude.concat ["NLAuth nlauth_account=",
                                                        show nsid,
                                                        ",nlauth_email=",
                                                        nsident,
                                                        ",nlauth_role=",
                                                        show nsrole,
                                                        ",nlauth_signature=",
                                                        nspwd]) hdrs

    describe "Restlet Errors" $ do
        it "should parse GibberishError correctly" $
            interpretError (head exampleRestletErrors) `shouldBe` GibberishError 400 "Unparseable response, expecting JSON." (C.pack "Bad Request")
        it "should parse BeginChunking correctly" $
            interpretError (exampleRestletErrors !! 1) `shouldBe` BeginChunking 400
        it "should parse EndChunking correctly" $
            interpretError (exampleRestletErrors !! 2) `shouldBe` EndChunking 400
        it "should parse NotFound correctly" $
            interpretError (exampleRestletErrors !! 3) `shouldBe` NotFound 404 ""
        it "should parse InvalidSearchFilter correctly" $
            interpretError (exampleRestletErrors !! 4) `shouldBe` InvalidSearchFilter 400 ""
        it "should parse CCProcessorError correctly" $
            interpretError (exampleRestletErrors !! 5) `shouldBe` CCProcessorError 400 ""
        it "should parse ResourceConflict correctly" $
            interpretError (exampleRestletErrors !! 6) `shouldBe` ResourceConflict 400 ""
        it "should parse fall back to UnknownError correctly" $
            interpretError (exampleRestletErrors !! 7) `shouldBe` UnknownError 400 "IDK_LOL" (C.pack "{\"error\": {\"message\": \"IDK_LOL\"}}")

-- | Tries to match header key values
suite = do
    describe "NsAction" $
        it "can marshal all NsAction types as valid JSON" $
            case mapMaybe (decode . encode) exampleNsActions of
                (x :: [Value]) -> length x `shouldBe` length exampleNsActions
                y              -> error $ show y ++ " should be a list of valid JSON objects"
    describe "NsRestletConfig" $ do
        it "can create identical NsRestletConfig object from dissimilar origins" $ do
            let a = ("http://example.com:8080", 12345 :: Int, 1000 :: Int, "foo@example.com", "bar")
            let b = NsRestletConfig (fromJust . parseURI $ "http://example.com:8080")
                                    (12345 :: Integer)
                                    (1000 :: Integer)
                                    "foo@example.com"
                                    "bar"
                                    Nothing
                                    Nothing
            toNsRestletConfig a `shouldBe` b
        it "does not accept invalid URLs" $ do
            let a = ("obviously incorrect ha ha", 12345 :: Int, 1000 :: Int, "foo@example.com", "bar")
            evaluate (show $ toNsRestletConfig a) `shouldThrow` errorCall "Maybe.fromJust: Nothing"

    describe "Restlet Requests" $
        it "creates the expected headers" $ do
            let (nsid, nsrole, nsident, nspwd) = (12345 :: Int, 1000 :: Int, "foo@example.com", "bar")

            let a = ("http://example.com:8080", nsid, nsrole, nsident, nspwd)
            let p = "/test"
            r <- makeRequest (toNsRestletConfig a) p
            qPath r `shouldBe` C.pack p
            let hdrs = retrieveHeaders . qHeaders $ r
            liftIO $ putStrLn $ show hdrs
            tryBsMatch "Content-Type" "application/json" hdrs
            tryBsMatch "Accept" "application/json" hdrs
            tryBsMatch "User-Agent" "NsRestlet" hdrs
            tryBsMatch "Authorization" (Prelude.concat ["NLAuth nlauth_account=",
                                                        show nsid,
                                                        ",nlauth_email=",
                                                        nsident,
                                                        ",nlauth_role=",
                                                        show nsrole,
                                                        ",nlauth_signature=",
                                                        nspwd]) hdrs

    describe "Restlet Errors" $ do
        it "should parse GibberishError correctly" $
            interpretError (head exampleRestletErrors) `shouldBe` GibberishError 400 "Unparseable response, expecting JSON." (C.pack "Bad Request")
        it "should parse BeginChunking correctly" $
            interpretError (exampleRestletErrors !! 1) `shouldBe` BeginChunking 400
        it "should parse EndChunking correctly" $
            interpretError (exampleRestletErrors !! 2) `shouldBe` EndChunking 400
        it "should parse NotFound correctly" $
            interpretError (exampleRestletErrors !! 3) `shouldBe` NotFound 404 ""
        it "should parse InvalidSearchFilter correctly" $
            interpretError (exampleRestletErrors !! 4) `shouldBe` InvalidSearchFilter 400 ""
        it "should parse CCProcessorError correctly" $
            interpretError (exampleRestletErrors !! 5) `shouldBe` CCProcessorError 400 ""
        it "should parse ResourceConflict correctly" $
            interpretError (exampleRestletErrors !! 6) `shouldBe` ResourceConflict 400 ""
        it "should parse fall back to UnknownError correctly" $
            interpretError (exampleRestletErrors !! 7) `shouldBe` UnknownError 400 "IDK_LOL" (C.pack "{\"error\": {\"message\": \"IDK_LOL\"}}")

-- | Tries to match header key values