测试文件
This commit is contained in:
42
pages/mall/delivery/webhook-server/test-send.js
Normal file
42
pages/mall/delivery/webhook-server/test-send.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const http = require('http')
|
||||
const https = require('https')
|
||||
const crypto = require('crypto')
|
||||
|
||||
const PORT = process.env.PORT || 7201
|
||||
const URL = `http://localhost:${PORT}/webhook/express/status`
|
||||
const SECRET = process.env.WEBHOOK_SECRET || 'test_secret'
|
||||
|
||||
const payload = {
|
||||
tracking_no: 'LOCALTEST123',
|
||||
status_code: 'DELIVERED',
|
||||
acceptTime: new Date().toISOString(),
|
||||
remark: 'local test event'
|
||||
}
|
||||
|
||||
const bodyText = JSON.stringify(payload)
|
||||
const ts = Math.floor(Date.now() / 1000).toString()
|
||||
const sig = crypto.createHmac('sha256', SECRET).update(bodyText + ts).digest('hex')
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(bodyText),
|
||||
'X-Timestamp': ts,
|
||||
'X-Signature': sig
|
||||
}
|
||||
}
|
||||
|
||||
const req = http.request(URL, options, (res) => {
|
||||
let data = ''
|
||||
res.on('data', (chunk) => data += chunk)
|
||||
res.on('end', () => {
|
||||
console.log('STATUS', res.statusCode)
|
||||
try { console.log('BODY', JSON.parse(data)) } catch (e) { console.log('BODY', data) }
|
||||
process.exit(0)
|
||||
})
|
||||
})
|
||||
|
||||
req.on('error', (e) => { console.error('request error', e); process.exit(2) })
|
||||
req.write(bodyText)
|
||||
req.end()
|
||||
Reference in New Issue
Block a user