1import { paramMock } from "./utils" 2 3export function mockWebSocket() { 4 global.systemplugin.net = {} 5 const WebSocketMock = { 6 connect: function (...args) { 7 console.warn("WebSocket.connect interface mocked in the Previewer. How this interface works on the Previewer" + 8 " may be different from that on a real device.") 9 const len = args.length 10 if (typeof args[len - 1] === 'function') { 11 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 12 } else { 13 return new Promise((resolve, reject) => { 14 resolve(paramMock.paramBooleanMock); 15 }) 16 } 17 }, 18 send: function (...args) { 19 console.warn("WebSocket.send interface mocked in the Previewer. How this interface works on the Previewer may" + 20 " be different from that on a real device.") 21 const len = args.length 22 if (typeof args[len - 1] === 'function') { 23 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 24 } else { 25 return new Promise((resolve, reject) => { 26 resolve(paramMock.paramBooleanMock); 27 }) 28 } 29 }, 30 close: function (...args) { 31 console.warn("WebSocket.close interface mocked in the Previewer. How this interface works on the Previewer " + 32 "may be different from that on a real device.") 33 const len = args.length 34 if (typeof args[len - 1] === 'function') { 35 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 36 } else { 37 return new Promise((resolve, reject) => { 38 resolve(paramMock.paramBooleanMock); 39 }) 40 } 41 }, 42 on: function (...args) { 43 console.warn("WebSocket.on interface mocked in the Previewer. How this interface works on the Previewer may " + 44 "be different from that on a real device.") 45 const len = args.length 46 if (typeof args[len - 1] === 'function') { 47 if (args[0] == 'open') { 48 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 49 } else if (args[0] == 'message') { 50 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock); 51 } else if (args[0] == 'close') { 52 args[len - 1].call(this, { 53 code: "[PC Preview] unknow code", 54 reason: "[PC Preview] unknow reason" 55 }); 56 } else if (args[0] == 'error') { 57 args[len - 1].call(this, paramMock.businessErrorMock); 58 } 59 } 60 }, 61 off: function (...args) { 62 console.warn("WebSocket.off interface mocked in the Previewer. How this interface works on the Previewer may" + 63 " be different from that on a real device.") 64 const len = args.length 65 if (typeof args[len - 1] === 'function') { 66 if (args[0] == 'open') { 67 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 68 } else if (args[0] == 'message') { 69 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock); 70 } else if (args[0] == 'close') { 71 args[len - 1].call(this, { 72 code: "[PC Preview] unknow code", 73 reason: "[PC Preview] unknow reason" 74 }); 75 } else if (args[0] == 'error') { 76 args[len - 1].call(this, paramMock.businessErrorMock); 77 } 78 } 79 } 80 } 81 global.systemplugin.net.webSocket = { 82 createWebSocket: function () { 83 console.warn("net.webSocket.createWebSocket interface mocked in the Previewer. How this interface works on the" + 84 " Previewer may be different from that on a real device.") 85 return WebSocketMock; 86 } 87 } 88} 89 90export function mockHttp() { 91 const HttpResponseMock = { 92 result: "[PC Preview] unknow result", 93 responseCode: "[PC Preview] unknow responseCode", 94 header: "[PC Preview] unknow header" 95 } 96 const HttpRequestMock = { 97 request: function (...args) { 98 console.warn("HttpRequest.request interface mocked in the Previewer. How this interface works on the Previewer" + 99 " may be different from that on a real device.") 100 const len = args.length 101 if (typeof args[len - 1] === 'function') { 102 args[len - 1].call(this, paramMock.businessErrorMock, HttpResponseMock); 103 } else { 104 return new Promise((resolve, reject) => { 105 resolve(HttpResponseMock); 106 }) 107 } 108 }, 109 destroy: function () { 110 console.warn("HttpRequest.destroy interface mocked in the Previewer. How this interface works on the Previewer" + 111 " may be different from that on a real device.") 112 }, 113 on: function (...args) { 114 console.warn("HttpRequest.on interface mocked in the Previewer. How this interface works on the Previewer may" + 115 " be different from that on a real device.") 116 const len = args.length 117 if (typeof args[len - 1] === 'function') { 118 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 119 } 120 }, 121 off: function (...args) { 122 console.warn("HttpRequest.off interface mocked in the Previewer. How this interface works on the Previewer may" + 123 " be different from that on a real device.") 124 const len = args.length 125 if (typeof args[len - 1] === 'function') { 126 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 127 } 128 } 129 } 130 global.systemplugin.net.http = { 131 createHttp: function () { 132 console.warn("net.http.createHttp interface mocked in the Previewer. How this interface works on the Previewer" + 133 " may be different from that on a real device.") 134 return HttpRequestMock; 135 } 136 } 137}