# mockhttp **Repository Path**: qing-lkyi/mockhttp ## Basic Information - **Project Name**: mockhttp - **Description**: 移植自 https://github.com/ChrisSkeldon/mockHTTP。 - **Primary Language**: Unknown - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-10-20 - **Last Updated**: 2023-10-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mockHTTP ## 简介 [mockHTTP](https://github.com/ChrisSkeldon/mockHTTP) ,是一个开源的js库,用于模拟HTTP请求和响应。本库基于[mockHTTP](https://github.com/ChrisSkeldon/mockHTTP) 原库master分支(2023.10.25版本)进行适配,使其可以运行在 OpenHarmony,并沿用其现有用法和特性。 - 创建模拟http请求和响应对象 - 允许设置请求头、请求体、响应状态码、响应头和响应体等信息 - 支持模拟延迟响应和异常响应 ## 示例应用 ![](./screenshots/example.gif) 在上面的示例应用中,我们使用 mockHTTP 测试了一个本地 http 服务: - 点击 `get mainpage.txt` 会发起一个模拟的 get 请求,获取 mainpage.txt 的文件内容; - 点击 `upload alice.txt` 会发起一个模拟的 post 请求,上传 alice.txt 并替换掉 mainpage.txt; - 点击 `upload jack.txt` 同理; - 上面的三个文本框显示 response 的完整 json 信息,最下面的显示响应的内容。 ## 下载安装 ```bash ohpm install @ncc/mockhttp ``` OpenHarmony ohpm 环境配置等更多内容,请参考[如何安装 OpenHarmony ohpm 包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md) ## 使用方式 本库通过提供类``mockHTTP``的导出接口实现原[mockHTTP](https://github.com/ChrisSkeldon/mockHTTP)库的功能,可以在文件中添加以下内容导入本库 ```js import mockHTTP from '@ncc/mockhttp' ``` ## 函数列表 | **函数** | 参数 | 功能 | 返回值 | |-----------------------------------------|-------------------------------------|----------------------------|------------------------------| | mockHTTP.createRequest(config) | config: 请求配置 | 创建模拟http.IncomingMessage对象 | 根据config生成的一个新请求类``Request`` | | mockHTTP.createResponse | | 创建模拟http.ServerResponse对象 | 一个空返回类``Response`` | | Request.data(payload) | payload: 传入的数据 | 向http请求正文中添加数据 | | | Request.end | | 结束向http请求正文中添加数据 | | | Response.writeHead(httpStatus, headers) | httpStatus: http状态码 headers: http头 | 写入http响应首部和状态码 | | | Response.write(body, encoding) | body: http响应正文 encoding: 编码方式 | 向http响应正文中添加数据 | 添加结果成功/失败(true/false) | | Response.end(body, encoding) | body: http响应正文 encoding: 编码方式 | 结束向http响应正文中添加数据 | | | Response.getHeader(headerName) | headerName: http响应首部名 | 返回小写形式的对应首部 | 对应首部内容 | | Response.getBody | | 返回http响应正文 | http响应正文 | ## 使用示例 ```ts // 引入三方库 import mockHTTP from '@ncc/mockhttp' // 配置 request let config = { headers: {}, // default to {} httpVersion: '1.0', // default to '1.0' method: 'POST', // default to 'GET' url: 'http://fakeurl/upload' // default to '' }; // 创建 request 和 response let req = mockHTTP.createRequest(config), resp = mockHTTP.createResponse(); // 传入想要测试的 handler myServerHandler(req, resp); // 检查 handler 的响应情况 console.log('response Content-Type: %s', resp.getHeader('Content-Type')); console.log('response status code: %s', resp.statusCode); console.log('response body:', resp.getBody()); ``` ## 约束与限制 在下述版本验证通过: DevEco Studio: 4.0 Beta2(4.0.0.400), SDK: API10(4.0.9.6) ## 目录结构 ```text |---- ohos_mockhttp | |---- AppScrope # 示例代码文件夹 | |---- entry # 示例代码文件夹 | |---- screenshots #截图 | |---- mockHTTP # mockHTTP库文件夹 | |---- build # axios模块打包后的文件 | |---- src # 模块代码 | |---- main # 模块代码 | |---- ets/components # mockHTTP核心代码 | |---- index.js # 入口文件 | |---- index.d.ts # 声明文件 | |---- *.json5 # 配置文件 | |---- README.md # 安装使用方法 | |---- README.OpenSource # 开源说明 | |---- CHANGELOG.md # 更新日志 | |---- TEST.md # 测试用例覆盖情况 ``` ## 开源协议 本项目基于 [BSD-3-Clause](./LICENSE) ,请自由地享受和参与开源。