-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathtestServer.js
More file actions
141 lines (122 loc) · 4.39 KB
/
Copy pathtestServer.js
File metadata and controls
141 lines (122 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const { PARENT_PORT, CHILD_PORT } = require('./tests/browser/test-ports');
const app = express();
// Test suite definitions
const TEST_SUITES = {
'dev': {
name: 'Development Build',
description: 'Unminified library for easier debugging',
customLibUrl: './static/build/mixpanel.js',
snippetUrl: './static/src/loaders/mixpanel-jslib-snippet.js',
testUrl: './static/build/test/browser/snippet-test.js'
},
'minified': {
name: 'Minified Build',
description: 'Production build (closure compiled, served via Mixpanel CDN)',
customLibUrl: './static/build/mixpanel.min.js',
snippetUrl: './static/build/mixpanel-jslib-snippet.min.test.js',
testUrl: './static/build/test/browser/snippet-test.js'
},
'module-cjs': {
name: 'CommonJS Module',
description: 'Node.js compatible module (served via npm install)',
testUrl: './static/build/test/browser/module-cjs-test.js'
}
};
app.use(cookieParser());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set('views', __dirname + '/tests');
app.set('view engine', 'pug');
// ========================================
// Test API endpoints for network plugin tests
// ========================================
// Main test endpoint - handles all HTTP methods
app.all('/api/test', function(req, res) {
res.json({
success: true,
method: req.method,
headers: req.headers,
query: req.query,
body: req.body,
url: req.originalUrl,
timestamp: Date.now()
});
});
// Form submission endpoint
app.post('/api/test/form', function(req, res) {
res.json({
success: true,
method: 'POST',
contentType: req.get('Content-Type'),
formData: req.body,
timestamp: Date.now()
});
});
// Endpoint with custom response headers
app.get('/api/test/headers', function(req, res) {
res.set({
'X-Custom-Header': 'custom-value',
'X-Request-Id': 'test-request-123'
});
res.json({
success: true,
message: 'Response includes custom headers'
});
});
// Error response endpoint
app.get('/api/test/error/:status', function(req, res) {
const status = parseInt(req.params.status, 10) || 500;
res.status(status).json({ success: false, status: status });
});
// Session recording endpoint (mimics Mixpanel's /record API)
app.post(/^\/record\/.*/, express.raw({ type: '*/*', limit: '10mb' }), function(req, res) {
res.json({ code: 200, status: 'OK' });
});
// ========================================
app.use('/tests', express.static(__dirname + "/tests"));
app.get('/tests/cookie_included/:cookieName', function(req, res) {
if (req.cookies && req.cookies[req.params.cookieName]) {
res.json(1);
} else {
res.json(0);
}
});
app.use(express.static(__dirname));
app.get('/', function(req, res) {
res.redirect(301, '/tests/new');
});
app.get('/tests/new', function(req, res) {
res.render('directory.pug', { testSuites: TEST_SUITES });
});
app.use('/tests/new/static', express.static(__dirname));
// register routes for each test suite
for (const [suiteId, suite] of Object.entries(TEST_SUITES)) {
app.get('/tests/new/' + suiteId, function(req, res) {
res.render('integration.pug', {
suiteName: suite.name,
customLibUrl: suite.customLibUrl,
snippetUrl: suite.snippetUrl,
testUrl: suite.testUrl
});
});
// Cross-origin child iframe page for session recording tests
app.get('/tests/new/' + suiteId + '-cross-origin-page', function(req, res) {
res.render('cross-origin-page.pug', {
customLibUrl: suite.customLibUrl || './static/build/mixpanel.js',
snippetUrl: suite.snippetUrl || './static/src/loaders/mixpanel-jslib-snippet.js',
testUrl: './static/build/test/browser/cross-origin-page.js'
});
});
}
const server = app.listen(PARENT_PORT, function () {
console.log(`Mixpanel test app listening on port ${server.address().port}`);
});
// Second port for cross-origin iframe tests
const server2 = app.listen(CHILD_PORT, function () {
console.log(`Mixpanel cross-origin test server listening on port ${server2.address().port}`);
});