• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { paramMock } from "../utils"
17
18export const RequestMethod = {
19  OPTIONS: "OPTIONS",
20  GET: "GET",
21  HEAD: "HEAD",
22  POST: "POST",
23  PUT: "PUT",
24  DELETE: "DELETE",
25  TRACE: "TRACE",
26  CONNECT: "CONNECT"
27};
28
29export const ResponseCode = {
30  OK: 200,
31  CREATED: 201,
32  ACCEPTED: 202,
33  NOT_AUTHORITATIVE: 203,
34  NO_CONTENT: 204,
35  RESET: 205,
36  PARTIAL: 206,
37  MULT_CHOICE: 300,
38  MOVED_PERM: 301,
39  MOVED_TEMP: 302,
40  SEE_OTHER: 303,
41  NOT_MODIFIED: 304,
42  USE_PROXY: 305,
43  BAD_REQUEST: 400,
44  UNAUTHORIZED: 401,
45  PAYMENT_REQUIRED: 402,
46  FORBIDDEN: 403,
47  NOT_FOUND: 404,
48  BAD_METHOD: 405,
49  NOT_ACCEPTABLE: 406,
50  PROXY_AUTH: 407,
51  CLIENT_TIMEOUT: 408,
52  CONFLICT: 409,
53  GONE: 410,
54  LENGTH_REQUIRED: 411,
55  PRECON_FAILED: 412,
56  ENTITY_TOO_LARGE: 413,
57  REQ_TOO_LONG: 414,
58  UNSUPPORTED_TYPE: 415,
59  INTERNAL_ERROR: 500,
60  NOT_IMPLEMENTED: 501,
61  BAD_GATEWAY: 502,
62  UNAVAILABLE: 503,
63  GATEWAY_TIMEOUT: 504,
64  VERSION: 505
65};
66
67export function mockHttp() {
68  const HttpResponse = {
69    result: "[PC Preview] unknow result",
70    responseCode: ResponseCode,
71    header: "[PC Preview] unknow header",
72    cookies: "[PC Preview] unknow cookies"
73  }
74  const HttpRequest = {
75    request: function (...args) {
76      console.warn("HttpRequest.request interface mocked in the Previewer. How this interface works on the Previewer" +
77        " may be different from that on a real device.")
78      const len = args.length
79      if (typeof args[len - 1] === 'function') {
80        args[len - 1].call(this, paramMock.businessErrorMock, HttpResponse);
81      } else {
82        return new Promise((resolve, reject) => {
83          resolve(HttpResponse);
84        })
85      }
86    },
87    destroy: function () {
88      console.warn("HttpRequest.destroy interface mocked in the Previewer. How this interface works on the Previewer" +
89        " may be different from that on a real device.")
90    },
91    on: function (...args) {
92      console.warn("HttpRequest.on interface mocked in the Previewer. How this interface works on the Previewer may" +
93        " be different from that on a real device.")
94      const len = args.length
95      if (typeof args[len - 1] === 'function') {
96        if (args[0] === 'headerReceive') {
97          args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock);
98        } else if (args[0] === 'headersReceive') {
99          args[len - 1].call(this, paramMock.paramObjectMock);
100        }
101      }
102    },
103    off: function (...args) {
104      console.warn("HttpRequest.off interface mocked in the Previewer. How this interface works on the Previewer may" +
105        " be different from that on a real device.")
106      const len = args.length
107      if (typeof args[len - 1] === 'function') {
108        if (args[0] === 'headerReceive') {
109          args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock);
110        } else if (args[0] === 'headersReceive') {
111          args[len - 1].call(this, paramMock.paramObjectMock);
112        }
113      }
114    },
115    once: function (...args) {
116      console.warn("HttpRequest.once interface mocked in the Previewer. How this interface works on the Previewer may" +
117        " be different from that on a real device.")
118      const len = args.length
119      if (typeof args[len - 1] === 'function') {
120        args[len - 1].call(this, paramMock.paramObjectMock);
121      }
122    }
123  }
124
125  const HttpRequestOptions = {
126    method: RequestMethod,
127    extraData: "[PC Preview] unknow extraData",
128    header: "[PC Preview] unknow header",
129    readTimeout: "[PC Preview] unknow readTimeout",
130    connectTimeout: "[PC Preview] unknow connectTimeout"
131  }
132
133  const http = {
134    RequestMethod,
135    ResponseCode,
136    createHttp: function () {
137      console.warn("net.http.createHttp interface mocked in the Previewer. How this interface works on the Previewer" +
138        " may be different from that on a real device.")
139      return HttpRequest;
140    },
141  }
142
143  return http;
144}