1

さまざまな状況を処理するリクエスト ハンドラーをテストしていますが、そのうちの 1 つは HTTP コード 204 で応答本文を持たない応答を期待しています ( typeof response.body === 'undefined')。

返信を に設定しようとしましたがnullundefinedまったく設定しませんでした。これらすべての場合において、応答本文は空の文字列 ( '') です。

Nock から応答本文を削除する方法はありますか?

これは私が実行しようとしているテストです:

it('should return true if body is undefined (usually, 204 status responses - No content)', async () => {
  nock('http://test.com')
    .put('/')
    .reply(204, undefined)
//  .reply(204, null) also fails
//  .reply(204) also fails

 const result = await Request._request('http://test.com', { method: 'PUT' })

 expect(result).to.be.true
})

そして、これは私がテストしようとしているコードです:

static async _request (url, options) {
    return new Promise((resolve, reject) => {
      request(url, options)
        .then((response) => {
          if (response.statusCode < 300) {
            if (typeof response.body === 'undefined') {
              resolve(true)
            } else if (isJSON(response.body)) {
              resolve(JSON.parse(response.body.replace(/\s/g, ' ')))
            } else {
              .
              .
              .
4

0 に答える 0