gpt4 book ai didi

node.js - swagger-test 中没有请求正文

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:30 25 4
gpt4 key购买 nike

经过一番查找,终于找到了swagger-test成为根据 swagger 规范测试 REST 服务的最佳框架。我遵循了 read me 和 Repo 中的示例。这是我的第一个 xamples 请求/响应对。

"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 手动运行成功的测试。这是我的控制台的屏幕截图: when I run the tests


这意味着我的请求由于缺少上面请求 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 的地方元素。
来 self 的问题

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}

  • 连接 host , basePathparams来自swagger-specrequest JSON组成完整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那就是xample.request从我的测试代码传递给 preq 的对象: 返回 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元素,否则会出现错误。

    关于node.js - swagger-test 中没有请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35437507/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com