-
-
Notifications
You must be signed in to change notification settings - Fork 753
fix: mock server for internal tests #5238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kobenguyent
merged 6 commits into
3.x
from
5237-create-a-simple-rest-server-for-internal-tests
Oct 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6ae9d3
mock server
kobenguyent 6b4b9b2
mock server
kobenguyent d17bfba
lint fix
kobenguyent a9f59d4
fix: acceptance tests cannot access mock server
kobenguyent 7d298cf
fix: acceptance tests cannot access mock server
kobenguyent 2003418
fix: flaky timeout test
kobenguyent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| const express = require('express'); | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| // Example users data | ||
| let users = [ | ||
| { id: 1, name: 'John Doe', email: 'john@example.com' }, | ||
| { id: 2, name: 'Jane Smith', email: 'jane@example.com' } | ||
| ]; | ||
|
|
||
| // GET /api/users | ||
| app.get('/api/users', (req, res) => { | ||
| res.json({ data: users }); | ||
| }); | ||
|
|
||
| // GET /api/users/:id | ||
| app.get('/api/users/:id', (req, res) => { | ||
| const user = users.find(u => u.id === parseInt(req.params.id)); | ||
| if (user) return res.json(user); | ||
| res.status(404).json({ error: 'User not found' }); | ||
| }); | ||
|
|
||
| // POST /api/users | ||
| app.post('/api/users', (req, res) => { | ||
| const { name, email } = req.body; | ||
| const newUser = { id: users.length + 1, name, email }; | ||
| users.push(newUser); | ||
| res.status(201).json(newUser); | ||
| }); | ||
|
|
||
| // PUT /api/users/:id | ||
| app.put('/api/users/:id', (req, res) => { | ||
| const user = users.find(u => u.id === parseInt(req.params.id)); | ||
| if (!user) return res.status(404).json({ error: 'User not found' }); | ||
| user.name = req.body.name || user.name; | ||
| user.email = req.body.email || user.email; | ||
| res.json(user); | ||
| }); | ||
|
|
||
| // DELETE /api/users/:id | ||
| app.delete('/api/users/:id', (req, res) => { | ||
| users = users.filter(u => u.id !== parseInt(req.params.id)); | ||
| res.status(204).send(); | ||
| }); | ||
|
|
||
| // Start server | ||
| const PORT = process.env.PORT || 3001; | ||
| app.listen(PORT, () => { | ||
| console.log(`Mock REST server running on port ${PORT}`); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| const { spawn } = require('child_process'); | ||
| const http = require('http'); | ||
|
|
||
| const PORT = process.env.PORT || 3001; | ||
| const MAX_RETRIES = 20; | ||
| const RETRY_DELAY = 500; | ||
|
|
||
| function waitForServer(retries = 0) { | ||
| return new Promise((resolve, reject) => { | ||
| http.get(`http://localhost:${PORT}/api/users`, res => { | ||
| if (res.statusCode === 200) return resolve(true); | ||
| res.resume(); | ||
| reject(); | ||
| }).on('error', () => { | ||
| if (retries < MAX_RETRIES) { | ||
| setTimeout(() => { | ||
| resolve(waitForServer(retries + 1)); | ||
| }, RETRY_DELAY); | ||
| } else { | ||
| reject(new Error('Mock server did not start in time')); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| const serverProcess = spawn('node', ['server.js'], { | ||
| cwd: __dirname, | ||
| stdio: 'inherit', | ||
| env: process.env, | ||
| }); | ||
|
|
||
| console.log('Starting mock server...'); | ||
| waitForServer() | ||
| .then(() => { | ||
| console.log('Mock server is up and running!'); | ||
| }) | ||
| .catch(err => { | ||
| console.error(err.message); | ||
| serverProcess.kill(); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.