HarmonyOS 鸿蒙Next使用rcp上传文件后接口返回成功但下载打开文件为空白
HarmonyOS 鸿蒙Next使用rcp上传文件后接口返回成功但下载打开文件为空白 上传文件到服务器 接口返回成功了 但是下载打开的文件是空白的 感觉文件流没有写进去
3 回复
参考如下demo
import { rcp } from '@kit.RemoteCommunicationKit';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
const HTTP_SERVER = "https://httpbin.org/anything";
const REQUEST_INTERCEPTOR_KEY_PREFIX = 'Request-Interceptor';
const REQUEST_INTERCEPTOR_VALUE_PREFIX = 'request-interceptor';
const REQUEST_INTERCEPTOR_1_KEY = REQUEST_INTERCEPTOR_KEY_PREFIX + '1';
const REQUEST_INTERCEPTOR_1_VALUE = REQUEST_INTERCEPTOR_VALUE_PREFIX + '1';
const REQUEST_INTERCEPTOR_2_KEY = REQUEST_INTERCEPTOR_KEY_PREFIX + '2';
const REQUEST_INTERCEPTOR_2_VALUE = REQUEST_INTERCEPTOR_VALUE_PREFIX + '2';
const REQUEST_INTERCEPTOR_3_KEY = REQUEST_INTERCEPTOR_KEY_PREFIX + '3';
const REQUEST_INTERCEPTOR_3_VALUE = REQUEST_INTERCEPTOR_VALUE_PREFIX + '3';
const RESPONSE_INTERCEPTOR_KEY_PREFIX = 'Response-Interceptor';
const RESPONSE_INTERCEPTOR_VALUE_PREFIX = 'response-interceptor';
const RESPONSE_INTERCEPTOR_1_KEY = RESPONSE_INTERCEPTOR_KEY_PREFIX + '1';
const RESPONSE_INTERCEPTOR_1_VALUE = RESPONSE_INTERCEPTOR_VALUE_PREFIX + '1';
const RESPONSE_INTERCEPTOR_2_KEY = RESPONSE_INTERCEPTOR_KEY_PREFIX + '2';
const RESPONSE_INTERCEPTOR_2_VALUE = RESPONSE_INTERCEPTOR_VALUE_PREFIX + '2';
const RETURN_INTERCEPTOR_1_KEY = 'testKey1';
const RETURN_INTERCEPTOR_1_VALUE = 'testValue1';
const RETURN_INTERCEPTOR_2_KEY = 'testKey2';
const RETURN_INTERCEPTOR_2_VALUE = 'testValue2';
const RETURN_INTERCEPTOR_1_UNREACHABLE_KEY = 'testKeyUnreachable1';
const RETURN_INTERCEPTOR_1_UNREACHABLE_VALUE = 'testValueUnreachable1';
const RETURN_INTERCEPTOR_2_UNREACHABLE_KEY = 'testKeyUnreachable2';
const RETURN_INTERCEPTOR_2_UNREACHABLE_VALUE = 'testValueUnreachable2';
const ORIGIN_KEY = 'Testkey';
const ORIGIN_VALUE = 'testValue';
let requestInterceptor1IsCalled = false;
let requestInterceptor2IsCalled = false;
let requestInterceptor3IsCalled = false;
let responseInterceptor1IsCalled = false;
let responseInterceptor2IsCalled = false;
let returnInterceptor1IsCalled = false;
let returnInterceptor2IsCalled = false;
function resetGlobal() {
requestInterceptor1IsCalled = false;
requestInterceptor2IsCalled = false;
requestInterceptor3IsCalled = false;
responseInterceptor1IsCalled = false;
responseInterceptor2IsCalled = false;
returnInterceptor1IsCalled = false;
returnInterceptor2IsCalled = false;
}
class RequestInterceptor1 implements rcp.Interceptor {
intercept(context, next) {
const myContext = {
request: context.request,
session: context.session,
};
console.info(myContext.session.id.toString());
if (context.request.headers) {
context.request.headers[REQUEST_INTERCEPTOR_1_KEY] = REQUEST_INTERCEPTOR_1_VALUE;
}
requestInterceptor1IsCalled = true;
return next.handle(context);
}
}
class RequestInterceptor2 implements rcp.Interceptor {
intercept(context, next) {
if (context.request.headers) {
context.request.headers[REQUEST_INTERCEPTOR_2_KEY] = REQUEST_INTERCEPTOR_2_VALUE;
}
requestInterceptor2IsCalled = true;
return next.handle(context);
}
}
class RequestInterceptor3 implements rcp.Interceptor {
intercept(context, next) {
if (context.request.headers) {
context.request.headers[REQUEST_INTERCEPTOR_3_KEY] = REQUEST_INTERCEPTOR_3_VALUE;
}
requestInterceptor3IsCalled = true;
return next.handle(context);
}
}
class ResponseInterceptor1 implements rcp.Interceptor {
intercept(context, next) {
let promise = next.handle(context);
promise.then(resp => {
resp.headers[RESPONSE_INTERCEPTOR_1_KEY] = RESPONSE_INTERCEPTOR_1_VALUE;
});
responseInterceptor1IsCalled = true;
return promise;
}
}
class ResponseInterceptor2 implements rcp.Interceptor {
intercept(context, next) {
let promise = next.handle(context);
promise.then(resp => {
resp.headers[RESPONSE_INTERCEPTOR_2_KEY] = RESPONSE_INTERCEPTOR_2_VALUE;
});
responseInterceptor2IsCalled = true;
return promise;
}
}
class ReturnInterceptor1 implements rcp.Interceptor {
intercept(context, next) {
let header = {};
header[RETURN_INTERCEPTOR_1_KEY] = RETURN_INTERCEPTOR_1_VALUE;
header[RETURN_INTERCEPTOR_2_KEY] = RETURN_INTERCEPTOR_2_VALUE;
let resp = new HttpResponse(new rcp.Request('https://localhost:8080'), 888, header);
returnInterceptor1IsCalled = true;
return Promise.resolve(resp);
}
}
class ReturnInterceptor2 implements rcp.Interceptor {
intercept(context, next) {
let header = {};
header[RETURN_INTERCEPTOR_1_UNREACHABLE_KEY] = RETURN_INTERCEPTOR_1_UNREACHABLE_VALUE;
header[RETURN_INTERCEPTOR_2_UNREACHABLE_KEY] = RETURN_INTERCEPTOR_2_UNREACHABLE_VALUE;
let resp = new HttpResponse(new rcp.Request('https://localhost:8080'), 999, header);
returnInterceptor2IsCalled = true;
return Promise.resolve(resp);
}
}
class HttpResponse implements rcp.Response {
readonly request;
readonly statusCode;
readonly headers;
constructor(request, statusCode, headers) {
this.request = request;
this.statusCode = statusCode;
this.headers = headers;
}
toString() {
return null;
}
toJSON() {
return null;
}
}
interface RespHeader {
headers: rcp.ResponseHeaders
}
let testRequest = async (done) => {
console.info("SUB_NetworkManager_HTTP_Interceptor_0100 test start")
let session = rcp.createSession({
interceptors: [new RequestInterceptor1(), new RequestInterceptor2(), new RequestInterceptor3()],
});
const request = new rcp.Request(HTTP_SERVER);
request.headers = {};
request.headers[ORIGIN_KEY] = ORIGIN_VALUE;
const response = await session.fetch(request);
const json = response.toJSON() as RespHeader;
const rh = json.headers;
expect(response.statusCode).assertEqual(200);
expect(rh[ORIGIN_KEY]).assertEqual(ORIGIN_VALUE);
expect(rh[REQUEST_INTERCEPTOR_1_KEY]).assertEqual(REQUEST_INTERCEPTOR_1_VALUE);
expect(rh[REQUEST_INTERCEPTOR_2_KEY]).assertEqual(REQUEST_INTERCEPTOR_2_VALUE);
expect(rh[REQUEST_INTERCEPTOR_3_KEY]).assertEqual(REQUEST_INTERCEPTOR_3_VALUE);
expect(requestInterceptor1IsCalled).assertTrue();
expect(requestInterceptor2IsCalled).assertTrue();
expect(requestInterceptor3IsCalled).assertTrue();
session.close();
resetGlobal();
console.info("SUB_NetworkManager_HTTP_Interceptor_0100 test end")
done();
}
let testResponse = async (done) => {
console.info("SUB_NetworkManager_HTTP_Interceptor_0200 test start")
let session = rcp.createSession({
interceptors: [new ResponseInterceptor1(), new ResponseInterceptor2()],
});
const request = new rcp.Request(HTTP_SERVER);
request.headers = {};
request.headers[ORIGIN_KEY] = ORIGIN_VALUE;
const response = await session.fetch(request);
const json = response.toJSON() as RespHeader;
const rh = json.headers;
expect(response.statusCode).assertEqual(200);
console.info('Rcp ' + JSON.stringify(rh));
expect(rh[ORIGIN_KEY]).assertEqual(ORIGIN_VALUE);
expect(response.headers[RESPONSE_INTERCEPTOR_1_KEY]).assertEqual(RESPONSE_INTERCEPTOR_1_VALUE);
expect(response.headers[RESPONSE_INTERCEPTOR_2_KEY]).assertEqual(RESPONSE_INTERCEPTOR_2_VALUE);
expect(responseInterceptor1IsCalled).assertTrue();
expect(responseInterceptor2IsCalled).assertTrue();
session.close();
resetGlobal();
console.info("SUB_NetworkManager_HTTP_Interceptor_0200 test end")
done();
}
let testReturn = async (done) => {
console.info("SUB_NetworkManager_HTTP_Interceptor_0300 test start")
let session = rcp.createSession({
interceptors: [new ReturnInterceptor1(), new ReturnInterceptor2()],
});
const request = new rcp.Request(HTTP_SERVER);
request.headers = {};
request.headers[ORIGIN_KEY] = ORIGIN_VALUE;
const response = await session.fetch(request);
expect(response.toJSON()).assertEqual(null);
expect(response.statusCode).assertEqual(888);
expect(response.headers[RETURN_INTERCEPTOR_1_KEY]).assertEqual(RETURN_INTERCEPTOR_1_VALUE);
expect(response.headers[RETURN_INTERCEPTOR_2_KEY]).assertEqual(RETURN_INTERCEPTOR_2_VALUE);
expect(returnInterceptor1IsCalled).assertEqual(true);
expect(returnInterceptor2IsCalled).assertEqual(false);
session.close();
resetGlobal();
console.info("SUB_NetworkManager_HTTP_Interceptor_0300 test end")
done();
}
let testCombine1 = async (done) => {
console.info("SUB_NetworkManager_HTTP_Interceptor_0400 test end")
let session = rcp.createSession({
interceptors: [new RequestInterceptor1(), new ResponseInterceptor1(), new ReturnInterceptor1()],
});
const request = new rcp.Request(HTTP_SERVER);
request.headers = {};
request.headers[ORIGIN_KEY] = ORIGIN_VALUE;
const response = await session.fetch(request);
expect(response.statusCode).assertEqual(888);
expect(requestInterceptor1IsCalled).assertEqual(true);
expect(responseInterceptor1IsCalled).assertEqual(true);
expect(returnInterceptor1IsCalled).assertEqual(true);
expect(response.toJSON()).assertEqual(null);
expect(response.headers[RETURN_INTERCEPTOR_1_KEY]).assertEqual(RETURN_INTERCEPTOR_1_VALUE);
expect(response.headers[RETURN_INTERCEPTOR_2_KEY]).assertEqual(RETURN_INTERCEPTOR_2_VALUE);
session.close();
resetGlobal();
console.info("SUB_NetworkManager_HTTP_Interceptor_0400 test end")
done();
}
let testCombine2 = async (done) => {
console.info("SUB_NetworkManager_HTTP_Interceptor_0500 test start")
let session = rcp.createSession({
interceptors: [new ReturnInterceptor1(), new RequestInterceptor1(), new ResponseInterceptor1()],
});
const request = new rcp.Request(HTTP_SERVER);
request.headers = {};
request.headers[ORIGIN_KEY] = ORIGIN_VALUE;
const response = await session.fetch(request);
expect(response.statusCode).assertEqual(888);
expect(requestInterceptor1IsCalled).assertEqual(false);
expect(responseInterceptor1IsCalled).assertEqual(false);
expect(returnInterceptor1IsCalled).assertEqual(true);
expect(response.toJSON()).assertEqual(null);
expect(response.headers[RETURN_INTERCEPTOR_1_KEY]).assertEqual(RETURN_INTERCEPTOR_1_VALUE);
expect(response.headers[RETURN_INTERCEPTOR_2_KEY]).assertEqual(RETURN_INTERCEPTOR_2_VALUE);
session.close();
resetGlobal();
console.info("SUB_NetworkManager_HTTP_Interceptor_0500 test end")
done();
}
let testCombine3 = async (done) => {
console.info("SUB_NetworkManager_HTTP_Interceptor_0600 test start")
let session = rcp.createSession({
interceptors: [new RequestInterceptor1(), new ReturnInterceptor1(), new ResponseInterceptor1()],
});
const request = new rcp.Request(HTTP_SERVER);
request.headers = {};
request.headers[ORIGIN_KEY] = ORIGIN_VALUE;
const response = await session.fetch(request);
expect(response.statusCode).assertEqual(888);
expect(requestInterceptor1IsCalled).assertEqual(true);
expect(responseInterceptor1IsCalled).assertEqual(false);
expect(returnInterceptor1IsCalled).assertEqual(true);
expect(response.toJSON()).assertEqual(null);
expect(response.headers[RETURN_INTERCEPTOR_1_KEY]).assertEqual(RETURN_INTERCEPTOR_1_VALUE);
expect(response.headers[RETURN_INTERCEPTOR_2_KEY]).assertEqual(RETURN_INTERCEPTOR_2_VALUE);
session.close();
resetGlobal();
console.info("SUB_NetworkManager_HTTP_Interceptor_0600 test end")
done();
}
let testCombine4 = async (done) => {
console.info("SUB_NetworkManager_HTTP_Interceptor_0700 test start")
let session = rcp.createSession({
interceptors: [new ResponseInterceptor1(), new RequestInterceptor1()],
});
const request = new rcp.Request(HTTP_SERVER);
request.headers = {};
request.headers[ORIGIN_KEY] = ORIGIN_VALUE;
const response = await session.fetch(request);
const json = response.toJSON() as RespHeader;
const rh = json.headers;
expect(response.statusCode).assertEqual(200);
expect(requestInterceptor1IsCalled).assertEqual(true);
expect(responseInterceptor1IsCalled).assertEqual(true);
expect(rh[ORIGIN_KEY]).assertEqual(ORIGIN_VALUE);
expect(rh[REQUEST_INTERCEPTOR_1_KEY]).assertEqual(REQUEST_INTERCEPTOR_1_VALUE);
expect(response.headers[RESPONSE_INTERCEPTOR_1_KEY]).assertEqual(RESPONSE_INTERCEPTOR_1_VALUE);
session.close();
resetGlobal();
console.info("SUB_NetworkManager_HTTP_Interceptor_0700 test end")
done();
}
export default function KitInterceptorTest() {
describe('KitInterceptor_Test', () => {
beforeAll(() => {
})
beforeEach(() => {
})
afterEach(() => {
})
afterAll(() => {
});
it('SUB_NetworkManager_HTTP_Interceptor_0100', 0, testRequest);
it('SUB_NetworkManager_HTTP_Interceptor_0200', 0, testResponse);
it('SUB_NetworkManager_HTTP_Interceptor_0300', 0, testReturn);
it('SUB_NetworkManager_HTTP_Interceptor_0400', 0, testCombine1);
it('SUB_NetworkManager_HTTP_Interceptor_0500', 0, testCombine2);
it('SUB_NetworkManager_HTTP_Interceptor_0600', 0, testCombine3);
it('SUB_NetworkManager_HTTP_Interceptor_0700', 0, testCombine4);
})
}
更多关于HarmonyOS 鸿蒙Next使用rcp上传文件后接口返回成功但下载打开文件为空白的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
能截图 看下 代码么