Skip to content

Commit

Permalink
Merge branch 'release/0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
smallhadroncollider committed Jan 17, 2019
2 parents 7016350 + 30e3a38 commit 4c43152
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.stack-work/
brok.cabal
releases/
/.brokdb
*~
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: brok
version: 0.1.0.0
version: 0.1.1.0
github: "smallhadroncollider/brok"
license: BSD3
author: "Small Hadron Collider"
Expand Down
16 changes: 9 additions & 7 deletions src/Brok/IO/Http.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module Brok.IO.Http
import ClassyPrelude

import Control.Concurrent (threadDelay)
import Network.HTTP.Simple (HttpException, Request, addRequestHeader, getResponseStatusCode,
httpNoBody, parseRequest, setRequestMethod)
import Network.HTTP.Simple (HttpException, HttpException (..), Request, addRequestHeader,
getResponseStatusCode, httpNoBody, parseRequest, setRequestMethod)

import Brok.IO.CLI (replace)
import Brok.Types.Link
Expand All @@ -20,10 +20,11 @@ setHeaders :: Request -> Request
setHeaders = addRequestHeader "User-Agent" "smallhadroncollider/brok"

makeRequest :: Integer -> ByteString -> URL -> IO StatusCode
makeRequest delay method url = do
request <- setHeaders . setRequestMethod method <$> parseRequest (unpack url)
threadDelay (fromIntegral delay * 1000) -- wait for a little while
(getResponseStatusCode <$>) <$> try (httpNoBody request)
makeRequest delay method url =
try $ do
request <- setHeaders . setRequestMethod method <$> parseRequest (unpack url)
threadDelay (fromIntegral delay * 1000) -- wait for a little while
getResponseStatusCode <$> httpNoBody request

tryWithGet :: Integer -> URL -> StatusCode -> IO StatusCode
tryWithGet delay url (Right code)
Expand All @@ -41,7 +42,8 @@ codeToResponse :: Link -> StatusCode -> Link
codeToResponse lnk (Right code)
| code >= 200 && code < 300 = working lnk code
| otherwise = broken lnk code
codeToResponse lnk (Left _) = failure lnk
codeToResponse lnk (Left (HttpExceptionRequest _ _)) = failure lnk
codeToResponse lnk (Left (InvalidUrlException _ _)) = invalid lnk

check :: Integer -> Link -> IO Link
check delay lnk = codeToResponse lnk <$> fetch delay (getURL lnk)
1 change: 1 addition & 0 deletions src/Brok/IO/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ linkOutput (Link url Cached) = splitOut "- OK (cached)" url
linkOutput (Link url (Working code)) = splitOut ("- OK (" ++ tshow code ++ ")") url
linkOutput (Link url (Broken code)) = splitErr ("- Failed (" ++ tshow code ++ ")") url
linkOutput (Link url ConnectionFailure) = splitErr "- Could not connect" url
linkOutput (Link url InvalidURL) = splitErr "- Invalid URL" url

statusError :: Link -> Bool
statusError (Link _ (Working _)) = False
Expand Down
5 changes: 5 additions & 0 deletions src/Brok/Types/Link.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data LinkType
| Ignored
| Working Int
| Broken Int
| InvalidURL
| ConnectionFailure
deriving (Show, Eq)

Expand All @@ -38,6 +39,10 @@ failure :: Link -> Link
failure (Link url BareLink) = Link url ConnectionFailure
failure lnk = lnk

invalid :: Link -> Link
invalid (Link url BareLink) = Link url InvalidURL
invalid lnk = lnk

findLink :: LinkType -> (URL -> URL -> Bool) -> [URL] -> Link -> Link
findLink lType fn urls (Link url BareLink) =
case find (fn url) urls of
Expand Down
3 changes: 3 additions & 0 deletions taskell.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
- Should detect if internet connection is down
- Cache length option should accept units: s, m, h, d - default to s
- Should be able to detect links without http:// or https:// prefixes
- More detailed HttpException errors

## Bugs

- Can't parse links containing single quote marks
- Fetching message sometimes goes to more than one line, so next line doesn't replace it (saveCursor/restoreCursor?)
- Edge case: shouldn't delay if only a single URL being checked

## In Progress
Expand All @@ -30,3 +32,4 @@
- --help command
- Use attoparsec
> Already used by HTTP-conduit, so may as well use it
- Fixed InvalidURLException crash
35 changes: 23 additions & 12 deletions test/IO/HttpTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@

module IO.HttpTest where

-- import ClassyPrelude
--
import ClassyPrelude

import Test.Tasty

-- import Test.Tasty.HUnit
-- import Brok.IO.Http (check)
-- import Brok.Types.Link (Link (Link), LinkType (Working), urlToLink)
--
--
import Brok.IO.Http (check)
import Brok.Types.Link (Link (Link), LinkType (..), urlToLink)
import Test.Tasty.HUnit

test_http :: TestTree
test_http = testGroup "Brok.IO.Http" []
{-
test_http =
testGroup
"Brok.IO.Http"
[ testCase "Medium (409 with HEAD)" $ do
result <-
check $
check 0 $
urlToLink
"https://medium.freecodecamp.org/understanding-redux-the-worlds-easiest-guide-to-beginning-redux-c695f45546f6"
assertEqual
Expand All @@ -30,7 +28,7 @@ test_http = testGroup "Brok.IO.Http" []
result
, testCase "TutsPlus (Requires User-Agent Header)" $ do
result <-
check $
check 0 $
urlToLink
"https://code.tutsplus.com/tutorials/stateful-vs-stateless-functional-components-in-react--cms-29541"
assertEqual
Expand All @@ -39,5 +37,18 @@ test_http = testGroup "Brok.IO.Http" []
"https://code.tutsplus.com/tutorials/stateful-vs-stateless-functional-components-in-react--cms-29541"
(Working 200))
result
, testCase "Non-existant site" $ do
result <- check 0 $ urlToLink "http://askdjfhaksjdhfkajsdfh.com"
assertEqual
"Returns a 200"
(Link "http://askdjfhaksjdhfkajsdfh.com" ConnectionFailure)
result
, testCase "Invalid URL" $ do
result <-
check 0 $
urlToLink "http://user:password&#64;securesite.com/secret-file.json&quot;"
assertEqual
"Returns a 200"
(Link "http://user:password&#64;securesite.com/secret-file.json&quot;" InvalidURL)
result
]
-}

0 comments on commit 4c43152

Please sign in to comment.