Skip to content

Commit a103f41

Browse files
authored
Merge pull request usebruno#196 from mirkogolze/main
check response type for ResponsePane CodeEditor
2 parents c020cd9 + 7d4d157 commit a103f41

File tree

5 files changed

+72
-25
lines changed

5 files changed

+72
-25
lines changed

packages/bruno-app/src/components/Environments/EnvironmentSelector/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const EnvironmentSelector = ({ collection }) => {
3535
toast.success(`No Environments are active now`);
3636
}
3737
})
38-
.catch((err) => console.log(err) && toast.error('An error occured while selecting the environment'));
38+
.catch((err) => console.log(err) && toast.error('An error occurred while selecting the environment'));
3939
};
4040

4141
return (
@@ -64,7 +64,7 @@ const EnvironmentSelector = ({ collection }) => {
6464
}}
6565
>
6666
<IconDatabaseOff size={18} strokeWidth={1.5} />
67-
<span className='ml-2'>No Environment</span>
67+
<span className="ml-2">No Environment</span>
6868
</div>
6969
<div className="dropdown-item border-top" onClick={() => setOpenSettingsModal(true)}>
7070
<div className="pr-2 text-gray-600">

packages/bruno-app/src/components/ResponsePane/QueryResult/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import { sendRequest } from 'providers/ReduxStore/slices/collections/actions';
66

77
import StyledWrapper from './StyledWrapper';
88

9-
const QueryResult = ({ item, collection, value, width, disableRunEventListener }) => {
10-
const {
11-
storedTheme
12-
} = useTheme();
9+
const QueryResult = ({ item, collection, value, width, disableRunEventListener, mode }) => {
10+
const { storedTheme } = useTheme();
1311
const dispatch = useDispatch();
1412

1513
const onRun = () => {
16-
if(disableRunEventListener) {
14+
if (disableRunEventListener) {
1715
return;
1816
}
1917
dispatch(sendRequest(item, collection.uid));
@@ -22,7 +20,7 @@ const QueryResult = ({ item, collection, value, width, disableRunEventListener }
2220
return (
2321
<StyledWrapper className="px-3 w-full" style={{ maxWidth: width }}>
2422
<div className="h-full">
25-
<CodeEditor collection={collection} theme={storedTheme} onRun={onRun} value={value || ''} readOnly />
23+
<CodeEditor collection={collection} theme={storedTheme} onRun={onRun} value={value || ''} mode={mode} readOnly />
2624
</div>
2725
</StyledWrapper>
2826
);

packages/bruno-app/src/components/ResponsePane/index.js

+34-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import find from 'lodash/find';
33
import classnames from 'classnames';
44
import { safeStringifyJSON } from 'utils/common';
5-
import { useSelector, useDispatch } from 'react-redux';
5+
import { useDispatch, useSelector } from 'react-redux';
66
import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs';
77
import QueryResult from './QueryResult';
88
import Overlay from './Overlay';
@@ -36,18 +36,21 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
3636
const getTabPanel = (tab) => {
3737
switch (tab) {
3838
case 'response': {
39-
return <QueryResult
40-
item={item}
41-
collection={collection}
42-
width={rightPaneWidth}
43-
value={response.data ? safeStringifyJSON(response.data, true) : ''}
44-
/>;
39+
return (
40+
<QueryResult
41+
item={item}
42+
collection={collection}
43+
width={rightPaneWidth}
44+
value={response.data ? (isJson(response.headers) ? safeStringifyJSON(response.data, true) : response.data) : ''}
45+
mode={getContentType(response.headers)}
46+
/>
47+
);
4548
}
4649
case 'headers': {
4750
return <ResponseHeaders headers={response.headers} />;
4851
}
4952
case 'timeline': {
50-
return <Timeline request={item.requestSent} response={item.response}/>;
53+
return <Timeline request={item.requestSent} response={item.response} />;
5154
}
5255
case 'tests': {
5356
return <TestResults results={item.testResults} assertionResults={item.assertionResults} />;
@@ -81,7 +84,7 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
8184

8285
const focusedTab = find(tabs, (t) => t.uid === activeTabUid);
8386
if (!focusedTab || !focusedTab.uid || !focusedTab.responsePaneTab) {
84-
return <div className="pb-4 px-4">An error occured!</div>;
87+
return <div className="pb-4 px-4">An error occurred!</div>;
8588
}
8689

8790
const getTabClassname = (tabName) => {
@@ -90,6 +93,28 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
9093
});
9194
};
9295

96+
const getContentType = (headers) => {
97+
if (headers && headers.length) {
98+
let contentType = headers
99+
.filter((header) => header[0].toLowerCase() === 'content-type')
100+
.map((header) => {
101+
return header[1];
102+
});
103+
if (contentType && contentType.length) {
104+
if (typeof contentType[0] == 'string' && /^[\w\-]+\/([\w\-]+\+)?json/.test(contentType[0])) {
105+
return 'application/ld+json';
106+
} else if (typeof contentType[0] == 'string' && /^[\w\-]+\/([\w\-]+\+)?xml/.test(contentType[0])) {
107+
return 'application/xml';
108+
}
109+
}
110+
}
111+
return '';
112+
};
113+
114+
const isJson = (headers) => {
115+
return getContentType(headers) === 'application/ld+json';
116+
};
117+
93118
return (
94119
<StyledWrapper className="flex flex-col h-full relative">
95120
<div className="flex items-center px-3 tabs" role="tablist">

packages/bruno-cli/src/runner/prepare-request.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ const { get, each, filter } = require('lodash');
22

33
const prepareRequest = (request) => {
44
const headers = {};
5+
let contentTypeDefined = false;
56
each(request.headers, (h) => {
67
if (h.enabled) {
78
headers[h.name] = h.value;
9+
if (h.name.toLowerCase() === 'content-type') {
10+
contentTypeDefined = true;
11+
}
812
}
913
});
1014

@@ -17,7 +21,9 @@ const prepareRequest = (request) => {
1721
request.body = request.body || {};
1822

1923
if (request.body.mode === 'json') {
20-
axiosRequest.headers['content-type'] = 'application/json';
24+
if (!contentTypeDefined) {
25+
axiosRequest.headers['content-type'] = 'application/json';
26+
}
2127
try {
2228
axiosRequest.data = JSON.parse(request.body.json);
2329
} catch (ex) {
@@ -26,12 +32,16 @@ const prepareRequest = (request) => {
2632
}
2733

2834
if (request.body.mode === 'text') {
29-
axiosRequest.headers['content-type'] = 'text/plain';
35+
if (!contentTypeDefined) {
36+
axiosRequest.headers['content-type'] = 'text/plain';
37+
}
3038
axiosRequest.data = request.body.text;
3139
}
3240

3341
if (request.body.mode === 'xml') {
34-
axiosRequest.headers['content-type'] = 'text/xml';
42+
if (!contentTypeDefined) {
43+
axiosRequest.headers['content-type'] = 'text/xml';
44+
}
3545
axiosRequest.data = request.body.xml;
3646
}
3747

@@ -56,7 +66,9 @@ const prepareRequest = (request) => {
5666
query: get(request, 'body.graphql.query'),
5767
variables: JSON.parse(get(request, 'body.graphql.variables') || '{}')
5868
};
59-
axiosRequest.headers['content-type'] = 'application/json';
69+
if (!contentTypeDefined) {
70+
axiosRequest.headers['content-type'] = 'application/json';
71+
}
6072
axiosRequest.data = graphqlQuery;
6173
}
6274

packages/bruno-electron/src/ipc/network/prepare-request.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ const { get, each, filter } = require('lodash');
22

33
const prepareRequest = (request) => {
44
const headers = {};
5+
let contentTypeDefined = false;
56
each(request.headers, (h) => {
67
if (h.enabled) {
78
headers[h.name] = h.value;
9+
if (h.name.toLowerCase() === 'content-type') {
10+
contentTypeDefined = true;
11+
}
812
}
913
});
1014

@@ -15,7 +19,9 @@ const prepareRequest = (request) => {
1519
};
1620

1721
if (request.body.mode === 'json') {
18-
axiosRequest.headers['content-type'] = 'application/json';
22+
if (!contentTypeDefined) {
23+
axiosRequest.headers['content-type'] = 'application/json';
24+
}
1925
try {
2026
axiosRequest.data = JSON.parse(request.body.json);
2127
} catch (ex) {
@@ -24,12 +30,16 @@ const prepareRequest = (request) => {
2430
}
2531

2632
if (request.body.mode === 'text') {
27-
axiosRequest.headers['content-type'] = 'text/plain';
33+
if (!contentTypeDefined) {
34+
axiosRequest.headers['content-type'] = 'text/plain';
35+
}
2836
axiosRequest.data = request.body.text;
2937
}
3038

3139
if (request.body.mode === 'xml') {
32-
axiosRequest.headers['content-type'] = 'text/xml';
40+
if (!contentTypeDefined) {
41+
axiosRequest.headers['content-type'] = 'text/xml';
42+
}
3343
axiosRequest.data = request.body.xml;
3444
}
3545

@@ -54,7 +64,9 @@ const prepareRequest = (request) => {
5464
query: get(request, 'body.graphql.query'),
5565
variables: JSON.parse(get(request, 'body.graphql.variables') || '{}')
5666
};
57-
axiosRequest.headers['content-type'] = 'application/json';
67+
if (!contentTypeDefined) {
68+
axiosRequest.headers['content-type'] = 'application/json';
69+
}
5870
axiosRequest.data = graphqlQuery;
5971
}
6072

0 commit comments

Comments
 (0)