Bugfix/incorrect space encode (#5870)

* Fix the space encoding issue

* fix: incorrect space encoding

Fixed an issue in Code Generation for requests. The original fix was
raised in [PR](https://github.com/usebruno/bruno/pull/4478). The current
PR fixes some merge conflicts and resolves some unimported dependencies
error.

* test: add URL encoding tests for code generation feature

Add Playwright tests to verify proper URL encoding behavior in Bruno's
code generation dialog for both encoded and unencoded query parameters.

* moved the test script inside request

* updated the snippet generation code to reuse code and reduce redundancy

* removed redundant code and reverted autoformat

* reverted some auto formatted changes

* reverting format during commit hook

* chore: reset formatting

* chore: reformat

---------

Co-authored-by: Vipin Sundar <86339268+vipin-sundar@users.noreply.github.com>
Co-authored-by: Chirag Chandrashekhar <chiragchan@Chirags-MacBook-Air.local>
Co-authored-by: Sid <siddharth@usebruno.com>

chore: reformat
This commit is contained in:
Chirag Chandrashekhar
2025-10-24 16:23:23 +05:30
committed by Sid
parent 045141efaf
commit fa1498e2a8
4 changed files with 117 additions and 19 deletions

View File

@@ -10,7 +10,6 @@ import { findCollectionByItemUid, getGlobalEnvironmentVariables } from 'utils/co
import { cloneDeep } from 'lodash';
import { useMemo } from 'react';
import { generateSnippet } from '../utils/snippet-generator';
const CodeView = ({ language, item }) => {
const { displayedTheme } = useTheme();
const preferences = useSelector((state) => state.app.preferences);
@@ -32,23 +31,14 @@ const CodeView = ({ language, item }) => {
return c;
}, [collectionOriginal, globalEnvironments, activeGlobalEnvironmentUid]);
const snippet = useMemo(() => {
let snippet = '';
try {
const request = cloneDeep(item.request);
if (request.url) {
request.url = decodeURIComponent(request.url);
}
return new HTTPSnippet(buildHarRequest({ request: request, headers, type: item.type })).convert(
target,
client
);
} catch (e) {
console.error(e);
return 'Error generating code snippet';
}
}, [language, item, collection, generateCodePrefs.shouldInterpolate]);
const snippet = useMemo(() => {
return generateSnippet({
language,
item,
collection,
shouldInterpolate: generateCodePrefs.shouldInterpolate
});
}, [language, item, collection, generateCodePrefs.shouldInterpolate]);
return (
<StyledWrapper>

View File

@@ -131,9 +131,16 @@ const createPostData = (body) => {
};
export const buildHarRequest = ({ request, headers }) => {
// NOTE:
// This is just a safety check.
// The interpolateUrlPathParams method validates the url, but it does not throw
if (!URL.canParse(request.url)) {
throw new Error('invalid request url');
}
return {
method: request.method,
url: encodeURI(request.url),
url: request.url,
httpVersion: 'HTTP/1.1',
cookies: [],
headers: createHeaders(request, headers),