module Test.HTTPurple.IntegrationSpec where import Prelude import Data.Newtype (wrap) import Effect.Aff (delay) import Effect.Aff.Class (liftAff) import Effect.Class (liftEffect) import Examples.AsyncResponse.Main as AsyncResponse import Examples.BinaryRequest.Main as BinaryRequest import Examples.BinaryResponse.Main as BinaryResponse import Examples.Chunked.Main as Chunked import Examples.CustomStack.Main as CustomStack import Examples.ExtensibleMiddleware.Main as ExtensibleMiddleware import Examples.Headers.Main as Headers import Examples.HelloWorld.Main as HelloWorld import Examples.JsonParsing.Main as JsonParsing import Examples.Middleware.Main as Middleware import Examples.MultiRoute.Main as MultiRoute import Examples.NodeMiddleware.Main as NodeMiddleware import Examples.PathSegments.Main as PathSegments import Examples.Post.Main as Post import Examples.QueryParameters.Main as QueryParameters import Examples.SSL.Main as SSL import Foreign.Object (empty, singleton) import Foreign.Object as Object import Node.Buffer (toArray) import Node.FS.Aff (readFile) import Test.HTTPurple.TestHelpers (Test, get, get', getBinary, getHeader, post, postBinary, (?=)) import Test.Spec (describe, it) import Test.Spec.Assertions.String (shouldStartWith) asyncResponseSpec :: Test asyncResponseSpec = it "runs the async response example" do close <- liftEffect AsyncResponse.main liftAff $ delay $ wrap 200.0 response <- get 10000 empty "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 response ?= "hello world!" binaryRequestSpec :: Test binaryRequestSpec = it "runs the binary request example" do close <- liftEffect BinaryRequest.main liftAff $ delay $ wrap 200.0 binaryBuf <- readFile BinaryResponse.filePath response <- postBinary 10000 empty "/" binaryBuf liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 response ?= "d5e776724dd545d8b54123b46362a553d10257cee688ef1be62166c984b34405" binaryResponseSpec :: Test binaryResponseSpec = it "runs the binary response example" do close <- liftEffect BinaryResponse.main liftAff $ delay $ wrap 200.0 responseBuf <- getBinary 10000 empty "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 binaryBuf <- readFile BinaryResponse.filePath expected <- liftEffect $ toArray binaryBuf response <- liftEffect $ toArray responseBuf response ?= expected chunkedSpec :: Test chunkedSpec = it "runs the chunked example" do close <- liftEffect Chunked.main liftAff $ delay $ wrap 200.0 response <- get 10000 empty "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 -- TODO this isn't a great way to validate this, we need a way of inspecting -- each individual chunk instead of just looking at the entire response response ?= "hello \nworld!\n" customStackSpec :: Test customStackSpec = it "runs the custom stack example" do close <- liftEffect CustomStack.main liftAff $ delay $ wrap 200.0 response <- get 10000 empty "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 response ?= "hello, joe" headersSpec :: Test headersSpec = it "runs the headers example" do close <- liftEffect Headers.main liftAff $ delay $ wrap 200.0 header <- getHeader 10000 empty "/" "X-Example" response <- get 10000 (singleton "X-Input" "test") "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 header ?= "hello world!" response ?= "test" helloWorldSpec :: Test helloWorldSpec = it "runs the hello world example" do close <- liftEffect HelloWorld.main liftAff $ delay $ wrap 200.0 response <- get 10000 empty "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 response ?= "hello world!" jsonParsingSpec :: Test jsonParsingSpec = it "runs the hello world example" do close <- liftEffect JsonParsing.main liftAff $ delay $ wrap 200.0 response <- post 10000 empty "/" "{\"name\":\"world\"}" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 response ?= "{\"hello\": \"world\" }" middlewareSpec :: Test middlewareSpec = it "runs the middleware example" do close <- liftEffect Middleware.main liftAff $ delay $ wrap 200.0 header <- getHeader 10000 empty "/" "X-Middleware" body <- get 10000 empty "/" header' <- getHeader 10000 empty "/middleware" "X-Middleware" body' <- get 10000 empty "/middleware" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 header ?= "router" body ?= "hello" header' ?= "middleware" body' ?= "Middleware!" multiRouteSpec :: Test multiRouteSpec = it "runs the multi route example" do close <- liftEffect MultiRoute.main liftAff $ delay $ wrap 200.0 hello <- get 10000 empty "/hello" goodbye <- get 10000 empty "/goodbye" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 hello ?= "hello" goodbye ?= "goodbye" pathSegmentsSpec :: Test pathSegmentsSpec = it "runs the path segments example" do close <- liftEffect PathSegments.main liftAff $ delay $ wrap 200.0 foo <- get 10000 empty "/segment/foo" somebars <- get 10000 empty "/some/bars" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 foo ?= "foo" somebars ?= "[\"some\",\"bars\"]" postSpec :: Test postSpec = it "runs the post example" do close <- liftEffect Post.main liftAff $ delay $ wrap 200.0 response <- post 10000 empty "/" "test" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 response ?= "test" queryParametersSpec :: Test queryParametersSpec = it "runs the query parameters example" do close <- liftEffect QueryParameters.main liftAff $ delay $ wrap 200.0 foo <- get 10000 empty "/?foo" bar <- get 10000 empty "/?bar=test" notbar <- get 10000 empty "/?bar=nottest" baz <- get 10000 empty "/?baz=test" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 foo ?= "foo" bar ?= "bar" notbar ?= "" baz ?= "test" sslSpec :: Test sslSpec = it "runs the ssl example" do close <- liftEffect SSL.main liftAff $ delay $ wrap 200.0 response <- get' 10000 empty "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 response ?= "hello world!" extensibleMiddlewareSpec :: Test extensibleMiddlewareSpec = it "runs the extensible middleware example" do close <- liftEffect ExtensibleMiddleware.main liftAff $ delay $ wrap 200.0 let headers = Object.singleton "X-Token" "123" body <- get 10000 headers "/" body' <- get 10000 empty "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 body `shouldStartWith` "hello John Doe, it is" body' `shouldStartWith` "hello anonymous, it is" nodeMiddlewareSpec :: Test nodeMiddlewareSpec = it "runs the node middleware example" do close <- liftEffect NodeMiddleware.main liftAff $ delay $ wrap 200.0 let headers = Object.singleton "X-Token" "123" body <- get 10000 headers "/" body' <- get 10000 empty "/" liftEffect $ close $ pure unit liftAff $ delay $ wrap 200.0 body `shouldStartWith` "hello John Doe" body' `shouldStartWith` "hello anonymous" integrationSpec :: Test integrationSpec = describe "Integration" do asyncResponseSpec binaryRequestSpec binaryResponseSpec chunkedSpec customStackSpec headersSpec helloWorldSpec middlewareSpec multiRouteSpec pathSegmentsSpec postSpec queryParametersSpec sslSpec jsonParsingSpec extensibleMiddlewareSpec nodeMiddlewareSpec