Postman 沙箱 API

2023-04-04 09:35 更新

Postman 為 JavaScript API 提供對象pm,您可以在 gRPC 請求腳本中使用該對象,在Postman Sandbox中執(zhí)行。

pm對象

pm對象提供測試請求和響應(yīng)數(shù)據(jù)、訪問變量和一些元信息的功能。

訪問腳本中的上下文信息

pm.request

該pm.request對象提供對腳本中請求數(shù)據(jù)的訪問。在Before invoke和After responsepm.request腳本中都可用。

以下是對象的屬性pm.request:

  • 請求網(wǎng)址:pm.request.url: Url
  • 格式的包、服務(wù)和方法名稱packageName.serviceName.methodName:pm.request.methodPath: string
  • 認證詳情:pm.request.auth: Auth
  • 隨請求發(fā)送的元數(shù)據(jù)列表:pm.request.metadata: PropertyList<{ key: string, value: string }>key單個元數(shù)據(jù)項是具有屬性和的對象value。
  • 傳出消息列表:pm.request.messages: PropertyList<{ data: any, timestamp: Date }>單個消息是具有以下屬性的對象:data:發(fā)送的消息內(nèi)容,和timestamp:發(fā)送消息的時間,表示為Date對象。對于具有一元和服務(wù)器流方法的請求,pm.request.messages將僅在索引 0 處包含一條消息,可以作為pm.request.messages.idx(0).
注意:對象不支持請求變更pm。

pm.response 

pm.response對象提供對當(dāng)前請求執(zhí)行的響應(yīng)中返回的數(shù)據(jù)的訪問。僅在After 響應(yīng)pm.response腳本中可用。

以下是對象的屬性pm.response:

  • gRPC響應(yīng)狀態(tài)碼:pm.response.statusCode: number
  • 響應(yīng)時間(以毫秒為單位):pm.response.responseTime: number對于使用流方法的請求,responseTime表示該請求執(zhí)行的總持續(xù)時間。
  • 隨響應(yīng)收到的元數(shù)據(jù)列表:pm.response.metadata: PropertyList<{ key: string, value: string }>key單個元數(shù)據(jù)項是具有屬性和的對象value。
  • 隨響應(yīng)收到的預(yù)告片列表:pm.response.trailers: PropertyList<{ key: string, value: string }>key單個拖車項目是具有屬性和的對象value。
  • 傳入消息列表:pm.response.messages: PropertyList<{ data: any, timestamp: Date }>單個消息是具有以下屬性的對象:data:收到的消息內(nèi)容,和timestamp:收到消息的時間(日期)。對于具有一元和客戶端流方法的請求,pm.response.messages將僅在索引 0 處包含一條消息,可以作為pm.response.messages.idx(0).

pm.info

該pm.info對象提供與請求和腳本本身相關(guān)的元信息,包括請求名稱、請求 ID 和執(zhí)行掛鉤的名稱。

以下是對象的屬性pm.info:

  • 執(zhí)行鉤子的名稱。這將取決于preinvoke腳本response是否分別在Pre-invoke或Response掛鉤中執(zhí)行。pm.info.eventName: 'beforeInvoke' | 'afterResponse'
  • 標識運行腳本的當(dāng)前請求的唯一 ID:pm.info.requestId: string
  • 請求名稱:pm.info.requestName: string

寫斷言

您可以分別使用pm.testpm.expect函數(shù)將測試規(guī)范和斷言添加到您的腳本中。

pm.test

  • 使用該pm.test函數(shù)將測試規(guī)范添加到您的Pre-invoke或Response腳本。pm.test: (testName: string, specFunction: Function) => pm用于幫助您識別測試結(jié)果testName部分中的測試,并將測試的目的傳達給您的集合的使用者。specFunction是您使用函數(shù)定義請求和響應(yīng)數(shù)據(jù)斷言的地方pm.expect。該pm.test方法返回pm對象,使調(diào)用可鏈接。示例測試套件:pm.test("response should have 'content-type' metadata", function () { pm.response.to.have.metadata('content-type', 'application/grpc'); });可以將可選的done回調(diào)傳遞給pm.test, 以測試異步函數(shù):pm.test('async test', function (done) { setTimeout(() => { pm.expect(pm.response.statusCode).to.equal(0); done(); }, 1500); });您還可以包含多個斷言以將相關(guān)斷言分組到一個測試中:pm.test("Should receive update events for both users", function () { pm.response.messages.to.include({ action: 'update', userId: 'user1' }); pm.response.messages.to.include({ action: 'update', userId: 'user2' }); });
  • 獲取從代碼中特定位置執(zhí)行的測試總數(shù):pm.test.index: () => number
  • 使用以下命令跳過測試:pm.test.skip: (testName: string, specFunction: Function) => pm

pm.expect

該方法允許您使用ChaiJS expect BDDpm.expect語法在請求和響應(yīng)數(shù)據(jù)上編寫斷言。

pm.expect: (assertOn: any) => Assertion

您還可以使用pm.request.to.have.*,pm.response.to.have.*和pm.response.to.be.*來構(gòu)建您的斷言。

pm.response.to.have.statusCode(0);
pm.expect(pm.response.responseTime).to.be.below(200);

查看示例部分以獲取更多斷言。

在腳本中使用變量

前往此處的綜合指南,了解如何在腳本中使用變量。

從腳本發(fā)送 HTTP 請求

您可以使用該方法從Before invoke和After responsepm.sendRequest腳本異步發(fā)送 HTTP 請求。

pm.sendRequest: (request: string | RequestDefinition, callback?: (error: any, response: Response)) => void

您可以向該pm.sendRequest方法傳遞一個 URL 字符串,或一個完整的請求定義對象,包括標頭、方法、正文。

// Example with a plain string URL
pm.sendRequest('https://postman-echo.com/get', (error, response) => {
  if (error) {
    console.error(error);
  } else {
    console.log(response.json());
  }
});
// Example with a full-fledged request
const request = {
  url: 'https://postman-echo.com/post',
  method: 'POST',
  header: {
    'Content-Type': 'application/json',
    'X-Foo': 'bar'
  },
  body: {
    mode: 'raw',
    raw: JSON.stringify({ key: 'this is json' })
  }
};

pm.sendRequest(request, (error, response) => {
  console.log(error ? error : response.json());
});
// Example containing a test
pm.sendRequest('https://postman-echo.com/get', (error, response) => {
  if (error) {
    console.log(error);
  }

  pm.test('Response is OK', () => {
    pm.expect(error).to.equal(null);
    pm.expect(response).to.have.property('code', 200);
    pm.expect(response).to.have.property('status', 'OK');
  });
});

使用外部庫

要使用一個庫,require模塊通過傳遞它的名字,并將返回的導(dǎo)出模塊內(nèi)容分配給一個變量。

require(moduleName: string): any

該require方法允許您使用沙箱內(nèi)置庫模塊。下面列出了可用庫的列表以及相應(yīng)文檔的鏈接。

以下 NodeJS 模塊也可在沙箱中使用:


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號