From bdc8f391b73eadaf0a0b62b8454d0e9b3493c7fb Mon Sep 17 00:00:00 2001 From: sanish chirayath Date: Wed, 5 Nov 2025 19:13:21 +0530 Subject: [PATCH] Feat/response examples testing fixes (#5974) * fix: cloning feat: add response saving limit fix: status code stays OK when i save an example * fix: lint * fix: newly created examples are not opening in new tab * fix: playwright tests * fix: use itemUId to fins item * fix: response save --- .../ResponsePane/ResponseBookmark/index.js | 56 +++++++++++-- .../src/components/ResponsePane/index.js | 21 ++++- .../CollectionItem/ExampleItem/index.js | 46 ++++++---- .../Collection/CollectionItem/index.js | 27 +++++- .../middlewares/tasks/middleware.js | 46 +++++++++- .../ReduxStore/middlewares/tasks/utils.js | 3 +- .../slices/collections/exampleReducers.js | 83 ++++++++++++++++++- .../ReduxStore/slices/collections/index.js | 2 + .../bruno-app/src/utils/collections/index.js | 5 ++ packages/bruno-electron/src/ipc/collection.js | 4 +- .../response-examples/create-example.spec.ts | 3 +- 11 files changed, 255 insertions(+), 41 deletions(-) diff --git a/packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js b/packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js index fb16597f3..cf004f4d5 100644 --- a/packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js +++ b/packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js @@ -1,18 +1,23 @@ -import React, { useState } from 'react'; +import React, { useState, useMemo } from 'react'; import { useDispatch } from 'react-redux'; import { IconBookmark } from '@tabler/icons'; import { addResponseExample } from 'providers/ReduxStore/slices/collections'; import { saveRequest } from 'providers/ReduxStore/slices/collections/actions'; +import { insertTaskIntoQueue } from 'providers/ReduxStore/slices/app'; +import { uuid } from 'utils/common'; import toast from 'react-hot-toast'; import CreateExampleModal from 'components/ResponseExample/CreateExampleModal'; -import { getBodyType, processResponseContent } from 'utils/responseBodyProcessor'; +import { getBodyType } from 'utils/responseBodyProcessor'; +import classnames from 'classnames'; import StyledWrapper from './StyledWrapper'; -const ResponseBookmark = ({ item, collection }) => { +const ResponseBookmark = ({ item, collection, responseSize }) => { const dispatch = useDispatch(); const [showSaveResponseExampleModal, setShowSaveResponseExampleModal] = useState(false); const response = item.response || {}; + const isResponseTooLarge = responseSize >= 5 * 1024 * 1024; // 5 MB + // Only show for HTTP requests if (item.type !== 'http-request') { return null; @@ -50,10 +55,16 @@ const ResponseBookmark = ({ item, collection }) => { toast.error('No valid response to save as example'); return; } + + if (isResponseTooLarge) { + toast.error('Response size exceeds 5MB limit. Cannot save as example.'); + return; + } + setShowSaveResponseExampleModal(true); }; - const saveAsExample = (name, description = '') => { + const saveAsExample = async (name, description = '') => { // Convert headers object to array format expected by schema const headersArray = response.headers && typeof response.headers === 'object' ? Object.entries(response.headers).map(([name, value]) => ({ @@ -80,12 +91,33 @@ const ResponseBookmark = ({ item, collection }) => { description: description }; + // Calculate the index where the example will be saved + // This will be the length of the examples array after adding the new one + const existingExamples = item.draft?.examples || item.examples || []; + const exampleIndex = existingExamples.length; + const exampleUid = uuid(); + dispatch(addResponseExample({ itemUid: item.uid, collectionUid: collection.uid, - example: exampleData + example: { + ...exampleData, + uid: exampleUid + } })); - dispatch(saveRequest(item.uid, collection.uid)); + + // Save the request + await dispatch(saveRequest(item.uid, collection.uid)); + + // Task middleware will track this and open the example in a new tab once the file is reloaded + dispatch(insertTaskIntoQueue({ + uid: exampleUid, + type: 'OPEN_EXAMPLE', + collectionUid: collection.uid, + itemUid: item.uid, + exampleIndex: exampleIndex + })); + setShowSaveResponseExampleModal(false); toast.success(`Example "${name}" created successfully`); }; @@ -95,9 +127,15 @@ const ResponseBookmark = ({ item, collection }) => {