2017-10-26 21:19:30 +00:00
|
|
|
module Test.HTTPure.TestHelpers where
|
2017-07-10 10:17:13 +00:00
|
|
|
|
2017-07-18 05:31:46 +00:00
|
|
|
import Prelude
|
2022-05-04 21:02:29 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
import Data.Array (fromFoldable) as Array
|
|
|
|
import Data.Either (Either(Right))
|
|
|
|
import Data.List (List(Nil, Cons), reverse)
|
|
|
|
import Data.Maybe (fromMaybe)
|
2017-07-23 06:13:47 +00:00
|
|
|
import Data.Options ((:=))
|
2021-11-19 06:16:35 +00:00
|
|
|
import Data.String (toLower)
|
|
|
|
import Data.Tuple (Tuple)
|
2022-05-04 21:02:29 +00:00
|
|
|
import Effect (Effect)
|
|
|
|
import Effect.Aff (Aff, makeAff, nonCanceler)
|
|
|
|
import Effect.Class (liftEffect)
|
|
|
|
import Effect.Ref (modify_, new, read)
|
2021-11-19 06:16:35 +00:00
|
|
|
import Foreign.Object (Object, lookup)
|
2022-05-04 21:02:29 +00:00
|
|
|
import Foreign.Object (fromFoldable) as Object
|
|
|
|
import Node.Buffer (Buffer, concat, create, fromString)
|
2021-11-19 06:16:35 +00:00
|
|
|
import Node.Buffer (toString) as Buffer
|
|
|
|
import Node.Encoding (Encoding(UTF8))
|
|
|
|
import Node.HTTP (Request)
|
2022-05-04 21:02:29 +00:00
|
|
|
import Node.HTTP (Response) as HTTP
|
2021-11-19 06:16:35 +00:00
|
|
|
import Node.HTTP.Client
|
|
|
|
( RequestHeaders(RequestHeaders)
|
2022-05-04 21:02:29 +00:00
|
|
|
, headers
|
2021-11-19 06:16:35 +00:00
|
|
|
, hostname
|
2022-05-04 21:02:29 +00:00
|
|
|
, method
|
2021-11-19 06:16:35 +00:00
|
|
|
, path
|
2022-05-04 21:02:29 +00:00
|
|
|
, port
|
|
|
|
, protocol
|
2021-11-19 06:16:35 +00:00
|
|
|
, rejectUnauthorized
|
2022-05-04 21:02:29 +00:00
|
|
|
, requestAsStream
|
2021-11-19 06:16:35 +00:00
|
|
|
, responseAsStream
|
2022-05-04 21:02:29 +00:00
|
|
|
, responseHeaders
|
|
|
|
, statusCode
|
2021-11-19 06:16:35 +00:00
|
|
|
)
|
2022-05-04 21:02:29 +00:00
|
|
|
import Node.HTTP.Client (Response, request) as HTTPClient
|
|
|
|
import Node.Stream (Readable, end, onData, onEnd, write)
|
2021-11-19 06:16:35 +00:00
|
|
|
import Test.Spec (Spec)
|
|
|
|
import Test.Spec.Assertions (shouldEqual)
|
|
|
|
import Unsafe.Coerce (unsafeCoerce)
|
|
|
|
|
|
|
|
infix 1 shouldEqual as ?=
|
2017-07-19 05:21:36 +00:00
|
|
|
|
2017-07-10 10:17:13 +00:00
|
|
|
-- | The type for integration tests.
|
2021-11-19 06:16:35 +00:00
|
|
|
type Test = Spec Unit
|
2017-07-10 10:17:13 +00:00
|
|
|
|
|
|
|
-- | The type for the entire test suite.
|
2021-11-19 06:16:35 +00:00
|
|
|
type TestSuite = Effect Unit
|
2017-07-10 10:17:13 +00:00
|
|
|
|
|
|
|
-- | Given a URL, a failure handler, and a success handler, create an HTTP
|
|
|
|
-- | client request.
|
2021-03-22 19:02:36 +00:00
|
|
|
request ::
|
|
|
|
Boolean ->
|
|
|
|
Int ->
|
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-03-22 19:02:36 +00:00
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Buffer ->
|
|
|
|
Aff HTTPClient.Response
|
|
|
|
request secure port' method' headers' path' body =
|
|
|
|
makeAff \done -> do
|
|
|
|
req <- HTTPClient.request options $ Right >>> done
|
2022-05-04 21:02:29 +00:00
|
|
|
let stream = requestAsStream req
|
2021-06-25 17:58:16 +00:00
|
|
|
void
|
2021-11-19 06:16:35 +00:00
|
|
|
$ write stream body
|
2022-05-04 21:02:29 +00:00
|
|
|
$ const
|
2021-11-19 06:16:35 +00:00
|
|
|
$ end stream
|
2022-05-04 21:02:29 +00:00
|
|
|
$ const
|
2021-06-25 17:58:16 +00:00
|
|
|
$ pure unit
|
2021-11-19 06:16:35 +00:00
|
|
|
pure nonCanceler
|
2017-07-18 17:09:03 +00:00
|
|
|
where
|
2021-03-22 19:02:36 +00:00
|
|
|
options =
|
2021-11-19 06:16:35 +00:00
|
|
|
protocol := (if secure then "https:" else "http:")
|
|
|
|
<> method := method'
|
|
|
|
<> hostname := "localhost"
|
|
|
|
<> port := port'
|
|
|
|
<> path := path'
|
|
|
|
<> headers := RequestHeaders headers'
|
|
|
|
<> rejectUnauthorized := false
|
2017-07-10 10:17:13 +00:00
|
|
|
|
2021-11-16 04:02:36 +00:00
|
|
|
-- | Same as `request` but without.
|
|
|
|
request' ::
|
|
|
|
Boolean ->
|
|
|
|
Int ->
|
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-11-16 04:02:36 +00:00
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Aff HTTPClient.Response
|
2021-11-16 04:02:36 +00:00
|
|
|
request' secure port method headers path =
|
2021-11-19 06:16:35 +00:00
|
|
|
liftEffect (create 0)
|
2021-11-16 04:02:36 +00:00
|
|
|
>>= request secure port method headers path
|
|
|
|
|
|
|
|
-- | Same as `request` but with a `String` body.
|
|
|
|
requestString ::
|
|
|
|
Boolean ->
|
|
|
|
Int ->
|
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-11-16 04:02:36 +00:00
|
|
|
String ->
|
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Aff HTTPClient.Response
|
2021-11-16 04:02:36 +00:00
|
|
|
requestString secure port method headers path body = do
|
2021-11-19 06:16:35 +00:00
|
|
|
liftEffect (fromString body UTF8)
|
2021-11-16 04:02:36 +00:00
|
|
|
>>= request secure port method headers path
|
|
|
|
|
2018-08-20 02:50:07 +00:00
|
|
|
-- | Convert a request to an Aff containing the `Buffer with the response body.
|
2021-11-19 06:16:35 +00:00
|
|
|
toBuffer :: HTTPClient.Response -> Aff Buffer
|
2021-03-22 19:02:36 +00:00
|
|
|
toBuffer response =
|
2021-11-19 06:16:35 +00:00
|
|
|
makeAff \done -> do
|
2021-03-22 19:02:36 +00:00
|
|
|
let
|
2021-11-19 06:16:35 +00:00
|
|
|
stream = responseAsStream response
|
|
|
|
chunks <- new Nil
|
|
|
|
onData stream $ \new -> modify_ (Cons new) chunks
|
|
|
|
onEnd stream $ read chunks
|
|
|
|
>>= reverse
|
2021-11-06 19:37:31 +00:00
|
|
|
>>> Array.fromFoldable
|
2021-11-19 06:16:35 +00:00
|
|
|
>>> concat
|
|
|
|
>>= Right
|
2021-11-06 19:37:31 +00:00
|
|
|
>>> done
|
2021-11-19 06:16:35 +00:00
|
|
|
pure nonCanceler
|
2017-07-10 10:17:13 +00:00
|
|
|
|
|
|
|
-- | Convert a request to an Aff containing the string with the response body.
|
2021-11-19 06:16:35 +00:00
|
|
|
toString :: HTTPClient.Response -> Aff String
|
2018-08-20 02:50:07 +00:00
|
|
|
toString resp = do
|
|
|
|
buf <- toBuffer resp
|
2021-11-19 06:16:35 +00:00
|
|
|
liftEffect $ Buffer.toString UTF8 buf
|
2017-07-10 10:17:13 +00:00
|
|
|
|
|
|
|
-- | Run an HTTP GET with the given url and return an Aff that contains the
|
|
|
|
-- | string with the response body.
|
2021-03-22 19:02:36 +00:00
|
|
|
get ::
|
|
|
|
Int ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-03-22 19:02:36 +00:00
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Aff String
|
2021-11-16 04:02:36 +00:00
|
|
|
get port headers path = request' false port "GET" headers path >>= toString
|
2017-07-23 19:17:02 +00:00
|
|
|
|
2018-08-20 02:50:07 +00:00
|
|
|
-- | Like `get` but return a response body in a `Buffer`
|
2021-03-22 19:02:36 +00:00
|
|
|
getBinary ::
|
|
|
|
Int ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-03-22 19:02:36 +00:00
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Aff Buffer
|
2021-11-16 04:02:36 +00:00
|
|
|
getBinary port headers path = request' false port "GET" headers path >>= toBuffer
|
2018-08-20 02:50:07 +00:00
|
|
|
|
2017-07-23 19:17:02 +00:00
|
|
|
-- | Run an HTTPS GET with the given url and return an Aff that contains the
|
|
|
|
-- | string with the response body.
|
2021-03-22 19:02:36 +00:00
|
|
|
get' ::
|
|
|
|
Int ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-03-22 19:02:36 +00:00
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Aff String
|
2021-11-16 04:02:36 +00:00
|
|
|
get' port headers path = request' true port "GET" headers path >>= toString
|
2017-07-18 17:09:03 +00:00
|
|
|
|
|
|
|
-- | Run an HTTP POST with the given url and body and return an Aff that
|
|
|
|
-- | contains the string with the response body.
|
2021-03-22 19:02:36 +00:00
|
|
|
post ::
|
|
|
|
Int ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-03-22 19:02:36 +00:00
|
|
|
String ->
|
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Aff String
|
2021-11-16 04:02:36 +00:00
|
|
|
post port headers path = requestString false port "POST" headers path >=> toString
|
|
|
|
|
|
|
|
-- | Run an HTTP POST with the given url and binary buffer body and return an
|
|
|
|
-- | Aff that contains the string with the response body.
|
|
|
|
postBinary ::
|
|
|
|
Int ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-11-16 04:02:36 +00:00
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Buffer ->
|
|
|
|
Aff String
|
2021-11-16 04:02:36 +00:00
|
|
|
postBinary port headers path = request false port "POST" headers path >=> toString
|
2017-07-17 23:42:13 +00:00
|
|
|
|
|
|
|
-- | Convert a request to an Aff containing the string with the given header
|
|
|
|
-- | value.
|
|
|
|
extractHeader :: String -> HTTPClient.Response -> String
|
2021-11-19 06:16:35 +00:00
|
|
|
extractHeader header = unmaybe <<< lookup' <<< responseHeaders
|
2017-07-17 23:42:13 +00:00
|
|
|
where
|
2021-11-19 06:16:35 +00:00
|
|
|
unmaybe = fromMaybe ""
|
2021-03-22 19:02:36 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
lookup' = lookup $ toLower header
|
2017-07-17 23:42:13 +00:00
|
|
|
|
|
|
|
-- | Run an HTTP GET with the given url and return an Aff that contains the
|
|
|
|
-- | string with the header value for the given header.
|
2021-03-22 19:02:36 +00:00
|
|
|
getHeader ::
|
|
|
|
Int ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-03-22 19:02:36 +00:00
|
|
|
String ->
|
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Aff String
|
2021-11-16 04:02:36 +00:00
|
|
|
getHeader port headers path header = extractHeader header <$> request' false port "GET" headers path
|
2021-03-22 19:02:36 +00:00
|
|
|
|
|
|
|
getStatus ::
|
|
|
|
Int ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
2021-03-22 19:02:36 +00:00
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Aff Int
|
|
|
|
getStatus port headers path = statusCode <$> request' false port "GET" headers path
|
2018-10-09 17:37:23 +00:00
|
|
|
|
2017-07-10 10:17:13 +00:00
|
|
|
-- | Mock an HTTP Request object
|
2017-09-26 06:08:07 +00:00
|
|
|
foreign import mockRequestImpl ::
|
2017-07-18 05:25:14 +00:00
|
|
|
String ->
|
|
|
|
String ->
|
|
|
|
String ->
|
2019-04-25 17:13:04 +00:00
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Object String ->
|
|
|
|
Effect Request
|
2017-07-10 10:17:13 +00:00
|
|
|
|
2017-09-26 06:08:07 +00:00
|
|
|
-- | Mock an HTTP Request object
|
2021-03-22 19:02:36 +00:00
|
|
|
mockRequest ::
|
|
|
|
String ->
|
|
|
|
String ->
|
|
|
|
String ->
|
|
|
|
String ->
|
2021-11-19 06:16:35 +00:00
|
|
|
Array (Tuple String String) ->
|
|
|
|
Aff Request
|
|
|
|
mockRequest httpVersion method url body = liftEffect <<< mockRequestImpl httpVersion method url body <<< Object.fromFoldable
|
2017-07-17 23:42:13 +00:00
|
|
|
|
|
|
|
-- | Mock an HTTP Response object
|
2021-11-19 06:16:35 +00:00
|
|
|
foreign import mockResponse :: Effect HTTP.Response
|
2017-07-17 23:42:13 +00:00
|
|
|
|
|
|
|
-- | Get the current body from an HTTP Response object (note this will only work
|
|
|
|
-- | with an object returned from mockResponse).
|
|
|
|
getResponseBody :: HTTP.Response -> String
|
2021-11-19 06:16:35 +00:00
|
|
|
getResponseBody = _.body <<< unsafeCoerce
|
2017-07-17 23:42:13 +00:00
|
|
|
|
|
|
|
-- | Get the currently set status from an HTTP Response object.
|
|
|
|
getResponseStatus :: HTTP.Response -> Int
|
2021-11-19 06:16:35 +00:00
|
|
|
getResponseStatus = _.statusCode <<< unsafeCoerce
|
2017-07-17 23:42:13 +00:00
|
|
|
|
|
|
|
-- | Get all current headers on the HTTP Response object.
|
2021-11-19 06:16:35 +00:00
|
|
|
getResponseHeaders :: HTTP.Response -> Object String
|
|
|
|
getResponseHeaders = unsafeCoerce <<< _.headers <<< unsafeCoerce
|
2017-07-17 23:42:13 +00:00
|
|
|
|
|
|
|
-- | Get the current value for the header on the HTTP Response object.
|
|
|
|
getResponseHeader :: String -> HTTP.Response -> String
|
2021-11-19 06:16:35 +00:00
|
|
|
getResponseHeader header = fromMaybe "" <<< lookup header <<< getResponseHeaders
|
2018-08-30 22:01:49 +00:00
|
|
|
|
|
|
|
-- | Create a stream out of a string.
|
2021-11-19 06:16:35 +00:00
|
|
|
foreign import stringToStream :: String -> Readable ()
|