Skip to content

Commit

Permalink
Add createMock function to mockManager.js and proxy.helper.js
Browse files Browse the repository at this point in the history
  • Loading branch information
davoxpa committed Jan 4, 2024
1 parent f37525a commit 7fbebca
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Add `http://localhost:3000/` as a prefix to your backend URL to use the mocking
- [x] Add functionality for single URL bypass.
- [x] Add functionality for massive bypass.
- [x] Add functionality to delete all files.
- [] Add functionality to search among mocks.
- [x] Add functionality to search among mocks.
- [x] Add functionality to create mocks.
- [] Add a monitor for API call logs.

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mockingbird-proxy",
"version": "1.0.4",
"version": "1.0.5",
"description": "mockingbird-proxy is a simple proxy server that allows you to mock API responses.",
"main": "./src/main.js",
"scripts": {
Expand Down
18 changes: 17 additions & 1 deletion src/app-proxy-server/mockManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require("path");
const { BrowserWindow, ipcMain } = require("electron");
const StoreManager = require('../storeManager');
const storeManager = StoreManager.getInstance();
const crypto = require('crypto');

// Definisci il percorso della cartella che desideri controllare/creare

Expand Down Expand Up @@ -33,6 +34,20 @@ const getMock = (filename) => {
}
}

const createMock = (data) => {

if (data.payload && data.payload.length > 0) {
data.uuid = crypto
.createHash('sha256')
.update(data.targetUrl + JSON.stringify(data.payload))
.digest('hex');
} else {
data.uuid = crypto.createHash('sha256').update(data.targetUrl + JSON.stringify({})).digest('hex');
}

return saveMock(data.uuid, data);
}

const deleteMock = (filename) => {
try {
const filePath = path.join(folderPath, filename + ".json");
Expand Down Expand Up @@ -134,5 +149,6 @@ module.exports = {
getAllMock,
startMockManager,
changeValueOnMock,
filterMock
filterMock,
createMock,
};
2 changes: 1 addition & 1 deletion src/app-proxy-server/proxy.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function proxySniffer(req, res, next) {
bypassCache: config.bypassCache,
uuid: sha256,
method: req.method,
payload: req.method === 'GET' || req.method === 'HEAD' ? undefined : JSON.stringify(req.body),
payload: req.method === 'GET' || req.method === 'HEAD' ? undefined : req.body,
targetUrl: target,
response: responseData || undefined,
statusCode: externalResponse.status || undefined,
Expand Down
16 changes: 14 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (isDev) {
require('./menu');

// gestione mock
const { getAllMock, deleteMock, deleteAllMock, getMock, saveMock, startMockManager, changeValueOnMock, filterMock } = require('./app-proxy-server/mockManager');
const { getAllMock, deleteMock, deleteAllMock, getMock, saveMock, startMockManager, changeValueOnMock, filterMock, createMock } = require('./app-proxy-server/mockManager');

// gestione server
const {startServer, stopServer, checkStatusServer} = require('./app-proxy-server/server');
Expand Down Expand Up @@ -200,4 +200,16 @@ ipcMain.on('changeConfig', (event, key, value)=>{
ipcMain.on('getConfig', (event)=>{
console.log('getConfig')
mainWindow.webContents.send('responseGetConfig', storeManager.getConfig());
});
});

ipcMain.on('openPageCreateMock', (event)=>{
console.log('openPageCreateMock')
mainWindow.loadFile(path.join(__dirname, './views/create.html'));
});

ipcMain.on('createNewMock', (event, mock)=>{
console.log('createNewMock', mock)
createMock(mock);
mainWindow.webContents.send('responseCreateNewMock');
});

36 changes: 36 additions & 0 deletions src/views/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!-- generate html for electron -->
<!DOCTYPE html>
<html>

<head>
<title>Mockingbird Proxy create</title>

<script src="../assets/library/jsoneditor.js"></script>
<link rel="stylesheet" type="text/css" href="../assets/library/jsoneditor.css">
<link rel="stylesheet" type="text/css" href="../assets/styles/main.css">
<script src="./create.js"></script>
</head>

<body>
<div class="container container-edit">
<div class="row top-bar">
<div class="col-6">
<div class="header">
<img src="../assets/logo/mocking-bird-proxy-logo.jpg" class="img-fluid" alt="" srcset="">
<h1>Mockingbird Proxy</h1>
</div>
</div>
<div class="col-6 controller-json d-flex justify-content-end align-items-center ">
<i class="bi bi-x-square-fill" onclick="goHome()"></i>
<i class="bi bi-floppy-fill" onclick="createNewMock()"></i>
</div>
</div>
<div class="row">
<div class="col-12 controller-json">
</div>
</div>
<div id="jsoneditor"></div>
</div>
</body>

</html>
51 changes: 51 additions & 0 deletions src/views/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { ipcRenderer } = require('electron');
console.log('edit.js');

let editor = null; // Variabile globale per mantenere l'editor
let jsonUpdated = null; // Variabile globale per mantenere il json aggiornato

function openEditor(data) {
const options = {};
const container = document.getElementById('jsoneditor');

// Pulisce il contenitore se l'editor esiste già
if (editor) {
editor.destroy();
}
// Crea una nuova istanza dell'editor
editor = new JSONEditor(container, options);
editor.set(data);
}
const data = {
"bypassCache": false,
"method": "GET",
"payload": {},
"targetUrl": "",
"response": {},
"statusCode": 200,
"timestamp": new Date().getTime()
}
document.addEventListener('DOMContentLoaded', () => {
console.log('DOMContentLoaded');
openEditor(data);
});

function goHome() {
ipcRenderer.send('goHome');
}

function createNewMock() {
jsonUpdated = editor.get();
// check data
if (!jsonUpdated.targetUrl) {
alert('targetUrl is required');
return;
}
jsonUpdated.uuid = '';
console.log('jsonUpdated', jsonUpdated);
ipcRenderer.send('createNewMock', jsonUpdated);
}
ipcRenderer.on('responseCreateNewMock', (event, data) => {
console.log('responseSaveMock js', data);
goHome();
});
1 change: 1 addition & 0 deletions src/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ <h1>Mockingbird Proxy</h1>
</div>
</div>
<div class="delete-section">
<button type="button" class="btn btn-success delete-all" onclick="openPageCreateMock()"><i class="bi bi-file-earmark-plus-fill"></i> Add</button>
<button type="button" class="btn btn-danger delete-all" onclick="showConfirmDelete()"><i class="bi bi-trash-fill"></i> Delete all mock</button>
<button type="button" class="btn btn-danger confirm-delete-all d-none" onclick="deleteAllMock()">
<i class="bi bi-trash-fill"></i>
Expand Down
4 changes: 4 additions & 0 deletions src/views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ function bypassAllChange(el) {
ipcRenderer.send('byPassGlobalChange', el.checked);
}

function openPageCreateMock() {
ipcRenderer.send('openPageCreateMock');
}

// Generate dom element for mock

function createMockElement({method, targetUrl, uuid, bypassCache}) {
Expand Down

0 comments on commit 7fbebca

Please sign in to comment.