Skip to content
28 changes: 24 additions & 4 deletions console/atest-ui/src/views/TestCase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,17 @@ const parseResponseBody = (body: any) => {
}
}

const handleTestResult = (e: any) => {
/**
* Handles test result data from API response
*
* Processes the test response with proper error handling and content type detection:
* - For JSON responses: Parses and makes it available for filtering/display
* - For plain text responses: Displays as raw text without JSON parsing
* - For file responses: Handles as downloadable content
*
* @param e The test result data from API
*/
const handleTestResult = (e: any): void => {
testResult.value = e;

if (!isHistoryTestCase.value) {
Expand All @@ -124,9 +134,19 @@ const handleTestResult = (e: any) => {
isResponseFile.value = true
} else if(e.body !== ''){
testResult.value.bodyLength = e.body.length
testResult.value.bodyObject = JSON.parse(e.body);
testResult.value.originBodyObject = JSON.parse(e.body);
responseBodyFilter()
try {
// Try to parse as JSON, fallback to plain text if parsing fails
testResult.value.bodyObject = JSON.parse(e.body);
testResult.value.originBodyObject = JSON.parse(e.body);
responseBodyFilter()
} catch (error) {
// This is an expected case for non-JSON responses (like text/plain)
// We intentionally display as plain text instead of attempting JSON parsing
console.debug("Response body is not valid JSON, displaying as plain text:", error);
testResult.value.bodyText = e.body;
testResult.value.bodyObject = null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type 'null' is not assignable to type '{}'.ts-plugin(2322)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check the content type, then handle JSON or not.

image

testResult.value.originBodyObject = null;
}
}

Cache.SetTestCaseResponseCache(suite + '-' + name, {
Expand Down
44 changes: 38 additions & 6 deletions console/atest-ui/src/views/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,44 @@ limitations under the License.
*/
import { Cache } from './cache'

async function DefaultResponseProcess(response: any) {
/**
* Process HTTP response with proper content type handling
*
* This function handles both JSON and non-JSON responses:
* 1. For JSON responses, parses and returns the JSON object
* 2. For non-JSON responses (like text/plain), returns the raw text
*
* @param response The fetch API response object
* @returns Parsed JSON object or raw text content
*/
async function DefaultResponseProcess(response: any): Promise<any> {
if (!response.ok) {
switch (response.status) {
case 401:
throw new Error("Unauthenticated")
}

const message = await response.json().then((data: any) => data.message)
throw new Error(message)
} else {
return response.json()
try {
const message = await response.json().then((data: any) => data.message)
throw new Error(message)
} catch {
const text = await response.text()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code will always run to here due to line 37.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

throw new Error(text)
}
}

// Get the complete response text first
const responseText = await response.text();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get the content type, such as:

const contentType = response.headers.get('content-type') || '';


// Try parsing as JSON, fallback to raw text if failed
try {
return JSON.parse(responseText);
} catch (e) {
// This is an expected case for non-JSON responses (like text/plain)
// We intentionally handle this by returning the raw text
// No need to log as error since this is a valid content type handling
console.debug("Response is not JSON, handling as plain text");
return responseText;
}
}

Expand Down Expand Up @@ -754,7 +781,12 @@ function GetTestCaseAllHistory(req: TestCase,
.then(callback).catch(errHandle)
}

function DownloadResponseFile(testcase,
interface ResponseFile {
body: string;
[key: string]: any;
}

function DownloadResponseFile(testcase: ResponseFile,
callback: (d: any) => void, errHandle?: (e: any) => void | null) {
const requestOptions = {
method: 'POST',
Expand Down