Skip to content

Commit 7922d34

Browse files
committed
fix: handle text/plain responses correctly
Fix parsing and displaying of text/plain responses by: - Improving text handling in DefaultResponseProcess function (net.ts) - Adding safe JSON parsing in TestCase.vue with fallback to text display
1 parent a81db18 commit 7922d34

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

console/atest-ui/src/views/TestCase.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,15 @@ const handleTestResult = (e: any) => {
124124
isResponseFile.value = true
125125
} else if(e.body !== ''){
126126
testResult.value.bodyLength = e.body.length
127-
testResult.value.bodyObject = JSON.parse(e.body);
128-
testResult.value.originBodyObject = JSON.parse(e.body);
129-
responseBodyFilter()
127+
try {
128+
testResult.value.bodyObject = JSON.parse(e.body);
129+
testResult.value.originBodyObject = JSON.parse(e.body);
130+
responseBodyFilter()
131+
} catch (error) {
132+
testResult.value.bodyText = e.body;
133+
testResult.value.bodyObject = null;
134+
testResult.value.originBodyObject = null;
135+
}
130136
}
131137
132138
Cache.SetTestCaseResponseCache(suite + '-' + name, {

console/atest-ui/src/views/net.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ async function DefaultResponseProcess(response: any) {
3030
throw new Error(text)
3131
}
3232
}
33-
34-
const contentType = response.headers.get('Content-Type') || '';
35-
if (contentType.startsWith('text/plain')) {
36-
return response.text();
37-
}
33+
34+
// 先获取响应文本
35+
const responseText = await response.text();
36+
37+
// 尝试解析为JSON,如果失败则直接返回原始文本
3838
try {
39-
return await response.json();
39+
return JSON.parse(responseText);
4040
} catch (e) {
41-
// If JSON parsing fails, return as text
42-
return response.text();
41+
// 不是有效的JSON,直接返回原始文本
42+
return responseText;
4343
}
4444
}
4545

@@ -768,7 +768,12 @@ function GetTestCaseAllHistory(req: TestCase,
768768
.then(callback).catch(errHandle)
769769
}
770770

771-
function DownloadResponseFile(testcase,
771+
interface ResponseFile {
772+
body: string;
773+
[key: string]: any;
774+
}
775+
776+
function DownloadResponseFile(testcase: ResponseFile,
772777
callback: (d: any) => void, errHandle?: (e: any) => void | null) {
773778
const requestOptions = {
774779
method: 'POST',

0 commit comments

Comments
 (0)