From 2a3ece4f09c9cb2d4b07424e5ded3c5f9f141776 Mon Sep 17 00:00:00 2001 From: maosiping Date: Sat, 23 Jul 2022 17:03:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fixed=203170c3b=20from=20https://gitee.com/?= =?UTF-8?q?maosiping/third=5Fparty=5Fjsframework/pulls/539=20=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E7=AE=A1=E7=90=86JSmock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- .../systemplugin/napi/ohos_net_connection.js | 777 +++++++++--------- .../extend/systemplugin/napi/ohos_net_http.js | 41 +- .../systemplugin/napi/ohos_net_socket.js | 58 +- 3 files changed, 479 insertions(+), 397 deletions(-) diff --git a/runtime/main/extend/systemplugin/napi/ohos_net_connection.js b/runtime/main/extend/systemplugin/napi/ohos_net_connection.js index a0ed0ba5..f9525a3f 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_net_connection.js +++ b/runtime/main/extend/systemplugin/napi/ohos_net_connection.js @@ -1,387 +1,390 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { paramMock } from "../utils" - -export function mockConnection() { - const NetAddress = "[PC Preview] unknow NetAddress" - const NetHandle = { - netId: "[PC Preview] unknow netId", - bindSocket: function () { - console.warn("NetHandle.bindSocket interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - openConnection: function () { - console.warn("NetHandle.openConnection interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, HttpRequest); - } else { - return new Promise((resolve, reject) => { - resolve(HttpRequest); - }) - } - }, - getAddressesByName: function () { - console.warn("NetHandle.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetAddress); - } else { - return new Promise((resolve, reject) => { - resolve(NetAddress); - }) - } - }, - getAddressByName: function () { - console.warn("NetHandle.getAddressByName interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetAddress); - } else { - return new Promise((resolve, reject) => { - resolve(NetAddress); - }) - } - }, - } - const NetBearType = "[PC Preview] unknow NetBearType" - const NetCapabilities = { - bearerTypes: function () { - console.warn("NetCapabilities.bearerTypes interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - var netBearTypeArray = new Array(); - netBearTypeArray.push(NetBearType); - return netBearTypeArray; - } - } - const NetCap = "[PC Preview] unknow NetCap" - const ConnectionProperties = { - interfaceName: "[PC Preview] unknow interfaceName", - isUsePrivateDns: "[PC Preview] unknow isUsePrivateDns", - privateDnsServerName: "[PC Preview] unknow privateDnsServerName", - domains: "[PC Preview] unknow domains", - httpProxy: "[PC Preview] unknow httpProxy", - linkAddresses: function () { - console.warn("ConnectionProperties.linkAddresses interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - var LinkAddress = new Array(); - LinkAddress.push(NetBearType); - return LinkAddress; - }, - dnses: function () { - console.warn("ConnectionProperties.dnses interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - var NetAddress = new Array(); - NetAddress.push(NetBearType); - return NetAddress; - }, - routes: function () { - console.warn("ConnectionProperties.routes interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - var RouteInfo = new Array(); - RouteInfo.push(NetBearType); - return RouteInfo; - }, - mtu: "[PC Preview] unknow mtu" - } - const HttpProxy = { - host: "[PC Preview] unknow host", - port: "[PC Preview] unknow port", - parsedExclusionList: "[PC Preview] unknow parsedExclusionList" - } - const blocked = "[PC Preview] unknow blocked" - const BackgroundPolicy = "[PC Preview] unknow BackgroundPolicy" - const connection = { - on: function (...args) { - console.warn("net.connection.on interface mocked in the Previewer. How this interface works on the Previewer may " + - "be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - if (args[0] === 'netAvailable') { - args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); - } else if (args[0] === 'netBlockStatusChange') { - args[len - 1].call(this, paramMock.businessErrorMock, { NetHandle, blocked }); - } else if (args[0] === 'netCapabilitiesChange') { - args[len - 1].call(this, { NetHandle, NetCap }); - } else if (args[0] === 'netConnectionPropertiesChange') { - args[len - 1].call(this, { NetHandle, ConnectionProperties }); - } else if (args[0] === 'netLosing') { - args[len - 1].call(this, { - NetHandle, - maxMsToLive: "[PC Preview] unknow maxMsToLive" - }); - } else if (args[0] === 'netLost') { - args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); - } else if (args[0] === 'netUnavailable') { - args[len - 1].call(this, paramMock.businessErrorMock); - } - } - }, - off: function (...args) { - console.warn("net.connection.off interface mocked in the Previewer. How this interface works on the Previewer may " + - "be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - if (args[0] === 'netAvailable') { - args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); - } else if (args[0] === 'netBlockStatusChange') { - args[len - 1].call(this, { NetHandle, blocked }); - } else if (args[0] === 'netCapabilitiesChange') { - args[len - 1].call(this, { NetHandle, NetCap }); - } else if (args[0] === 'netConnectionPropertiesChange') { - args[len - 1].call(this, { NetHandle, ConnectionProperties }); - } else if (args[0] === 'netLosing') { - args[len - 1].call(this, { - NetHandle, - maxMsToLive: "[PC Preview] unknow maxMsToLive" - }); - } else if (args[0] === 'netLost') { - args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); - } else if (args[0] === 'netUnavailable') { - args[len - 1].call(this, paramMock.businessErrorMock); - } - } - }, - addNetStatusCallback: function (...args) { - console.warn("net.connection.addNetStatusCallback interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - removeNetStatusCallback: function (...args) { - console.warn("net.connection.removeNetStatusCallback interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - getAppNet: function (...args) { - console.warn("net.connection.getAppNet interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); - } else { - return new Promise((resolve, reject) => { - resolve(NetHandle); - }) - } - }, - setAppNet: function (...args) { - console.warn("net.connection.setAppNet interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - getDefaultNet: function (...args) { - console.warn("net.connection.getDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); - } else { - return new Promise((resolve, reject) => { - resolve(NetHandle); - }) - } - }, - getAllNets: function (...args) { - console.warn("net.connection.getAllNets interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); - } else { - return new Promise((resolve, reject) => { - resolve(NetHandle); - }) - } - }, - getDefaultHttpProxy: function (...args) { - console.warn("net.connection.getDefaultHttpProxy interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, HttpProxy); - } else { - return new Promise((resolve, reject) => { - resolve(HttpProxy); - }) - } - }, - getConnectionProperties: function (...args) { - console.warn("net.connection.getConnectionProperties interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, ConnectionProperties); - } else { - return new Promise((resolve, reject) => { - resolve(ConnectionProperties); - }) - } - }, - getNetCapabilities: function (...args) { - console.warn("net.connection.getNetCapabilities interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetCapabilities); - } else { - return new Promise((resolve, reject) => { - resolve(NetCapabilities); - }) - } - }, - getBackgroundPolicy: function (...args) { - console.warn("net.connection.getBackgroundPolicy interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, BackgroundPolicy); - } else { - return new Promise((resolve, reject) => { - resolve(BackgroundPolicy); - }) - } - }, - isDefaultNetMetered: function (...args) { - console.warn("net.connection.isDefaultNetMetered interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); - } else { - return new Promise((resolve, reject) => { - resolve(paramMock.paramBooleanMock); - }) - } - }, - hasDefaultNet: function (...args) { - console.warn("net.connection.hasDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); - } else { - return new Promise((resolve, reject) => { - resolve(paramMock.paramBooleanMock); - }) - } - }, - enableAirplaneMode: function (...args) { - console.warn("net.connection.enableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - disableAirplaneMode: function (...args) { - console.warn("net.connection.disableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - enableDistributedCellularData: function (...args) { - console.warn("net.connection.enableDistributedCellularData interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - disableDistributedCellularData: function (...args) { - console.warn("net.connection.disableDistributedCellularData interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - reportNetConnected: function (...args) { - console.warn("net.connection.reportNetConnected interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - reportNetDisconnected: function (...args) { - console.warn("net.connection.reportNetDisconnected interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - } - return connection -} +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { paramMock } from "../utils" + +export function mockConnection() { + const NetSpecifierMock = { + netCapabilities: NetCapabilitiesMock, + bearerPrivateIdentifier: '[PC preview] unknow bearerPrivateIdentifier' + } + + const NetCapabilitiesMock = { + linkUpBandwidthKbps: '[PC preview] unknow linkUpBandwidthKbps', + linkDownBandwidthKbps: '[PC preview] unknow linkDownBandwidthKbps', + networkCap:[netCapMock], + bearerTypes:[netBearTypeMock] + } + + const netCapMock = '[PC preview] unknow NetCap' + + const netBearTypeMock = '[PC preview] unknow NetBearType' + + const ConnectionPropertiesMock = { + interfaceName: '[PC preview] unknow interfaceName', + domains: '[PC preview] unknow domains', + linkAddresses: [LinkAddressMock], + dnses: [NetAddressMock], + routes: [RouteInfoMock], + mtu: '[PC preview] unknow mtu' + } + + const LinkAddressMock = { + address: NetAddressMock, + prefixLength: '[PC preview] unknow prefixLength' + } + + const NetAddressMock = { + address: '[PC preview] unknow address', + family: '[PC preview] unknow family', + port: '[PC preview] unknow port' + } + + const RouteInfoMock = { + interface: '[PC preview] unknow interface', + destination: LinkAddressMock, + gateway: NetAddressMock, + hasGateway: '[PC preview] unknow hasGateway', + isDefaultRoute: '[PC preview] unknow isDefaultRoute' + } + + const netConnection = { + on: function (...args) { + console.warn("netConnection.on interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + if (args[0] === 'netAvailable') { + args[len - 1].call(this, netHandle); + } else if (args[0] === 'netBlockStatusChange') { + var array = new Array(netHandle, paramMock.paramBooleanMock); + args[len - 1].call(this, array); + } else if (args[0] === 'netCapabilitiesChange') { + var array = new Array(netHandle, NetCapabilitiesMock); + args[len - 1].call(this, array); + } else if (args[0] === 'netConnectionPropertiesChange') { + var array = new Array(netHandle, ConnectionPropertiesMock); + args[len - 1].call(this, array); + } else if (args[0] === 'netLost') { + args[len - 1].call(this, netHandle); + } else if (args[0] === 'netUnavailable') { + args[len - 1].call(this); + } + } + }, + + register: function (...args) { + console.warn("netConnection.register interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (len > 0 && typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve, reject) => { + resolve() + }) + } + }, + + unregister: function (...args) { + console.warn("netConnection.unregister interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (len > 0 && typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve, reject) => { + resolve() + }) + } + } + } + + const netHandle = { + netId: '[PC preview] unknow netId', + + bindSocket: function (...args) { + console.warn("netHandle.bindSocket interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (len > 0 && typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve, reject) => { + resolve() + }) + } + }, + + getAddressesByName: function (...args) { + console.warn("netHandle.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (len > 0 && typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, [NetAddressMock]) + } else { + return new Promise((resolve, reject) => { + resolve([NetAddressMock]) + }) + } + }, + + getAddressByName: function (...args) { + console.warn("netHandle.getAddressByName interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (len > 0 && typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, NetAddressMock) + } else { + return new Promise((resolve, reject) => { + resolve(NetAddressMock) + }) + } + } + } + + const connection = { + createNetConnection: function () { + console.warn("connection.createNetConnection interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + return netConnection; + }, + + getDefaultNet: function (...args) { + console.warn("connection.getDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, netHandle) + } else { + return new Promise((resolve) => { + resolve(netHandle) + }) + } + }, + + getAllNets: function (...args) { + console.warn("connection.getAllNets interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, [netHandle]) + } else { + return new Promise((resolve) => { + resolve([netHandle]) + }) + } + }, + + getConnectionProperties: function (...args) { + console.warn("connection.getConnectionProperties interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, ConnectionPropertiesMock) + } else { + return new Promise((resolve) => { + resolve(ConnectionPropertiesMock) + }) + } + }, + + getNetCapabilities: function (...args) { + console.warn("connection.getNetCapabilities interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, NetCapabilitiesMock) + } else { + return new Promise((resolve) => { + resolve(NetCapabilitiesMock) + }) + } + }, + + hasDefaultNet: function (...args) { + console.warn("connection.hasDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock) + } else { + return new Promise((resolve) => { + resolve(paramMock.paramBooleanMock) + }) + } + }, + + enableAirplaneMode: function (...args) { + console.warn("connection.enableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + disableAirplaneMode: function (...args) { + console.warn("connection.disableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + reportNetConnected: function (...args) { + console.warn("connection.reportNetConnected interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + reportNetDisconnected: function (...args) { + console.warn("connection.reportNetDisconnected interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + getAddressesByName: function (...args) { + console.warn("connection.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, [NetAddressMock]) + } else { + return new Promise((resolve) => { + resolve([NetAddressMock]) + }) + } + }, + + getAddressByName: function (...args) { + console.warn("connection.getAddressByName interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, NetAddressMock) + } else { + return new Promise((resolve) => { + resolve(NetAddressMock) + }) + } + }, + + getAppNet: function (...args) { + console.warn("connection.getAppNet interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, netHandle) + } else { + return new Promise((resolve) => { + resolve(netHandle) + }) + } + }, + + setAppNet: function (...args) { + console.warn("connection.setAppNet interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + registerReportNetConnectCallback: function (...args) { + console.warn("connection.registerReportNetConnectCallback interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + unregisterReportNetConnectCallback: function (...args) { + console.warn("connection.unregisterReportNetConnectCallback interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + getSpecificNet: function (...args) { + console.warn("connection.getSpecificNet interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, Array(paramMock.paramNumberMock)) + } else { + return new Promise((resolve) => { + resolve(Array(paramMock.paramNumberMock)) + }) + } + }, + + restoreFactoryData: function (...args) { + console.warn("connection.restoreFactoryData interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + + }; + + return connection; +} \ No newline at end of file diff --git a/runtime/main/extend/systemplugin/napi/ohos_net_http.js b/runtime/main/extend/systemplugin/napi/ohos_net_http.js index 18c21004..05ce0d4d 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_net_http.js +++ b/runtime/main/extend/systemplugin/napi/ohos_net_http.js @@ -61,10 +61,11 @@ export function mockHttp() { } const HttpResponseMock = { result: "[PC Preview] unknow result", - responseCode: "[PC Preview] unknow responseCode", - header: "[PC Preview] unknow header" + responseCode: responseCodeMock, + header: "[PC Preview] unknow header", + cookies: "[PC Preview] unknow cookies" } - const HttpRequestMock = { + const httpRequest = { request: function (...args) { console.warn("HttpRequest.request interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") @@ -86,7 +87,11 @@ export function mockHttp() { " be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); + if (args[0] === 'headerReceive') { + args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); + } else if (args[0] === 'headersReceive') { + args[len - 1].call(this, paramMock.paramObjectMock); + } } }, off: function (...args) { @@ -94,15 +99,39 @@ export function mockHttp() { " be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); + if (args[0] === 'headerReceive') { + args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); + } else if (args[0] === 'headersReceive') { + args[len - 1].call(this, paramMock.paramObjectMock); + } + } + }, + once: function (...args) { + console.warn("HttpRequest.once interface mocked in the Previewer. How this interface works on the Previewer may" + + " be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.paramObjectMock); } } } + const requestMethodMock = "[PC Preview] unknow RequestMethod" + + const HttpRequestOptionsMock = { + method: requestMethodMock, + extraData: "[PC Preview] unknow extraData", + header: "[PC Preview] unknow header", + readTimeout: "[PC Preview] unknow readTimeout", + connectTimeout: "[PC Preview] unknow connectTimeout" + } + + const responseCodeMock = "[PC Preview] unknow ResponseCode" + const http = { createHttp: function () { console.warn("net.http.createHttp interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") - return HttpRequestMock; + return httpRequest; }, getInstalledHttpResponseCache: function (...args) { console.warn("net.http.getInstalledHttpResponseCache interface mocked in the Previewer. How this interface works on the Previewer may" + diff --git a/runtime/main/extend/systemplugin/napi/ohos_net_socket.js b/runtime/main/extend/systemplugin/napi/ohos_net_socket.js index 446d1d78..4e57bce0 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_net_socket.js +++ b/runtime/main/extend/systemplugin/napi/ohos_net_socket.js @@ -21,17 +21,20 @@ export function mockSocket() { isClose: "[PC Preview] unknow isClose", isConnected: "[PC Preview] unknow isConnected" } + const SocketRemoteInfo = { address: "[PC Preview] unknow address", family: "[PC Preview] unknow family", port: "[PC Preview] unknow port", size: "[PC Preview] unknow size" } + const NetAddress = { address: "[PC Preview] unknow saddressize", family: "[PC Preview] unknow family", port: "[PC Preview] unknow port " } + const UDPSocket = { bind: function (...args) { console.warn("UDPSocket.bind interface mocked in the Previewer. How this interface works on the Previewer" + @@ -105,6 +108,8 @@ export function mockSocket() { }); } else if (args[0] === 'listening') { args[len - 1].call(this, paramMock.businessErrorMock); + } else if (args[0] === 'close') { + args[len - 1].call(this, paramMock.businessErrorMock); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -122,6 +127,8 @@ export function mockSocket() { }); } else if (args[0] === 'listening') { args[len - 1].call(this, paramMock.businessErrorMock); + } else if (args[0] === 'close') { + args[len - 1].call(this, paramMock.businessErrorMock); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -224,7 +231,9 @@ export function mockSocket() { message: "[PC Preview] unknow message", remoteInfo: SocketRemoteInfo }); - } else if (args[0] === 'listening') { + } else if (args[0] === 'connect') { + args[len - 1].call(this, paramMock.businessErrorMock); + } else if (args[0] === 'close') { args[len - 1].call(this, paramMock.businessErrorMock); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); @@ -241,7 +250,9 @@ export function mockSocket() { message: "[PC Preview] unknow message", remoteInfo: SocketRemoteInfo }); - } else if (args[0] === 'listening') { + } else if (args[0] === 'connect') { + args[len - 1].call(this, paramMock.businessErrorMock); + } else if (args[0] === 'close') { args[len - 1].call(this, paramMock.businessErrorMock); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); @@ -249,6 +260,40 @@ export function mockSocket() { } } } + + const UDPSendOptionsMock = { + data: "[PC Preview] unknow data", + address: NetAddress, + } + + const ExtraOptionsBaseMock = { + receiveBufferSize: "[PC Preview] unknow receiveBufferSize", + sendBufferSize: "[PC Preview] unknow sendBufferSize", + reuseAddress: "[PC Preview] unknow reuseAddress", + socketTimeout: "[PC Preview] unknow socketTimeout", + } + + const UDPExtraOptionsMock = { + broadcast: "[PC Preview] unknow broadcast", + } + + const TCPConnectionOptionsMock = { + address: NetAddress, + timeout: "[PC Preivew] unknow timeout", + } + + const TCPSendOptionsMock = { + data: "[PC Preview] unknow data", + encoding: "[PC Preview] unknow encoding", + } + + const TCPExtraOptionsMock = { + keepAlive: "[PC Preview] unknow keepAlive", + OOBInline: "[PC Preview] unknow OOBInline", + TCPNoDelay: "[PC Preview] unknow TCPNoDelay", + socketLinger: "[PC Preview] unknow socketLinger", + } + const socket = { constructUDPSocketInstance: function () { console.warn("net.socket.constructUDPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" + @@ -259,7 +304,12 @@ export function mockSocket() { console.warn("net.socket.constructTCPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") return TCPSocket; + }, + NetAddress: function() { + console.warn("net.socket.NetAddress interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device."); + return NetAddress; } } - return socket -} \ No newline at end of file + return socket; +} From bfc2c38f0f49f635638d49c625ebe658e02b2fe1 Mon Sep 17 00:00:00 2001 From: jiawen Date: Mon, 25 Jul 2022 14:13:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E5=88=A0=E6=8E=89=E6=97=A0=E7=94=A8=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jiawen Change-Id: I06f01af58d7312eeb6294bb199cc995a68ae3ade --- .../systemplugin/napi/ohos_net_connection.js | 218 +++++------------- .../extend/systemplugin/napi/ohos_net_http.js | 140 +++++------ .../systemplugin/napi/ohos_net_socket.js | 66 +++--- 3 files changed, 145 insertions(+), 279 deletions(-) diff --git a/runtime/main/extend/systemplugin/napi/ohos_net_connection.js b/runtime/main/extend/systemplugin/napi/ohos_net_connection.js index f9525a3f..a478e905 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_net_connection.js +++ b/runtime/main/extend/systemplugin/napi/ohos_net_connection.js @@ -15,70 +15,80 @@ import { paramMock } from "../utils" +export const NetCap = { + NET_CAPABILITY_MMS: '[PC preview] unknow NET_CAPABILITY_MMS', + NET_CAPABILITY_NOT_METERED: '[PC preview] unknow NET_CAPABILITY_NOT_METERED', + NET_CAPABILITY_INTERNET: '[PC preview] unknow NET_CAPABILITY_INTERNET', + NET_CAPABILITY_NOT_VPN: '[PC preview] unknow NET_CAPABILITY_NOT_VPN', + NET_CAPABILITY_VALIDATED: '[PC preview] unknow NET_CAPABILITY_VALIDATED' +} + +export const NetBearType = { + BEARER_CELLULAR: '[PC preview] unknow BEARER_CELLULAR', + BEARER_WIFI: '[PC preview] unknow BEARER_WIFI', + BEARER_ETHERNET: '[PC preview] unknow BEARER_ETHERNET' +} + export function mockConnection() { - const NetSpecifierMock = { - netCapabilities: NetCapabilitiesMock, + const NetSpecifier = { + netCapabilities: NetCapabilities, bearerPrivateIdentifier: '[PC preview] unknow bearerPrivateIdentifier' } - const NetCapabilitiesMock = { + const NetCapabilities = { linkUpBandwidthKbps: '[PC preview] unknow linkUpBandwidthKbps', linkDownBandwidthKbps: '[PC preview] unknow linkDownBandwidthKbps', - networkCap:[netCapMock], - bearerTypes:[netBearTypeMock] + networkCap:[NetCap], + bearerTypes:[NetBearType] } - const netCapMock = '[PC preview] unknow NetCap' - - const netBearTypeMock = '[PC preview] unknow NetBearType' - - const ConnectionPropertiesMock = { + const ConnectionProperties = { interfaceName: '[PC preview] unknow interfaceName', domains: '[PC preview] unknow domains', - linkAddresses: [LinkAddressMock], - dnses: [NetAddressMock], - routes: [RouteInfoMock], + linkAddresses: [LinkAddress], + dnses: [NetAddress], + routes: [RouteInfo], mtu: '[PC preview] unknow mtu' } - const LinkAddressMock = { - address: NetAddressMock, + const LinkAddress = { + address: NetAddress, prefixLength: '[PC preview] unknow prefixLength' } - const NetAddressMock = { + const NetAddress = { address: '[PC preview] unknow address', family: '[PC preview] unknow family', port: '[PC preview] unknow port' } - const RouteInfoMock = { + const RouteInfo = { interface: '[PC preview] unknow interface', - destination: LinkAddressMock, - gateway: NetAddressMock, + destination: LinkAddress, + gateway: NetAddress, hasGateway: '[PC preview] unknow hasGateway', isDefaultRoute: '[PC preview] unknow isDefaultRoute' } - const netConnection = { + const NetConnection = { on: function (...args) { - console.warn("netConnection.on interface mocked in the Previewer. How this interface works on the Previewer" + + console.warn("NetConnection.on interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { if (args[0] === 'netAvailable') { - args[len - 1].call(this, netHandle); + args[len - 1].call(this, NetHandle); } else if (args[0] === 'netBlockStatusChange') { - var array = new Array(netHandle, paramMock.paramBooleanMock); + var array = new Array(NetHandle, paramMock.paramBooleanMock); args[len - 1].call(this, array); } else if (args[0] === 'netCapabilitiesChange') { - var array = new Array(netHandle, NetCapabilitiesMock); + var array = new Array(NetHandle, NetCapabilities); args[len - 1].call(this, array); } else if (args[0] === 'netConnectionPropertiesChange') { - var array = new Array(netHandle, ConnectionPropertiesMock); + var array = new Array(NetHandle, ConnectionProperties); args[len - 1].call(this, array); } else if (args[0] === 'netLost') { - args[len - 1].call(this, netHandle); + args[len - 1].call(this, NetHandle); } else if (args[0] === 'netUnavailable') { args[len - 1].call(this); } @@ -86,80 +96,61 @@ export function mockConnection() { }, register: function (...args) { - console.warn("netConnection.register interface mocked in the Previewer. How this interface works on the Previewer" + + console.warn("NetConnection.register interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") const len = args.length if (len > 0 && typeof args[len - 1] === 'function') { args[len - 1].call(this, paramMock.businessErrorMock) - } else { - return new Promise((resolve, reject) => { - resolve() - }) } }, unregister: function (...args) { - console.warn("netConnection.unregister interface mocked in the Previewer. How this interface works on the Previewer" + + console.warn("NetConnection.unregister interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") const len = args.length if (len > 0 && typeof args[len - 1] === 'function') { args[len - 1].call(this, paramMock.businessErrorMock) - } else { - return new Promise((resolve, reject) => { - resolve() - }) } } } - const netHandle = { + const NetHandle = { netId: '[PC preview] unknow netId', - bindSocket: function (...args) { - console.warn("netHandle.bindSocket interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (len > 0 && typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock) - } else { - return new Promise((resolve, reject) => { - resolve() - }) - } - }, - getAddressesByName: function (...args) { - console.warn("netHandle.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + + console.warn("NetHandle.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") const len = args.length if (len > 0 && typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, [NetAddressMock]) + args[len - 1].call(this, paramMock.businessErrorMock, [NetAddress]) } else { return new Promise((resolve, reject) => { - resolve([NetAddressMock]) + resolve([NetAddress]) }) } }, getAddressByName: function (...args) { - console.warn("netHandle.getAddressByName interface mocked in the Previewer. How this interface works on the Previewer" + + console.warn("NetHandle.getAddressByName interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") const len = args.length if (len > 0 && typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetAddressMock) + args[len - 1].call(this, paramMock.businessErrorMock, NetAddress) } else { return new Promise((resolve, reject) => { - resolve(NetAddressMock) + resolve(NetAddress) }) } } } const connection = { + NetCap, + NetBearType, createNetConnection: function () { console.warn("connection.createNetConnection interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") - return netConnection; + return NetConnection; }, getDefaultNet: function (...args) { @@ -167,10 +158,10 @@ export function mockConnection() { " may be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, netHandle) + args[len - 1].call(this, paramMock.businessErrorMock, NetHandle) } else { return new Promise((resolve) => { - resolve(netHandle) + resolve(NetHandle) }) } }, @@ -180,10 +171,10 @@ export function mockConnection() { " may be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, [netHandle]) + args[len - 1].call(this, paramMock.businessErrorMock, [NetHandle]) } else { return new Promise((resolve) => { - resolve([netHandle]) + resolve([NetHandle]) }) } }, @@ -193,10 +184,10 @@ export function mockConnection() { " may be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, ConnectionPropertiesMock) + args[len - 1].call(this, paramMock.businessErrorMock, ConnectionProperties) } else { return new Promise((resolve) => { - resolve(ConnectionPropertiesMock) + resolve(ConnectionProperties) }) } }, @@ -206,10 +197,10 @@ export function mockConnection() { " may be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetCapabilitiesMock) + args[len - 1].call(this, paramMock.businessErrorMock, NetCapabilities) } else { return new Promise((resolve) => { - resolve(NetCapabilitiesMock) + resolve(NetCapabilities) }) } }, @@ -284,106 +275,13 @@ export function mockConnection() { " may be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, [NetAddressMock]) + args[len - 1].call(this, paramMock.businessErrorMock, [NetAddress]) } else { return new Promise((resolve) => { - resolve([NetAddressMock]) + resolve([NetAddress]) }) } }, - - getAddressByName: function (...args) { - console.warn("connection.getAddressByName interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, NetAddressMock) - } else { - return new Promise((resolve) => { - resolve(NetAddressMock) - }) - } - }, - - getAppNet: function (...args) { - console.warn("connection.getAppNet interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, netHandle) - } else { - return new Promise((resolve) => { - resolve(netHandle) - }) - } - }, - - setAppNet: function (...args) { - console.warn("connection.setAppNet interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock) - } else { - return new Promise((resolve) => { - resolve() - }) - } - }, - - registerReportNetConnectCallback: function (...args) { - console.warn("connection.registerReportNetConnectCallback interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock) - } else { - return new Promise((resolve) => { - resolve() - }) - } - }, - - unregisterReportNetConnectCallback: function (...args) { - console.warn("connection.unregisterReportNetConnectCallback interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock) - } else { - return new Promise((resolve) => { - resolve() - }) - } - }, - - getSpecificNet: function (...args) { - console.warn("connection.getSpecificNet interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, Array(paramMock.paramNumberMock)) - } else { - return new Promise((resolve) => { - resolve(Array(paramMock.paramNumberMock)) - }) - } - }, - - restoreFactoryData: function (...args) { - console.warn("connection.restoreFactoryData interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock) - } else { - return new Promise((resolve) => { - resolve() - }) - } - }, - - }; return connection; diff --git a/runtime/main/extend/systemplugin/napi/ohos_net_http.js b/runtime/main/extend/systemplugin/napi/ohos_net_http.js index 05ce0d4d..b343812c 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_net_http.js +++ b/runtime/main/extend/systemplugin/napi/ohos_net_http.js @@ -15,66 +15,72 @@ import { paramMock } from "../utils" +export const RequestMethod = { + OPTIONS: "[PC Preview] unknow OPTIONS", + GET: "[PC Preview] unknow GET", + HEAD: "[PC Preview] unknow HEAD", + POST: "[PC Preview] unknow POST", + PUT: "[PC Preview] unknow PUT", + DELETE: "[PC Preview] unknow DELETE", + TRACE: "[PC Preview] unknow TRACE", + CONNECT: "[PC Preview] unknow CONNECT" +} + +export const ResponseCode = { + OK: "[PC Preview] unknow OK", + CREATED: "[PC Preview] unknow CREATED", + ACCEPTED: "[PC Preview] unknow ACCEPTED", + NOT_AUTHORITATIVE: "[PC Preview] unknow NOT_AUTHORITATIVE", + NO_CONTENT: "[PC Preview] unknow NO_CONTENT", + RESET: "[PC Preview] unknow RESET", + PARTIAL: "[PC Preview] unknow PARTIAL", + MULT_CHOICE: "[PC Preview] unknow MULT_CHOICE", + MOVED_PERM: "[PC Preview] unknow MOVED_PERM", + MOVED_TEMP: "[PC Preview] unknow MOVED_TEMP", + SEE_OTHER: "[PC Preview] unknow SEE_OTHER", + NOT_MODIFIED: "[PC Preview] unknow NOT_MODIFIED", + USE_PROXY: "[PC Preview] unknow USE_PROXY", + BAD_REQUEST: "[PC Preview] unknow BAD_REQUEST", + UNAUTHORIZED: "[PC Preview] unknow UNAUTHORIZED", + PAYMENT_REQUIRED: "[PC Preview] unknow PAYMENT_REQUIRED", + FORBIDDEN: "[PC Preview] unknow FORBIDDEN", + NOT_FOUND: "[PC Preview] unknow NOT_FOUND", + BAD_METHOD: "[PC Preview] unknow BAD_METHOD", + NOT_ACCEPTABLE: "[PC Preview] unknow NOT_ACCEPTABLE", + PROXY_AUTH: "[PC Preview] unknow PROXY_AUTH", + CLIENT_TIMEOUT: "[PC Preview] unknow CLIENT_TIMEOUT", + CONFLICT: "[PC Preview] unknow CONFLICT", + GONE: "[PC Preview] unknow GONE", + LENGTH_REQUIRED: "[PC Preview] unknow LENGTH_REQUIRED", + PRECON_FAILED: "[PC Preview] unknow PRECON_FAILED", + ENTITY_TOO_LARGE: "[PC Preview] unknow ENTITY_TOO_LARGE", + REQ_TOO_LONG: "[PC Preview] unknow REQ_TOO_LONG", + UNSUPPORTED_TYPE: "[PC Preview] unknow UNSUPPORTED_TYPE", + INTERNAL_ERROR: "[PC Preview] unknow INTERNAL_ERROR", + NOT_IMPLEMENTED: "[PC Preview] unknow NOT_IMPLEMENTED", + BAD_GATEWAY: "[PC Preview] unknow BAD_GATEWAY", + UNAVAILABLE: "[PC Preview] unknow UNAVAILABLE", + GATEWAY_TIMEOUT: "[PC Preview] unknow GATEWAY_TIMEOUT", + VERSION: "[PC Preview] unknow VERSION" +} + export function mockHttp() { - const HttpResponseCacheOptions = { - filePath: "[PC Preview] unknow filePath", - fileChildPath: "[PC Preview] unknow fileChildPath", - cacheSize: "[PC Preview] unknow cacheSize" - } - const HttpResponseCache = { - close: function (...args) { - console.warn("net.HttpResponseCache.close interface mocked in the Previewer. How this interface works on the Previewer may" + - " be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - delete: function (...args) { - console.warn("net.HttpResponseCache.delete interface mocked in the Previewer. How this interface works on the Previewer may" + - " be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - }, - flush: function (...args) { - console.warn("net.HttpResponseCache.flush interface mocked in the Previewer. How this interface works on the Previewer may" + - " be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock); - } else { - return new Promise((resolve, reject) => { - resolve(); - }) - } - } - } - const HttpResponseMock = { + const HttpResponse = { result: "[PC Preview] unknow result", - responseCode: responseCodeMock, + responseCode: ResponseCode, header: "[PC Preview] unknow header", cookies: "[PC Preview] unknow cookies" } - const httpRequest = { + const HttpRequest = { request: function (...args) { console.warn("HttpRequest.request interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") const len = args.length if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, HttpResponseMock); + args[len - 1].call(this, paramMock.businessErrorMock, HttpResponse); } else { return new Promise((resolve, reject) => { - resolve(HttpResponseMock); + resolve(HttpResponse); }) } }, @@ -115,48 +121,24 @@ export function mockHttp() { } } } - const requestMethodMock = "[PC Preview] unknow RequestMethod" - const HttpRequestOptionsMock = { - method: requestMethodMock, + const HttpRequestOptions = { + method: RequestMethod, extraData: "[PC Preview] unknow extraData", header: "[PC Preview] unknow header", readTimeout: "[PC Preview] unknow readTimeout", connectTimeout: "[PC Preview] unknow connectTimeout" } - const responseCodeMock = "[PC Preview] unknow ResponseCode" - const http = { + RequestMethod, + ResponseCode, createHttp: function () { console.warn("net.http.createHttp interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") - return httpRequest; + return HttpRequest; }, - getInstalledHttpResponseCache: function (...args) { - console.warn("net.http.getInstalledHttpResponseCache interface mocked in the Previewer. How this interface works on the Previewer may" + - " be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, HttpResponseCache); - } else { - return new Promise((resolve, reject) => { - resolve(HttpResponseCache); - }) - } - }, - createHttpResponseCache: function (...args) { - console.warn("net.http.createHttpResponseCache interface mocked in the Previewer. How this interface works on the Previewer may" + - " be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, HttpResponseCacheOptions); - } else { - return new Promise((resolve, reject) => { - resolve(HttpResponseCache); - }) - } - } } - return http + + return http; } \ No newline at end of file diff --git a/runtime/main/extend/systemplugin/napi/ohos_net_socket.js b/runtime/main/extend/systemplugin/napi/ohos_net_socket.js index 4e57bce0..c889c7e9 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_net_socket.js +++ b/runtime/main/extend/systemplugin/napi/ohos_net_socket.js @@ -102,14 +102,11 @@ export function mockSocket() { const len = args.length if (typeof args[len - 1] === 'function') { if (args[0] === 'message') { - args[len - 1].call(this, { - message: "[PC Preview] unknow message", - remoteInfo: SocketRemoteInfo - }); + args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo}); } else if (args[0] === 'listening') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this); } else if (args[0] === 'close') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -121,14 +118,11 @@ export function mockSocket() { const len = args.length if (typeof args[len - 1] === 'function') { if (args[0] === 'message') { - args[len - 1].call(this, { - message: "[PC Preview] unknow message", - remoteInfo: SocketRemoteInfo - }); + args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo}); } else if (args[0] === 'listening') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this); } else if (args[0] === 'close') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -227,14 +221,11 @@ export function mockSocket() { const len = args.length if (typeof args[len - 1] === 'function') { if (args[0] === 'message') { - args[len - 1].call(this, { - message: "[PC Preview] unknow message", - remoteInfo: SocketRemoteInfo - }); + args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo}); } else if (args[0] === 'connect') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this); } else if (args[0] === 'close') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -246,14 +237,11 @@ export function mockSocket() { const len = args.length if (typeof args[len - 1] === 'function') { if (args[0] === 'message') { - args[len - 1].call(this, { - message: "[PC Preview] unknow message", - remoteInfo: SocketRemoteInfo - }); + args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo}); } else if (args[0] === 'connect') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this); } else if (args[0] === 'close') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -261,39 +249,41 @@ export function mockSocket() { } } - const UDPSendOptionsMock = { + const UDPSendOptions = { data: "[PC Preview] unknow data", - address: NetAddress, + address: NetAddress } - const ExtraOptionsBaseMock = { + const ExtraOptionsBase = { receiveBufferSize: "[PC Preview] unknow receiveBufferSize", sendBufferSize: "[PC Preview] unknow sendBufferSize", reuseAddress: "[PC Preview] unknow reuseAddress", - socketTimeout: "[PC Preview] unknow socketTimeout", + socketTimeout: "[PC Preview] unknow socketTimeout" } - const UDPExtraOptionsMock = { - broadcast: "[PC Preview] unknow broadcast", + const UDPExtraOptions = { + broadcast: "[PC Preview] unknow broadcast" } - const TCPConnectionOptionsMock = { + const TCPConnectOptions = { address: NetAddress, timeout: "[PC Preivew] unknow timeout", } - const TCPSendOptionsMock = { + const TCPSendOptions = { data: "[PC Preview] unknow data", encoding: "[PC Preview] unknow encoding", } - const TCPExtraOptionsMock = { + const TCPExtraOptions = { keepAlive: "[PC Preview] unknow keepAlive", OOBInline: "[PC Preview] unknow OOBInline", TCPNoDelay: "[PC Preview] unknow TCPNoDelay", - socketLinger: "[PC Preview] unknow socketLinger", + socketLinger: { + on: "[PC Preview] unknow on", + linger: "[PC Preview] unknow linger" + } } - const socket = { constructUDPSocketInstance: function () { console.warn("net.socket.constructUDPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" + @@ -304,12 +294,8 @@ export function mockSocket() { console.warn("net.socket.constructTCPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" + " may be different from that on a real device.") return TCPSocket; - }, - NetAddress: function() { - console.warn("net.socket.NetAddress interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device."); - return NetAddress; } } + return socket; }