Reqman,一個可以快速幫助後端工程師進行api測試的工具,同時也是一個基於nodejs的爬蟲工具。

一拳超人發表於2019-03-08

Reqman

Reqman是一個可以快速幫助後端工程師進行api測試的工具,同時也是一個基於nodejs的爬蟲工具。

github:github.com/lisniuse/re…

安裝

這是一個通過 npm registry 提供的 Node.js 模組。

在安裝之前,下載並安裝Node.js。需要Node.js 8.0或更高版本。

使用npm install命令完成安裝:

$ npm install reqman
複製程式碼

引入使用

// 使用的是 Node.js `require()`
const Reqman = require('reqman');

// Using ES6 imports
import Reqman from 'reqman';
複製程式碼

特性

  • ✔︎ 鏈式API
  • ✔︎ 開箱即用
  • ✔︎ 可爬蟲、可模擬請求
  • ✔︎ 適用於複雜強耦合場景
  • ✔︎ 基於nodejs強大請求庫 request

超簡單的入門教程

Reqman被設計成像 request 一樣,是進行http呼叫的最簡單的方式。它支援https,預設情況下遵循重定向。

你只需要在返回一個objectpush 方法的引數中編寫一個匿名函式,返回你的請求引數即可。

例子1:單個請求

const Reqman = require('reqman');

//只需要設定一個請求基地址
const req = new Reqman({
  baseUrl: "http://127.0.0.1:3000"
});

//比如你想獲取所有使用者的統計資訊,你可以這樣寫
req
  .push(function () {
    return {
      method: "GET",
      url: `/api/user/count`
    }
  })
  .done();

複製程式碼

如果你指定showInfo引數為false,那麼就不會列印結果到螢幕上,像這樣:

req
  .push(function () {
    return {
      method: "GET",
      url: `/api/user/count`,
      showInfo: false
    }
  })
  .done();
複製程式碼

例子2: 鏈式API

鏈API中,第一個請求的結果作為第二個請求的引數。就像這樣:

const Reqman = require('reqman');

//需要設定一個請求基地址
const req = new Reqman({
  baseUrl: "http://127.0.0.1:3000"
});

//登陸的使用者賬號密碼
const user = {
  username: "admin",
  password: "admin"
}

req
  //請求登陸地址
  .push(function () {
    return {
      method: "POST",
      url: `/api/login`,
      data: user, //傳入剛剛定義的user物件到data
      complete: function (selfElement) { //返回該佇列的requestElement物件
        let response = selfElement.result.response;
        let body = JSON.parse(response.body);
        //注意:建議不要把公共變數直接掛到reqman的例項上,可能會覆蓋requman的屬性和方法,reqman提供了一個store物件用於儲存請求過程中需要儲存的公共變數。
        this.store.userToken = body.data.token; //拿到使用者token
      }
    }
  })
  //然後我們以登陸之後的獲取到的token來更新使用者的資訊。
  .push(function () {
    return {
      method: "POST",
      url: `/api/user/updateInfo`,
      headers: {
        'Authorization': this.store.userToken //之後的請求中可以直接使用store裡的變數
      },
      data: {
        nickname: "jack ma" //更新暱稱為 jack ma
      }
    }
  })
  .done()//just do it.

複製程式碼

例字3:完整體現reqman的api和特性的例子

'use strict'

const req = new Reqman({
  baseUrl: "http://127.0.0.1:3000",
  output: "./request-result.txt", //將請求後返回的結果同時追加寫入到指定的檔案路徑
  specList: {
    type: 'valid', //引數:invalid 或 valid。定義有效或者無效的請求。
    list: ['bob'] //讓名為bob的請求有效,其餘的請求失效。如果type為invalid,則相反。
  }
});

//請求鏈
req.
  //定義一個名為bob的請求,prevElement參數列示上一個請求的requestElement物件。
  push('bob', function (prevElement) {
    return {
      method: "POST",
      url: `/?name=bob`,
      headers: { //附帶自定義headers
        'content-type': 'application/octet-stream'
      },
      complete: function (selfElement) { //請求完畢後的回撥函式

      }
    }
  })
  //定義一個名為jack的請求,prevElement參數列示上一個請求的requestElement物件。
  .push('jack', function (prevElement) {
    return {
      baseUrl: 'http://127.0.0.1:4000', //為該請求自定義基地址
      output: "./jack-result.txt", //為該請求自定義輸出檔案路徑
      method: "GET",
      url: `/`,
      data: {
        name: 'jack'
      },
      showInfo: false, //不將返回的body資訊列印出來
      complete: function (selfElement) { //請求完畢後的回撥函式
        //do something...
      }
    }
  })
  .done(function () {
    //退出程式
    process.exit(1);
  })

複製程式碼

更多例子

更多例子在專案的 examples 資料夾中,你可以直接執行:

node getHelloworld.js
複製程式碼

License

The MIT License (MIT)

Copyright (c) 2015-present ZhiBing <17560235@qq.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

back to top

相關文章