From 427f871ba89554625b23cf1093f1714fd35f87d6 Mon Sep 17 00:00:00 2001 From: jiawen Date: Mon, 25 Jul 2022 14:06:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=AE=A1=E7=90=86js=20mock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jiawen Change-Id: Ib6a8cd67315b9516c91f174724903ace61b62e23 --- .../systemplugin/napi/ohos_net_connection.js | 675 ++++++++---------- .../extend/systemplugin/napi/ohos_net_http.js | 165 +++-- .../systemplugin/napi/ohos_net_socket.js | 84 ++- 3 files changed, 436 insertions(+), 488 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..a478e905 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_net_connection.js +++ b/runtime/main/extend/systemplugin/napi/ohos_net_connection.js @@ -1,387 +1,288 @@ -/* - * 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 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 NetSpecifier = { + netCapabilities: NetCapabilities, + bearerPrivateIdentifier: '[PC preview] unknow bearerPrivateIdentifier' + } + + const NetCapabilities = { + linkUpBandwidthKbps: '[PC preview] unknow linkUpBandwidthKbps', + linkDownBandwidthKbps: '[PC preview] unknow linkDownBandwidthKbps', + networkCap:[NetCap], + bearerTypes:[NetBearType] + } + + const ConnectionProperties = { + interfaceName: '[PC preview] unknow interfaceName', + domains: '[PC preview] unknow domains', + linkAddresses: [LinkAddress], + dnses: [NetAddress], + routes: [RouteInfo], + mtu: '[PC preview] unknow mtu' + } + + const LinkAddress = { + address: NetAddress, + prefixLength: '[PC preview] unknow prefixLength' + } + + const NetAddress = { + address: '[PC preview] unknow address', + family: '[PC preview] unknow family', + port: '[PC preview] unknow port' + } + + const RouteInfo = { + interface: '[PC preview] unknow interface', + destination: LinkAddress, + gateway: NetAddress, + 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, NetCapabilities); + args[len - 1].call(this, array); + } else if (args[0] === 'netConnectionPropertiesChange') { + var array = new Array(NetHandle, ConnectionProperties); + 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) + } + }, + + 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) + } + } + } + + const NetHandle = { + netId: '[PC preview] unknow netId', + + 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, [NetAddress]) + } else { + return new Promise((resolve, reject) => { + resolve([NetAddress]) + }) + } + }, + + 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, NetAddress) + } else { + return new Promise((resolve, reject) => { + 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; + }, + + 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, ConnectionProperties) + } else { + return new Promise((resolve) => { + resolve(ConnectionProperties) + }) + } + }, + + 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, NetCapabilities) + } else { + return new Promise((resolve) => { + resolve(NetCapabilities) + }) + } + }, + + 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, [NetAddress]) + } else { + return new Promise((resolve) => { + resolve([NetAddress]) + }) + } + }, + }; + + 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..b343812c 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_net_http.js +++ b/runtime/main/extend/systemplugin/napi/ohos_net_http.js @@ -15,65 +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: "[PC Preview] unknow responseCode", - header: "[PC Preview] unknow header" + responseCode: ResponseCode, + 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.") 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); }) } }, @@ -86,7 +93,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,40 +105,40 @@ 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 HttpRequestOptions = { + method: RequestMethod, + extraData: "[PC Preview] unknow extraData", + header: "[PC Preview] unknow header", + readTimeout: "[PC Preview] unknow readTimeout", + connectTimeout: "[PC Preview] unknow connectTimeout" + } + 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 HttpRequestMock; + 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 446d1d78..c889c7e9 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" + @@ -99,12 +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); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -116,12 +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); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -220,12 +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 - }); - } else if (args[0] === 'listening') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo}); + } else if (args[0] === 'connect') { + args[len - 1].call(this); + } else if (args[0] === 'close') { + args[len - 1].call(this); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } @@ -237,18 +237,53 @@ 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 - }); - } else if (args[0] === 'listening') { - args[len - 1].call(this, paramMock.businessErrorMock); + args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo}); + } else if (args[0] === 'connect') { + args[len - 1].call(this); + } else if (args[0] === 'close') { + args[len - 1].call(this); } else if (args[0] === 'error') { args[len - 1].call(this, paramMock.businessErrorMock); } } } } + + const UDPSendOptions = { + data: "[PC Preview] unknow data", + address: NetAddress + } + + const ExtraOptionsBase = { + receiveBufferSize: "[PC Preview] unknow receiveBufferSize", + sendBufferSize: "[PC Preview] unknow sendBufferSize", + reuseAddress: "[PC Preview] unknow reuseAddress", + socketTimeout: "[PC Preview] unknow socketTimeout" + } + + const UDPExtraOptions = { + broadcast: "[PC Preview] unknow broadcast" + } + + const TCPConnectOptions = { + address: NetAddress, + timeout: "[PC Preivew] unknow timeout", + } + + const TCPSendOptions = { + data: "[PC Preview] unknow data", + encoding: "[PC Preview] unknow encoding", + } + + const TCPExtraOptions = { + keepAlive: "[PC Preview] unknow keepAlive", + OOBInline: "[PC Preview] unknow OOBInline", + TCPNoDelay: "[PC Preview] unknow TCPNoDelay", + 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" + @@ -261,5 +296,6 @@ export function mockSocket() { return TCPSocket; } } - return socket -} \ No newline at end of file + + return socket; +}