{-# LANGUAGE OverloadedStrings #-}

module Crawler.Fixtures
    ( homePage
    , aboutPage
    ) where

import Data.Monoid
  ( (<>)
  , mempty
  )
import Network.HTTP.Client (HttpException(..))
import Network.HTTP.Types  (status404)
import Data.ByteString     (ByteString)
import Crawler             (GetRequest(..))
import Control.Monad.Identity (Identity)

homePage :: ByteString
homePage =
  "<html>" <> 
  "  <head>" <>
  "  </head>" <>
  "  <body>" <> 
  "    <img src=\"http://www.example.com/hello.jpg\"/>" <>
  "    <a href=\"http://www.google.com\">google</a>" <> 
  "    <a href=\"/missing_page\">missing page</a>" <> 
  "    <a href=\"http://example.com/about\">about</a>" <> 
  "    <a href=\"http://www.example.com#hello\">hello</a>" <> 
  "  </body>" <> 
  "</html>"

aboutPage :: ByteString
aboutPage =
  "<html>" <> 
  "  <head>" <>
  "    <script src=\"something.js\"/>" <>
  "  </head>" <>
  "  <body>" <> 
  "    <img src=\"about.jpg\"/>" <>
  "    <iframe src=\"http:/www.google.com\"/>" <>
  "    <a href=\"http://example.com/\">home</a>" <> 
  "  </body>" <> 
  "</html>"

instance GetRequest Identity where
  getRequest uri = case (show uri) of
    "http://example.com/about/"         -> return $ (Right aboutPage)
    "http://www.example.com/"           -> return $ Right homePage
    "http://example.com/"               -> return $ Right homePage
    "http://example.com#hello/"         -> return $ Right homePage
    "http://example.com/missing_page/"  -> return $ Left (StatusCodeException status404 [] mempty)
    x                                   -> error  $ "missing uri in test data, was given: " ++ x
