tauri/tooling/cli/node/test/jest/fixtures/app-test-setup.js

91 lines
2.1 KiB
JavaScript
Raw Normal View History

// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2023-02-19 16:17:49 +03:00
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
const path = require('path')
const http = require('http')
2020-03-07 19:40:24 +03:00
const currentDirName = __dirname
const mockFixtureDir = path.resolve(currentDirName, '../fixtures')
2020-03-07 19:40:24 +03:00
module.exports.fixtureDir = mockFixtureDir
2020-03-07 19:40:24 +03:00
module.exports.initJest = (mockFixture) => {
feat(license): SPDX Headers (#1449) * chore(licenses): api Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(licenses): scripts Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/core Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/tauri-bundler Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): workflows Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): require license_template in rust Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-api Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-build Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-codegen Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-macros Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-updater Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-utils Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): examples Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/tauri.js Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): changefile Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): place both licenses in root Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): package.json SPDX Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): SPDX everywhere Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * fix(tauri.js): tests more time for ubuntu Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): commons conservancy language Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): add spdx file Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * fix(license): clippy Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): language Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com>
2021-04-11 01:09:09 +03:00
jest.setTimeout(1200000)
const mockAppDir = path.join(mockFixtureDir, mockFixture)
process.env.__TAURI_TEST_APP_DIR = mockAppDir
2020-03-07 19:40:24 +03:00
}
module.exports.startServer = (onSuccess) => {
const responses = {
writeFile: null,
readFile: null,
writeFileWithDir: null,
readFileWithDir: null,
readDir: null,
readDirWithDir: null,
copyFile: null,
copyFileWithDir: null,
createDir: null,
createDirWithDir: null,
removeDir: null,
removeDirWithDir: null,
renameFile: null,
renameFileWithDir: null,
removeFile: null,
renameFileWithDir: null,
listen: null
}
function addResponse(response) {
responses[response.cmd] = true
if (!Object.values(responses).some((c) => c === null)) {
server.close(onSuccess)
}
}
2020-03-07 19:40:24 +03:00
const app = http.createServer((req, res) => {
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Request-Method', '*')
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
res.setHeader('Access-Control-Allow-Headers', '*')
if (req.method === 'OPTIONS') {
res.writeHead(200)
res.end()
return
}
if (req.method === 'POST') {
let body = ''
req.on('data', (chunk) => {
body += chunk.toString()
})
2020-03-07 19:40:24 +03:00
if (req.url === '/reply') {
req.on('end', () => {
const json = JSON.parse(body)
addResponse(json)
res.writeHead(200)
res.end()
2020-03-07 19:40:24 +03:00
})
}
if (req.url === '/error') {
2020-03-07 19:40:24 +03:00
req.on('end', () => {
res.writeHead(200)
res.end()
throw new Error(body)
2020-03-07 19:40:24 +03:00
})
}
}
})
const port = 7000
const server = app.listen(port)
return {
server,
responses
}
2020-03-07 19:40:24 +03:00
}