forked from eclipse-thingweb/node-wot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: improve file client implementation (eclipse-thingweb#1175)
* feat!: improve file client implementation * fixup! feat!: improve file client implementation * fixup! feat!: improve file client implementation * fixup! feat!: improve file client implementation * fixup! feat!: improve file client implementation * fixup! feat!: improve file client implementation * fixup! feat!: improve file client implementation Co-authored-by: danielpeintner <[email protected]> * fixup! feat!: improve file client implementation * fixup! feat!: improve file client implementation --------- Co-authored-by: danielpeintner <[email protected]>
- Loading branch information
1 parent
5c06db1
commit 8761e64
Showing
5 changed files
with
138 additions
and
56 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
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
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,2 @@ | ||
# Ignore auxiliary files created by the FileClient implementation tests | ||
test.* |
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,96 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and | ||
* Document License (2015-05-13) which is available at | ||
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513 | ||
********************************************************************************/ | ||
|
||
import { Content, ContentSerdes } from "@node-wot/core"; | ||
|
||
import FileClient from "../src/file-client"; | ||
import { Form } from "@node-wot/td-tools"; | ||
import { expect } from "chai"; | ||
import { unlink } from "fs"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
const jsonValue = { | ||
foo: "bar", | ||
}; | ||
|
||
function formatContentType(contentType?: string) { | ||
if (contentType == null) { | ||
return "no Content-Type"; | ||
} | ||
|
||
return `Content-Type ${contentType}`; | ||
} | ||
|
||
describe("File Client Implementation", () => { | ||
let fileClient: FileClient; | ||
|
||
beforeEach(async () => { | ||
fileClient = new FileClient(); | ||
await fileClient.start(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await fileClient.stop(); | ||
}); | ||
|
||
for (const uriScheme of ["file:///", "file://"]) { | ||
for (const [index, testData] of [ | ||
{ | ||
value: jsonValue, | ||
contentType: "application/json", | ||
fileExtension: "json", | ||
}, | ||
{ value: jsonValue, contentType: undefined, fileExtension: "json" }, | ||
{ value: "Lorem ipsum dolor sit amet.", contentType: "text/plain", fileExtension: "txt" }, | ||
].entries()) { | ||
it(`should be able to write and read files using URI scheme ${uriScheme} with ${formatContentType( | ||
testData.contentType | ||
)}`, async () => { | ||
const contentType = testData.contentType; | ||
const originalValue = testData.value; | ||
const fileName = `test${index}.${testData.fileExtension}`; | ||
|
||
// eslint-disable-next-line n/no-path-concat | ||
const href = `${uriScheme}${__dirname}/${fileName}`; | ||
const filePath = fileURLToPath(href); | ||
|
||
const form: Form = { | ||
href, | ||
contentType, | ||
}; | ||
|
||
const writeContent = ContentSerdes.get().valueToContent( | ||
originalValue, | ||
undefined, | ||
contentType ?? ContentSerdes.DEFAULT | ||
); | ||
|
||
await fileClient.writeResource(form, writeContent); | ||
|
||
const rawContent: Content = await fileClient.readResource(form); | ||
|
||
const readContent = { | ||
body: await rawContent.toBuffer(), | ||
type: writeContent.type, | ||
}; | ||
|
||
const readValue = ContentSerdes.get().contentToValue(readContent, {}); | ||
expect(readValue).to.deep.eq(originalValue); | ||
|
||
unlink(filePath, () => {}); | ||
}); | ||
} | ||
} | ||
}); |