Add fullPath function for inspecting and logging full resolved request path (#81)

This commit is contained in:
Connor Prussin 2017-09-29 09:52:21 -07:00 committed by GitHub
parent a5e29897b2
commit f38c5987ee
4 changed files with 51 additions and 3 deletions

View File

@ -21,10 +21,12 @@ loggingMiddleware :: forall e.
HTTPure.Request ->
HTTPure.ResponseM (console :: Console.CONSOLE | e)
loggingMiddleware router request = do
EffClass.liftEff $ Console.log $ "Request starting for " <> show request.path
EffClass.liftEff $ Console.log $ "Request starting for " <> path
response <- router request
EffClass.liftEff $ Console.log $ "Request ending for " <> show request.path
EffClass.liftEff $ Console.log $ "Request ending for " <> path
pure response
where
path = HTTPure.fullPath request
-- | A middleware that adds the X-Middleware header to the response, if it
-- | wasn't already in the response

View File

@ -12,7 +12,7 @@ import HTTPure.Headers (Headers, empty, header, headers)
import HTTPure.Lookup (lookup, (!!))
import HTTPure.Method (Method(..))
import HTTPure.Path (Path)
import HTTPure.Request (Request)
import HTTPure.Request (Request, fullPath)
import HTTPure.Response
( ResponseM
, response, response'

View File

@ -1,11 +1,14 @@
module HTTPure.Request
( Request
, fromHTTPRequest
, fullPath
) where
import Prelude
import Control.Monad.Aff as Aff
import Data.String as String
import Data.StrMap as StrMap
import Node.HTTP as HTTP
import HTTPure.Body as Body
@ -25,6 +28,18 @@ type Request =
, body :: Body.Body
}
-- | Return the full resolved path, including query parameters. This may not
-- | match the requested path--for instance, if there are empty path segments in
-- | the request--but it is equivalent.
fullPath :: Request -> String
fullPath request = "/" <> path <> questionMark <> queryParams
where
path = String.joinWith "/" request.path
questionMark = if StrMap.isEmpty request.query then "" else "?"
queryParams = String.joinWith "&" queryParamsArr
queryParamsArr = StrMap.toArrayWithKey stringifyQueryParam request.query
stringifyQueryParam key value = key <> "=" <> value
-- | Given an HTTP `Request` object, this method will convert it to an HTTPure
-- | `Request` object.
fromHTTPRequest :: forall e.

View File

@ -36,6 +36,37 @@ fromHTTPRequestSpec = Spec.describe "fromHTTPRequest" do
SpecHelpers.mockRequest "POST" "/test?a=b" "body" mockHeaders
mockRequest = mockHTTPRequest >>= Request.fromHTTPRequest
fullPathSpec :: SpecHelpers.Test
fullPathSpec = Spec.describe "fullPath" do
Spec.describe "without query parameters" do
Spec.it "is correct" do
mock <- mockRequest "/foo/bar"
Request.fullPath mock ?= "/foo/bar"
Spec.describe "with empty path segments" do
Spec.it "strips the empty segments" do
mock <- mockRequest "//foo////bar/"
Request.fullPath mock ?= "/foo/bar"
Spec.describe "with only query parameters" do
Spec.it "is correct" do
mock <- mockRequest "?a=b&c=d"
Request.fullPath mock ?= "/?a=b&c=d"
Spec.describe "with only empty query parameters" do
Spec.it "is has the default value of 'true' for the empty parameters" do
mock <- mockRequest "?a"
Request.fullPath mock ?= "/?a=true"
Spec.describe "with empty query parameters" do
Spec.it "strips out the empty arameters" do
mock <- mockRequest "?a=b&&&"
Request.fullPath mock ?= "/?a=b"
Spec.describe "with a mix of segments and query parameters" do
Spec.it "is correct" do
mock <- mockRequest "/foo///bar/?&a=b&&c"
Request.fullPath mock ?= "/foo/bar?a=b&c=true"
where
mockHTTPRequest path = SpecHelpers.mockRequest "POST" path "body" []
mockRequest path = mockHTTPRequest path >>= Request.fromHTTPRequest
requestSpec :: SpecHelpers.Test
requestSpec = Spec.describe "Request" do
fromHTTPRequestSpec
fullPathSpec