2023-02-19 16:17:49 +03:00
|
|
|
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-02-10 22:45:41 +03:00
|
|
|
const path = require('path')
|
|
|
|
const http = require('http')
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2022-02-10 22:45:41 +03:00
|
|
|
const currentDirName = __dirname
|
2021-08-17 16:27:02 +03:00
|
|
|
const mockFixtureDir = path.resolve(currentDirName, '../fixtures')
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2022-02-10 22:45:41 +03:00
|
|
|
module.exports.fixtureDir = mockFixtureDir
|
2020-03-07 19:40:24 +03:00
|
|
|
|
2022-02-10 22:45:41 +03:00
|
|
|
module.exports.initJest = (mockFixture) => {
|
2021-04-11 01:09:09 +03:00
|
|
|
jest.setTimeout(1200000)
|
2020-03-10 03:27:54 +03:00
|
|
|
|
2021-08-17 16:27:02 +03:00
|
|
|
const mockAppDir = path.join(mockFixtureDir, mockFixture)
|
|
|
|
process.env.__TAURI_TEST_APP_DIR = mockAppDir
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|
|
|
|
|
2022-02-10 22:45:41 +03:00
|
|
|
module.exports.startServer = (onSuccess) => {
|
2020-03-30 05:41:45 +03:00
|
|
|
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
|
|
|
|
}
|
2020-07-09 16:40:31 +03:00
|
|
|
|
|
|
|
function addResponse(response) {
|
2020-03-30 05:41:45 +03:00
|
|
|
responses[response.cmd] = true
|
2020-08-19 05:36:46 +03:00
|
|
|
if (!Object.values(responses).some((c) => c === null)) {
|
2020-03-30 05:41:45 +03:00
|
|
|
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') {
|
2020-03-30 05:41:45 +03:00
|
|
|
let body = ''
|
2020-08-19 05:36:46 +03:00
|
|
|
req.on('data', (chunk) => {
|
2020-03-30 05:41:45 +03:00
|
|
|
body += chunk.toString()
|
|
|
|
})
|
2020-03-07 19:40:24 +03:00
|
|
|
if (req.url === '/reply') {
|
2020-03-30 05:41:45 +03:00
|
|
|
req.on('end', () => {
|
|
|
|
const json = JSON.parse(body)
|
|
|
|
addResponse(json)
|
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
2020-03-07 19:40:24 +03:00
|
|
|
})
|
2020-03-30 05:41:45 +03:00
|
|
|
}
|
|
|
|
if (req.url === '/error') {
|
2020-03-07 19:40:24 +03:00
|
|
|
req.on('end', () => {
|
2020-03-30 05:41:45 +03:00
|
|
|
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)
|
2020-07-09 16:40:31 +03:00
|
|
|
return {
|
|
|
|
server,
|
|
|
|
responses
|
|
|
|
}
|
2020-03-07 19:40:24 +03:00
|
|
|
}
|