【问题标题】:Nock - Get matched hostnameNock - 获取匹配的主机名
【发布时间】:2020-09-22 09:29:59
【问题描述】:

我想模拟一个内部主机命名方案,像这样。

nock(/some-internal.(ds1|ds2|ds3).hostname/)
  .get("/info")
  .reply(200, (???, requestBody) => {
    if(??? === "d1") {
      // return mock for d1
    } else if (??? === "d2") {
      // return mock for d2
    }
    // ... 
}) 

回调的第一个参数是没有base url的路径,这可能吗?

【问题讨论】:

    标签: nock


    【解决方案1】:

    您可以使用上下文从回调内部访问ClientRequest 实例。 Docs for accessing the original request and headers.

    const scope = nock(/some-internal.(ds1|ds2|ds3).hostname/)
      .get('/info')
      .reply(function (uri, requestBody) {
        console.log('host:', this.req.options.host)
        // ...
      })
    

    【讨论】:

    • 感谢您的回复。 this.req.options.host 对我有用
    猜你喜欢
    • 2013-07-30
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 2021-02-03
    • 2016-12-27
    • 2018-06-18
    相关资源
    最近更新 更多