Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
akupiec
GitHub Repository: akupiec/mad-proxy
Path: blob/master/test/int_server.test.js
115 views
1
/* eslint-env jest */
2
3
const request = require('supertest');
4
const config = require('../src/config/config');
5
const fs = require('fs');
6
7
jest.mock('../src/config/config', () => require('./testingConfig'));
8
jest.mock('fs', () => ({
9
readdirSync: jest.fn(),
10
existsSync: jest.fn(),
11
writeFileSync: jest.fn(),
12
}));
13
jest.mock('../src/config/logger');
14
15
jest.mock('../src/middlewares/utils/proxyServer', () => {
16
return {
17
web: (req, res) => {
18
res.status(200).send('OK');
19
},
20
};
21
});
22
23
describe('integration express', function () {
24
let server;
25
beforeEach(function () {
26
fs.readdirSync.mockReturnValue([]);
27
fs.existsSync.mockReturnValue(true);
28
Object.assign(config, {
29
proxies: [{
30
path: '/api',
31
target: 'http://google.com',
32
cache: {enabled: true},
33
}],
34
});
35
server = require('../src/server')();
36
});
37
afterEach(function () {
38
server.close();
39
fs.writeFileSync.mockReset();
40
});
41
42
it('responds to reverse proxyServer', function () {
43
return request(server)
44
.get('/api/test')
45
.expect(200)
46
.then(response => {
47
expect(response.text).toEqual('OK');
48
});
49
});
50
51
it('should create cached file at get', function () {
52
return request(server)
53
.get('/api/abb-cc?par1=aaa&par2=bbb')
54
.expect(200)
55
.then(() => {
56
const calls = fs.writeFileSync.mock.calls;
57
expect(calls.length).toEqual(1);
58
let path = calls[0][0];
59
expect(path).toBeDefined();
60
path = path.replace(/\\/g, '/');
61
expect(path).toContain('/FAKE_MOCK_DIR/api/GET/abb-cc#977c21ac.html');
62
expect(calls[0][1].toString()).toBe('OK');
63
});
64
});
65
66
it('should create cached file at post', function () {
67
return request(server)
68
.post('/api/abb-dd', {a: '12', b: 'aaaa'})
69
.expect(200)
70
.then(() => {
71
const calls = fs.writeFileSync.mock.calls;
72
expect(calls.length).toEqual(1);
73
let path = calls[0][0];
74
expect(path).toBeDefined();
75
path = path.replace(/\\/g, '/');
76
expect(path).toContain('/FAKE_MOCK_DIR/api/POST/abb-dd#824ac3d0.html');
77
expect(calls[0][1].toString()).toBe('OK');
78
});
79
});
80
});
81
82