mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
test curlify function
This commit is contained in:
@@ -2,7 +2,8 @@
|
|||||||
"spec_dir": "test",
|
"spec_dir": "test",
|
||||||
"spec_files": [
|
"spec_files": [
|
||||||
"spec_lib/*.js",
|
"spec_lib/*.js",
|
||||||
"spec_rule/*.js"
|
"spec_rule/*.js",
|
||||||
|
"spec_web/*.js"
|
||||||
],
|
],
|
||||||
"helpers": [
|
"helpers": [
|
||||||
"../node_modules/babel-register/lib/node.js",
|
"../node_modules/babel-register/lib/node.js",
|
||||||
|
|||||||
50
test/spec_web/curlUtil.js
Normal file
50
test/spec_web/curlUtil.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
const { curlify } = require('../../web/src/common/curlUtil');
|
||||||
|
|
||||||
|
describe('Test the curlify function', () => {
|
||||||
|
it('request with headers', () => {
|
||||||
|
const requestDetail = {
|
||||||
|
method: 'POST',
|
||||||
|
url: 'https://localhost:3001/test',
|
||||||
|
reqHeader: {
|
||||||
|
'via-proxy': 'true',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = 'curl \'https://localhost:3001/test\' -X POST -H \'via-proxy: true\'';
|
||||||
|
expect(curlify(requestDetail)).toBe(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('request with JSON body', () => {
|
||||||
|
const requestDetail = {
|
||||||
|
method: 'POST',
|
||||||
|
url: 'https://localhost:3001/test',
|
||||||
|
reqHeader: {
|
||||||
|
'content-type': 'application/json; charset=utf-8',
|
||||||
|
},
|
||||||
|
reqBody: '{"action":1,"method":"test"}',
|
||||||
|
};
|
||||||
|
const result = `curl '${requestDetail.url}' -X POST -H 'content-type: application/json; charset=utf-8' -d '${requestDetail.reqBody}'`;
|
||||||
|
expect(curlify(requestDetail)).toBe(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accpet gzip encoding with compressed flag', () => {
|
||||||
|
const requestDetail = {
|
||||||
|
method: 'GET',
|
||||||
|
url: 'https://localhost:3001/test',
|
||||||
|
reqHeader: {
|
||||||
|
Host: 'localhost',
|
||||||
|
'Accept-Encoding': 'gzip',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = 'curl \'https://localhost:3001/test\' -H \'Host: localhost\' -H \'Accept-Encoding: gzip\' --compressed';
|
||||||
|
expect(curlify(requestDetail)).toBe(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('escape url character', () => {
|
||||||
|
const requestDetail = {
|
||||||
|
method: 'GET',
|
||||||
|
url: 'https://localhost:3001/test?a[]=1',
|
||||||
|
};
|
||||||
|
const result = 'curl \'https://localhost:3001/test?a\\[\\]=1\'';
|
||||||
|
expect(curlify(requestDetail)).toBe(result);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,37 +1,25 @@
|
|||||||
|
export function curlify(recordDetail) {
|
||||||
|
const headers = { ...recordDetail.reqHeader };
|
||||||
|
const acceptEncoding = headers['Accept-Encoding'] || headers['accept-encoding'];
|
||||||
|
// escape reserve character in url
|
||||||
|
const url = recordDetail.url.replace(/([\[\]])/g, '\\$1');
|
||||||
|
const curlified = ['curl', `'${url}'`];
|
||||||
|
|
||||||
export function curlify(recordDetail){
|
if (recordDetail.method.toUpperCase() !== 'GET') {
|
||||||
let curlified = []
|
curlified.push('-X', recordDetail.method);
|
||||||
let type = ''
|
|
||||||
let headers = { ...recordDetail.reqHeader }
|
|
||||||
curlified.push('curl')
|
|
||||||
curlified.push('-X', recordDetail.method)
|
|
||||||
curlified.push(`'${recordDetail.url}'`)
|
|
||||||
|
|
||||||
if (headers) {
|
|
||||||
type = headers['Content-Type']
|
|
||||||
delete headers['Accept-Encoding']
|
|
||||||
|
|
||||||
for(let k of Object.keys(headers)){
|
|
||||||
let v = headers[k]
|
|
||||||
curlified.push('-H')
|
|
||||||
curlified.push(`'${k}: ${v}'`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recordDetail.reqBody){
|
Object.keys(headers).forEach((key) => {
|
||||||
|
curlified.push('-H', `'${key}: ${headers[key]}'`);
|
||||||
|
});
|
||||||
|
|
||||||
if(type === 'multipart/form-data' && recordDetail.method === 'POST') {
|
if (recordDetail.reqBody) {
|
||||||
let formDataBody = recordDetail.reqBody.split('&')
|
curlified.push('-d', `'${recordDetail.reqBody}'`);
|
||||||
|
|
||||||
for(let data of formDataBody) {
|
|
||||||
curlified.push('-F')
|
|
||||||
curlified.push(data)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
curlified.push('-d')
|
|
||||||
curlified.push(recordDetail.reqBody)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return curlified.join(' ')
|
if (/deflate|gzip/.test(acceptEncoding)) {
|
||||||
|
curlified.push('--compressed');
|
||||||
|
}
|
||||||
|
|
||||||
|
return curlified.join(' ');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user