mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
50
test/rule/beforeDealHttpsRequest.spec.js
Normal file
50
test/rule/beforeDealHttpsRequest.spec.js
Normal file
@@ -0,0 +1,50 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { basicProxyRequest, proxyServerWithRule, } = require('../util.js');
|
||||
|
||||
const RULE_PAYLOAD = 'this is something in rule';
|
||||
|
||||
const rule = {
|
||||
*beforeSendRequest(requestDetail) {
|
||||
const requestOptions = requestDetail.requestOptions;
|
||||
return {
|
||||
requestOptions,
|
||||
requestData: RULE_PAYLOAD,
|
||||
};
|
||||
},
|
||||
|
||||
*beforeDealHttpsRequest(requestDetail) {
|
||||
return requestDetail.host.indexOf('httpbin.org') >= 0;
|
||||
}
|
||||
};
|
||||
|
||||
describe('Rule beforeDealHttpsRequest', () => {
|
||||
let proxyServer;
|
||||
let proxyPort;
|
||||
let proxyHost;
|
||||
|
||||
beforeAll(async () => {
|
||||
proxyServer = await proxyServerWithRule(rule);
|
||||
proxyPort = proxyServer.proxyPort;
|
||||
proxyHost = `http://localhost:${proxyPort}`;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
return proxyServer && proxyServer.close();
|
||||
});
|
||||
it('Should replace the https request body', async () => {
|
||||
const url = 'https://httpbin.org/put';
|
||||
const payloadStream = fs.createReadStream(path.resolve(__dirname, '../fixtures/image.png'));
|
||||
const postHeaders = {
|
||||
anyproxy_header: 'header_value',
|
||||
};
|
||||
|
||||
await basicProxyRequest(proxyHost, 'PUT', url, postHeaders, {}, payloadStream).then((result) => {
|
||||
const proxyRes = result.response;
|
||||
const body = JSON.parse(result.body);
|
||||
expect(proxyRes.statusCode).toBe(200);
|
||||
expect(body.data).toEqual(RULE_PAYLOAD);
|
||||
expect(body.url.indexOf('/put')).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
93
test/rule/beforeSendRequest.spec.js
Normal file
93
test/rule/beforeSendRequest.spec.js
Normal file
@@ -0,0 +1,93 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { basicProxyRequest, proxyServerWithRule, } = require('../util.js');
|
||||
|
||||
const RULE_PAYLOAD = 'this is something in rule';
|
||||
const RULE_REPLACE_HEADER_KEY = 'rule_replace_header_key';
|
||||
const RULE_REPLACE_HEADER_VALUE = 'rule_replace_header_value';
|
||||
|
||||
const rule = {
|
||||
*beforeSendRequest(requestDetail) {
|
||||
const reqUrl = requestDetail.url;
|
||||
if (reqUrl.indexOf('/post') >= 0) {
|
||||
const requestOptions = requestDetail.requestOptions;
|
||||
requestOptions.path = '/put';
|
||||
requestOptions.method = 'PUT';
|
||||
return {
|
||||
requestOptions,
|
||||
requestData: RULE_PAYLOAD,
|
||||
};
|
||||
} else if (reqUrl.indexOf('/status/302') >= 0) {
|
||||
return {
|
||||
response: {
|
||||
statusCode: 404,
|
||||
header: {
|
||||
[RULE_REPLACE_HEADER_KEY]: RULE_REPLACE_HEADER_VALUE,
|
||||
'content-type': 'plain/text',
|
||||
},
|
||||
body: RULE_PAYLOAD
|
||||
}
|
||||
};
|
||||
} else if (reqUrl.indexOf('/should_be_replaced') >= 0) {
|
||||
const requestOptions = requestDetail.requestOptions;
|
||||
requestOptions.hostname = 'httpbin.org';
|
||||
requestOptions.path = '/status/302';
|
||||
requestOptions.port = '443';
|
||||
return {
|
||||
protocol: 'https',
|
||||
requestOptions,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
describe('Rule replaceRequestData', () => {
|
||||
let proxyServer;
|
||||
let proxyPort;
|
||||
let proxyHost;
|
||||
|
||||
beforeAll(async () => {
|
||||
proxyServer = await proxyServerWithRule(rule);
|
||||
proxyPort = proxyServer.proxyPort;
|
||||
proxyHost = `http://localhost:${proxyPort}`;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
return proxyServer && proxyServer.close();
|
||||
});
|
||||
|
||||
it('should replace the request data in proxy if the assertion is true', async () => {
|
||||
const url = 'http://httpbin.org/post';
|
||||
const payloadStream = fs.createReadStream(path.resolve(__dirname, '../fixtures/image.png'));
|
||||
const postHeaders = {
|
||||
anyproxy_header: 'header_value',
|
||||
};
|
||||
|
||||
await basicProxyRequest(proxyHost, 'POST', url, postHeaders, {}, payloadStream).then((result) => {
|
||||
const proxyRes = result.response;
|
||||
const body = JSON.parse(result.body);
|
||||
expect(proxyRes.statusCode).toBe(200);
|
||||
expect(body.data).toEqual(RULE_PAYLOAD);
|
||||
expect(body.url.indexOf('/put')).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
it('should respond content specified in rule', async () => {
|
||||
const url = 'http://httpbin.org/status/302';
|
||||
await basicProxyRequest(proxyHost, 'GET', url).then((result) => {
|
||||
const proxyRes = result.response;
|
||||
const body = result.body;
|
||||
expect(body).toBe(RULE_PAYLOAD);
|
||||
expect(proxyRes.statusCode).toBe(404);
|
||||
expect(proxyRes.headers[RULE_REPLACE_HEADER_KEY]).toBe(RULE_REPLACE_HEADER_VALUE);
|
||||
});
|
||||
});
|
||||
|
||||
it('should replace protocol and url', async () => {
|
||||
const url = 'http://domain_not_exists.anyproxy.io/should_be_replaced';
|
||||
await basicProxyRequest(proxyHost, 'GET', url).then((result) => {
|
||||
const proxyRes = result.response;
|
||||
expect(proxyRes.statusCode).toBe(302);
|
||||
});
|
||||
});
|
||||
});
|
||||
45
test/rule/beforeSendResponse.js
Normal file
45
test/rule/beforeSendResponse.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const { basicProxyRequest, proxyServerWithRule, } = require('../util.js');
|
||||
|
||||
const RULE_REPLACE_HEADER_KEY = 'rule_replace_header_key';
|
||||
const RULE_REPLACE_HEADER_VALUE = 'rule_replace_header_value';
|
||||
const RULE_REPLACE_BODY = 'RULE_REPLACE_BODY';
|
||||
const rule = {
|
||||
*beforeSendResponse(requestDetail, responseDetail) {
|
||||
if (requestDetail.url.indexOf('/uuid') >= 0) {
|
||||
const newResponse = responseDetail.response;
|
||||
newResponse.header[RULE_REPLACE_HEADER_KEY] = RULE_REPLACE_HEADER_VALUE;
|
||||
newResponse.body = RULE_REPLACE_BODY;
|
||||
newResponse.statusCode = 502;
|
||||
return {
|
||||
response: newResponse,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
describe('Rule replaceResponseData', () => {
|
||||
let proxyServer;
|
||||
let proxyPort;
|
||||
let proxyHost;
|
||||
|
||||
beforeAll(async () => {
|
||||
proxyServer = await proxyServerWithRule(rule);
|
||||
proxyPort = proxyServer.proxyPort;
|
||||
proxyHost = `http://localhost:${proxyPort}`;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
return proxyServer && proxyServer.close();
|
||||
});
|
||||
|
||||
it('Should replace the header and body', async () => {
|
||||
const url = 'http://httpbin.org/uuid';
|
||||
await basicProxyRequest(proxyHost, 'GET', url).then((result) => {
|
||||
const proxyRes = result.response;
|
||||
const body = result.body;
|
||||
expect(proxyRes.statusCode).toBe(502);
|
||||
expect(proxyRes.headers[RULE_REPLACE_HEADER_KEY]).toBe(RULE_REPLACE_HEADER_VALUE);
|
||||
expect(body).toBe(RULE_REPLACE_BODY);
|
||||
});
|
||||
});
|
||||
});
|
||||
60
test/rule/onError.spec.js
Normal file
60
test/rule/onError.spec.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const { basicProxyRequest, proxyServerWithRule, } = require('../util.js');
|
||||
|
||||
const jestMockErrorFn = jest.fn();
|
||||
const jestMockConnectErrorFn = jest.fn();
|
||||
|
||||
const ERROR_PAGE_IN_RULE = 'this is my error page';
|
||||
const rule = {
|
||||
onConnectError: jestMockConnectErrorFn,
|
||||
*onError(requestDetail, error) {
|
||||
jestMockErrorFn(requestDetail, error);
|
||||
return {
|
||||
response: {
|
||||
statusCode: '200',
|
||||
header: {},
|
||||
body: ERROR_PAGE_IN_RULE,
|
||||
}
|
||||
};
|
||||
},
|
||||
*beforeDealHttpsRequest(requestDetail) {
|
||||
return requestDetail.host.indexOf('intercept') === 0;
|
||||
},
|
||||
};
|
||||
|
||||
describe('Rule replaceResponseData', () => {
|
||||
let proxyServer;
|
||||
let proxyPort;
|
||||
let proxyHost;
|
||||
|
||||
beforeAll(async () => {
|
||||
proxyServer = await proxyServerWithRule(rule);
|
||||
proxyPort = proxyServer.proxyPort;
|
||||
proxyHost = `http://localhost:${proxyPort}`;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
return proxyServer && proxyServer.close();
|
||||
});
|
||||
|
||||
it('should get error', async () => {
|
||||
const url = 'https://intercept.anyproxy_not_exists.io/some_path';
|
||||
const result = await basicProxyRequest(proxyHost, 'GET', url);
|
||||
const proxyRes = result.response;
|
||||
const body = result.body;
|
||||
expect(proxyRes.statusCode).toBe(200);
|
||||
expect(body).toBe(ERROR_PAGE_IN_RULE);
|
||||
expect(jestMockErrorFn.mock.calls.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should get connec error', async () => {
|
||||
const url = 'https://anyproxy_not_exists.io/do_not_intercept';
|
||||
let e;
|
||||
try {
|
||||
await basicProxyRequest(proxyHost, 'GET', url);
|
||||
} catch (err) {
|
||||
e = err;
|
||||
}
|
||||
expect(e).not.toBeUndefined();
|
||||
expect(jestMockConnectErrorFn.mock.calls.length).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user