body如何使用nock拦截请求

How to use nock to intercept requests regardless of body

我正在尝试在我的测试中使用 nock 来拦截我从 Node.js 中的本机 https 模块发出的请求调用。 I'm using Promise.all 向外部服务器发出两个请求。我希望我的测试能够拦截调用,并检查一些表单字段以确保它们已按照我的意愿填写。

我的 class 设置如下(保留了最相关的代码部分):

const archiver = require('archiver');
const { generateKeyPairSync } = require('crypto');
const FormData = require('form-data');
const fs = require('fs');
const https = require('https');

class Platform {
  constructor() {
    this.FILESTORE_USERNAME = process.env.FILESTORE_USERNAME;
    this.FILESTORE_PASSWORD = process.env.FILESTORE_PASSWORD;
  }

  store(serviceName) {
    const { publicKey, privateKey } = this._generateKeys();

    return Promise.all([this._postKey(publicKey), this._postKey(privateKey)])
      .then(() => {
        return this._zipKeys(publicKey, privateKey, serviceName);
      })
      .catch((err) => {
        throw err;
      });
  }

  _postKey(key) {
    const options = this._getOptions();
    const keyName = (key.search(/(PUBLIC)/) !== -1) ? 'publicKey' : 'privateKey';
    const form = new FormData();
    form.append('file', key);
    form.append('Name', keyName);
    form.append('MimeMajor', 'application');
    form.append('MimeMinor', 'x-pem-file');
    form.append('Extension', (keyName == 'publicKey') ? 'pub' : '');
    form.append('FileClass', 'MFS::File');
    options.headers = form.getHeaders();
    options.headers.Authorization = 'Basic ' + Buffer.from(this.FILESTORE_USERNAME + ':' + this.FILESTORE_PASSWORD).toString('base64');

    return new Promise((resolve, reject) => {
      let post = https.request(options, (res) => {
        let data = '';
        if (res.statusCode < 200 || res.statusCode > 299) {
          reject(new Error('File Storage API returned a status code outside of acceptable range: ' + res.statusCode));
        } else {
          res.setEncoding('utf8');
          res.on('data', (chunk) => {
            data += chunk;
          });

          res.on('error', (err) => {
            reject(err);
          });

          res.on('end', () => {
            if (data) {
              resolve(JSON.parse(data));
            } else {
              resolve();
            }
          });
        }
      });
      post.on('error', (err) => {
        reject(err);
      });
      form.pipe(post);
      post.end();

    });
  }

  _getOptions() {
    return {
      hostname: 'api.example.com',
      path: '/media/files/',
      method: 'POST',
    };
  }
}

module.exports = Platform;

然后,我的测试代码如下所示。我正在使用 mocha、sinon、chai、sinon-chai 和 nock。

const Platform = require('/usr/src/app/api/Services/Platform');
const crypto = require('crypto');
const fs = require('fs');
const nock = require('nock');
const yauzl = require('yauzl');

describe('Platform', function() {
  let platform;
  beforeEach(() => {
    platform = new Platform();
  });
  afterEach(() => {
    const list = fs.readdirSync('/usr/src/app/api/Services/data/');
    list.forEach((file) => {
      fs.unlink('/usr/src/app/api/Services/data/' + file, (err) => {
        if (err) throw err;
      });
    });
    nock.cleanAll();
  });
  after(() => {
    nock.restore();
  });
  describe('store', function() {
    it('should post each generated key to an external storage place', async function() {
      this.timeout(5000);
      // const stub = sinon.stub(platform, '_postKey').resolves();
      const scope = nock('https://api.example.com')
        .persist()
        .post('/media/files/', (body) => {
          // console.log(body);
        })
        .reply(200);

      let serviceName = 'test';
      let actual = await platform.store(serviceName)
        .catch((err) => {
          (() => { throw err; }).should.not.throw();
        });

      console.log(scope);
      // expect(stub.callCount).to.equal(2);
      expect(actual).to.be.a('string');
      expect(actual).to.include(serviceName + '.zip');
      // stub.reset();

    });
  });
});

我遇到的问题是 运行 我的测试时抛出的错误:

AssertionError: expected [Function] to not throw an error but 'Error: Nock: No match for request {\n "method": "POST",\n "url": "https://api.example.com/media/files/",\n "headers": {\n
"content-type": "multipart/form-data; boundary=--------------------------363749230271182821931703",\n
"authorization": "Basic abcdef1224u38454857483hfjdhjgtuyrwyt="\n },\n "body": "----------------------------363749230271182821931703\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAq+QnVOYVjbrHIlAEsEoF\nZ4sTvqiB3sJGwecNhmgrUp9U8oqgoB50aW6VMsL71ATRyq9b3vMQKpjbU3R2RcOF\na6mlaBtBjxDGu2nEpGX++mtPCdD9HV7idvWgJ3XS0vGaCM//8ukY+VLBc1IB8CHC\nVj+8YOD5Y9TbdpwXR+0zCaiHwwd8MHIo1kBmQulIL7Avtjh55OmQZZtjO525lbqa\nWUZ24quDp38he2GjLDeTzHm9z1RjYJG6hS+Ui0s2xRUs6VAr7KFtiJmmjxPS9/vZ\nwQyFcz/R7AJKoEH8p7NE7nn/onbybJy+SWRxjXVH8afHkVoC65BiNoMiEzk1rIsx\ns92woHnq227JzYwFYcLD0W+TYjtGCB8+ks+QRIiV0pFJ3ja5VFIxjn9MxLntWcf2\nhsiYrmfJlqmpW1DMfZrtt41cJUFQwt7CpN72aix7btmd/q0syh6VVlQEHq/0nDky\nItv7dqyqZc9NNOMqK9/kXWhbq5cwS21mm+kTGas5KSdeIR0LH7uVtivB+LKum14e\nRDGascZcXZIVTbOeCxA6BD7LyaJPzXmlMy4spXlhGoDYyVRhpvv2K03Mg7ybiB4X\nEL1oJtiCFkRX5LtRJv0PCRJjaa3UvfyIuz8bHK4ANxIZqcwZwER+g02gw8iqNfMa\nDWXpfMJUU8TQuLGWZQaGJc8CAwEAAQ==\n-----END PUBLIC KEY-----\n\r\n----------------------------363749230271182821931703\r\nContent-Disposition: form-data; name=\"Name\"\r\n\r\npublicKey\r\n----------------------------363749230271182821931703\r\nContent-Disposition: form-data; name=\"MimeMajor\"\r\n\r\napplication\r\n----------------------------363749230271182821931703\r\nContent-Disposition: form-data; name=\"MimeMinor\"\r\n\r\nx-pem-file\r\n----------------------------363749230271182821931703\r\nContent-Disposition: form-data; name=\"Extension\"\r\n\r\npub\r\n----------------------------363749230271182821931703\r\nContent-Disposition: form-data; name=\"FileClass\"\r\n\r\nMFS::File\r\n----------------------------363749230271182821931703--\r\n"\n}' was thrown

我认为这是因为 nock 希望我伪造 body 以获得正确匹配的请求?有没有一种方法可以只查找对该地址发出的请求,而不考虑 body,这样我就可以进行自己的测试或其他任何操作。

当向 Nock Scope 的 post 方法传递第二个参数时,它用于匹配请求的主体。

Docs for specifying the request body

在您的测试中,您传递了一个函数作为第二个参数,但没有 returning true,所以 Nock 认为它不匹配。

来自文档:

Function: nock will evaluate the function providing the request body object as first argument. Return true if it should be considered a match

由于您的目标是根据请求断言表单字段,因此最好的方法是将函数留在那里,在 // console.log(body); 行所在的位置进行断言,但将 return true; 添加到函数结束。

您也可以 return truefalse 取决于您的表单字段是否与您的断言相匹配,但根据我的经验,这会使测试的错误输出变得复杂。我的偏好是使用标准 chai expect() 调用并在 Nock 继续请求匹配之前让断言出现错误。