27 lines
2.4 KiB
JavaScript
27 lines
2.4 KiB
JavaScript
|
|
'use strict';
|
||
|
|
const assert=require('assert/strict');
|
||
|
|
const fs=require('fs');
|
||
|
|
const fsp=fs.promises;
|
||
|
|
const os=require('os');
|
||
|
|
const path=require('path');
|
||
|
|
const {spawn,spawnSync}=require('child_process');
|
||
|
|
const service=require('../scripts/service-control');
|
||
|
|
|
||
|
|
const php=process.env.LINK_FIELD_PHP_PATH||'php',probe=spawnSync(php,['-v'],{encoding:'utf8'});
|
||
|
|
if(probe.error||probe.status!==0)throw new Error('PHP CLI is required for the bridge integration test');
|
||
|
|
const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));
|
||
|
|
|
||
|
|
(async()=>{
|
||
|
|
const root=await fsp.mkdtemp(path.join(os.tmpdir(),'linkfield-php-bridge-')),publicDir=path.join(root,'public'),dataRoot=path.join(root,'data'),nodePort=40000+Math.floor(Math.random()*5000),phpPort=nodePort+5000;
|
||
|
|
await service.deployPublicFiles(publicDir);
|
||
|
|
const node=spawn(process.execPath,[path.join(service.ROOT,'server.js')],{env:{...process.env,HOST:'127.0.0.1',PORT:String(nodePort),LINK_FIELD_TEST_DATA_ROOT:dataRoot,LINK_FIELD_PUBLIC_DIR:publicDir,LINK_FIELD_APACHE_BRIDGE:'0'},stdio:['ignore','pipe','pipe']});
|
||
|
|
const phpServer=spawn(php,['-S',`127.0.0.1:${phpPort}`,'-t',publicDir],{stdio:['ignore','pipe','pipe']});let errors='';node.stderr.on('data',chunk=>errors+=chunk);phpServer.stderr.on('data',chunk=>errors+=chunk);
|
||
|
|
try{
|
||
|
|
let response;for(let i=0;i<100;i++){try{response=await fetch(`http://127.0.0.1:${phpPort}/api-bridge.php?path=/api/cloud/status`);if(response.ok)break}catch(_){}await sleep(50)}
|
||
|
|
assert(response?.ok,errors);assert.equal(response.headers.get('x-linkfield-bridge'),'php');const status=await response.json();assert.equal(status.available,true);
|
||
|
|
response=await fetch(`http://127.0.0.1:${phpPort}/api-bridge.php?path=/api/cloud/session`,{method:'POST',headers:{'content-type':'application/json'},body:'{"name":"PHP"}'});assert.equal(response.status,201);assert.match((await response.json()).playerId,/^[a-f0-9]{24}$/);
|
||
|
|
response=await fetch(`http://127.0.0.1:${phpPort}/api-bridge.php?path=${encodeURIComponent('/api/cloud/status\r\nInjected: true')}`);assert.equal(response.status,400);
|
||
|
|
console.log('Real PHP-to-Node bridge integration passed');
|
||
|
|
}finally{node.kill('SIGTERM');phpServer.kill('SIGTERM');await sleep(300);if(node.exitCode==null)node.kill('SIGKILL');if(phpServer.exitCode==null)phpServer.kill('SIGKILL');await fsp.rm(root,{recursive:true,force:true})}
|
||
|
|
})().catch(error=>{console.error(error);process.exitCode=1});
|
||
|
|
|