招摇测试中没有请求主体

No request body in swagger-test

经过长时间的搜索,我终于发现 swagger-test 是从 swagger 规范测试我的 REST 服务的最佳框架。我遵循了 Repo 中的自述文件和示例。这是我的第一对 xamples request/response。

"x-amples": [{
"description": "should save an object",
"request": {
    "params": {
        "app_id": "bengi",
        "table_name": "Student",
        "body": {
            "collectionName": "Student",
            "key": "mLiJB380x9893rjlaf0"
        }
    }
},
"response": {
    "status": 200,
    "headers": {
        "content-type": "application/json"
    }
}}]

app_idtable_nameurl中被很好地替换了。
我的问题是参数 body 从未包含在 http 请求中。我知道这一点是因为我检查了来自 wireshark 的流量。
我很确定我的规格非常好,因为我已经 运行 从 Swagger-UI 手动成功测试。这是我的控制台的屏幕截图:


这意味着我的请求未被授权,因为缺少上面请求 body 中的 key 参数。
这是我的 js.js 文件 mocha:

var swaggerTest = require('swagger-test');
var fs = require('fs');
var preq = require('preq');
var swaggerSpec;

var buffer  = fs.readFileSync('cb.json');
    swaggerSpec    = JSON.parse(buffer);
var xamples = swaggerTest.parse(swaggerSpec);


describe('specification-driven tests', function () {
  xamples.forEach(function (xample) {
    it(xample.description, function() {
        this.timeout(10000);
      return preq[xample.request.method](xample.request)
      .then(function (response) {
        assert.deepEqual(response, xample.response);
      });
    });
  });
});


我应该怎么做才能确保我的请求 bodyswagger-test 看到和使用?

在网上爬了几个小时却没有找到解决方案后,我尝试查看代码和使用的模块。这是我发现解决我的问题的方法:
我的 x-amples JSON 格式错误,尤其是我放置 body 元素的地方。
来自我的问题

app_id and table_name are substituted very well in the url.

那是因为这条线:

 var xamples = swaggerTest.parse(swaggerSpec);


来自 test file.


swagger-test的主要功能是解析Swagger规范文件并将所有x-ample元素提取到数组中,如下行所示:

var xamples = swaggerTest.parse(swaggerSpec);

这意味着 swagger-test 完美地完成了它的工作,即:

  1. 将所有 x-ample 个扩展检索到数组中。
  2. 替换 params 元素中的值:

    "request": {
        "params": {
            "app_id": "bengi",
            "table_name": "Student",
            "body": {
                "collectionName": "Student",
                "key": "mLiJB380x9893rjlaf0"
            }
        }
    }
    

    进入url模板:

e>/data/{app_id}/{table_name}

  1. swagger-specrequestJSON中的hostbasePathparams连接起来完成 uri
    所有这一切都在这个代码片段中完成,驴子在

    中工作
    function parseXample(spec, uri, method, xample) {
    var uriTemplate = template.parse(uri);
    var expandedUri = uriTemplate.expand(xample.request.params);
    xample.request.method = method;
    xample.request.uri = spec.host + spec.basePath + expandedUri;
    return {
        description: xample.description || method + ' ' + uri,
        request: xample.request,
        response: xample.response
    };
    

    }


来自我的问题:

My problem is that the parameter body is never included in the http request

诀窍在于 preq 模块源代码中的 getOptions([url],[o],[method]) 方法。

    if (o.body && o.body instanceof Object) {
        if (o.headers && /^application\/json/.test(o.headers['content-     type'])) {
            o.body = JSON.stringify(o.body);
        } else if (o.method === 'post') {
            o.form = o.body;
            o.body = undefined;
        }
    }


参数 ooptions 即从我的测试代码传递给 preq 的 xample.request 对象: return preqxample.request.method 因此很明显,xample.request.body 存在于我的请求对象中,因为它位于 xample.request.params.body,因此条件 if (o.body && o.body instanceof Object) 没有通过,因此分配 o.body=JSON.stringify(o.body) 没有发生.
最终的 x-ample 扩展应该如下所示:

"x-amples": [{
    "description": "should save an object",
    "request": {
        "method": "put",
        "uri": "/data/{app_id}/{table_name}",
        "headers": {
            "content-type": "application/json"
        },
        "body": {
            "collectionName": "Student",
            "key": "xxxxxx"
        },
        "params": {
            "app_id": "xxxxxx",
            "table_name": "xxxxxx"
        }
    },
    "response": {
        "status": 200,
        "headers": {
            "content-type": "application/json"
        }
    }
}]


除了 body 元素的位置外,还必须包括 headers 元素,否则会出错。