-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save only the relative path of the filename for
StaticDirStorage
(#…
…1990) * Save only the relative path of the filename for `StaticDirStorage` fixes #1989 * Adapt createTemporaryDownloadUrlFromPathWithExpiredAt * Change slash logic * Start adding tests * Use newControllerContext * Start fix tests * More fixes * Copy from another test * Add withFrameworkConfig * Fix compile * Fix tests * Fix logic * Ignore test folders * Fix tests * Re-enable all tests * Remove line break * Import cleanups * More line breaks * Line breaks * Fix typo * Prefix objectPath with slash
- Loading branch information
Showing
4 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ devenv.local.nix | |
result* | ||
|
||
.idea | ||
|
||
# Test folders | ||
static/Test.FileStorage.ControllerFunctionsSpec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module Test.FileStorage.ControllerFunctionsSpec where | ||
|
||
import Test.Hspec | ||
import IHP.Prelude | ||
import IHP.FileStorage.ControllerFunctions | ||
import IHP.Controller.Context | ||
import IHP.FrameworkConfig | ||
import Network.Wai as Wai (defaultRequest) | ||
import Network.Wai.Parse (FileInfo(..)) | ||
import IHP.Controller.RequestContext | ||
import IHP.FileStorage.Types | ||
import IHP.FileStorage.Config | ||
|
||
tests :: Spec | ||
tests = describe "IHP.FileStorage.ControllerFunctions" $ do | ||
|
||
let config :: ConfigBuilder | ||
config = do | ||
initStaticDirStorage | ||
|
||
let withFrameworkConfig = IHP.FrameworkConfig.withFrameworkConfig config | ||
|
||
describe "storeFileWithOptions" $ do | ||
it "returns the objectPath without the baseUrl" $ do | ||
withFrameworkConfig \frameworkConfig -> do | ||
context <- createControllerContext frameworkConfig | ||
let ?context = context | ||
|
||
let fileInfo = FileInfo | ||
{ fileName = "test.txt" | ||
, fileContentType = "text/plain" | ||
, fileContent = "Hello, world!" | ||
} | ||
|
||
-- We pass the UUID that will be used as the filename, so we can easily assert the objectPath. | ||
let options :: StoreFileOptions = def | ||
{ fileName = Just "4c55dac2-e411-45ac-aa10-b957b01221df" | ||
, directory = "Test.FileStorage.ControllerFunctionsSpec" | ||
} | ||
|
||
result <- storeFileWithOptions fileInfo options | ||
|
||
result.url `shouldBe` ("/Test.FileStorage.ControllerFunctionsSpec/4c55dac2-e411-45ac-aa10-b957b01221df") | ||
|
||
describe "createTemporaryDownloadUrlFromPath" $ do | ||
it "returns baseUrl concatenated with objectPath when objectPath does not start with http:// or https://" $ do | ||
withFrameworkConfig \frameworkConfig -> do | ||
context <- createControllerContext frameworkConfig | ||
let ?context = context | ||
let objectPath = "/static/test.txt" | ||
temporaryDownloadUrl <- createTemporaryDownloadUrlFromPath objectPath | ||
|
||
temporaryDownloadUrl.url `shouldBe` "http://localhost:8000/static/test.txt" | ||
|
||
it "returns the objectPath when objectPath starts with 'http://' or 'https://'" $ do | ||
withFrameworkConfig \frameworkConfig -> do | ||
context <- createControllerContext frameworkConfig | ||
let ?context = context | ||
let objectPath = "https://example.com/static/test.txt" | ||
temporaryDownloadUrl <- createTemporaryDownloadUrlFromPath objectPath | ||
|
||
temporaryDownloadUrl.url `shouldBe` "https://example.com/static/test.txt" | ||
|
||
createControllerContext frameworkConfig = do | ||
let | ||
requestBody = FormBody { params = [], files = [] } | ||
request = Wai.defaultRequest | ||
requestContext = RequestContext { request, respond = error "respond", requestBody, frameworkConfig = frameworkConfig } | ||
let ?requestContext = requestContext | ||
newControllerContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters