0

使用协议验证消费者和提供者的响应标头是否匹配。在提供者端运行协议验证会给我以下错误:

Failure/Error: expect(header_value).to match_header(name, expected_header_value)
Expected header "abc" to equal "xyz", but was nil

但是,当我检查我的响应标头时,它给了我预期的值(“xyz”)。

这是我要验证的示例协议文件:

"interactions": [
{
  "description": "a request to do something",
  "request": {
    "method": "get",
    "path": "/example"
  },
  "response": {
    "status": 200,
    "headers": {
      "abc": "xyz"
    }
  }
}]

我是新来的契约。任何帮助,将不胜感激。

4

1 回答 1

1

虽然这是一个旧帖子,但我希望这对任何查看此内容的人有所帮助。我不熟悉 ruby​​,但是如果您使用基本的 HTTP Rest 请求,则需要在“withRequest”上添加接受标头以及在“withRespondWith”上添加预期标头。您可以使用 Postman 查看请求和响应标头;JavaScript 示例:

describe('When a request is made to get all <resources>', () => {
    beforeAll(() =>
      provider.setup().then(() => {
        provider.addInteraction({
          uponReceiving: 'a request to receive to receive all...',
          withRequest: {
            method: 'GET',
            path: '/<resource>',
            // Default headers from Axios documentation
            headers: { Accept: "application/json, text/plain, */*" }
          },
...

willRespondWith: {
            // expected headers
            headers: { "Content-Type": "application/json; charset=utf-8" },
...
于 2021-02-04T20:33:32.603 回答