如何使用supertest测试Express应用程序的POST请求参数?

时间:2023-01-23 13:49:26

I'm creating an API using Express 4.0.0 and one of the routes takes a POST. Currently I'm just trying to have it echo a name sent in the request parameters. The response is a JSON object, but the request expects form fields.

我正在使用Express 4.0.0创建一个API,其中一个路由需要POST。目前我只是想让它回应请求参数中发送的名称。响应是JSON对象,但请求需要表单字段。

users.post '/user', (req, res) ->
    res.json name: req.params.name

My test sets the type() to form which should allow send() to pass the hash as POST parameters/fields.

我的测试将type()设置为应该允许send()将散列作为POST参数/字段传递的形式。

describe 'POST /user', ->
    it 'should echo the name sent', (done) ->
        request app
        .post '/user'
        .type 'form'
        .send name: 'Foo'
        .expect '{"name":"Foo"}'
        .end done

Regardless, my test fails and in Express, my req.params is empty, req.param('name') comes up undefined and req.body is also empty.

无论如何,我的测试失败了,在Express中,我的req.params为空,req.param('name')出现未定义且req.body也为空。

Is there some req.fields attribute I'm not aware of, or is my test flawed somehow?

是否有一些我不知道的req.fields属性,或者我的测试是否存在缺陷?

1 个解决方案

#1


2  

tl;dr: You need to parse the body to get that, you can do app.use(require('body-parser')()) and then change your code with name: req.param('name').

tl; dr:你需要解析身体才能得到它,你可以做app.use(require('body-parser')())然后用名字改变你的代码:req.param('name')。

So express(and actually node.js http server) will call the request call whenever a http request header have been received and not the body of the request. You have to explicitly read and wait for the body to come and parse it yourself.

因此,只要收到http请求头而不是请求正文,express(实际上是node.js http服务器)就会调用请求调用。你必须明确地阅读并等待身体来自己解析它。

Luckily there are express middleware modules that parses the body for you. In this case you are sending a urlencoded/form body, so you need to parse it as such. Use any of these modules according to their examples:

幸运的是,有快速的中间件模块可以为您解析正文。在这种情况下,您将发送urlencoded / form正文,因此您需要解析它。根据他们的示例使用任何这些模块:

Assuming you use body-parser, then if you want it to parse the body for all routes then just do app.use(require('body-parser')(). If you want it to parse the body for a particular route then do this:

假设你使用了body-parser,那么如果你想让它解析所有路由的主体,那么只需要做app.use(require('body-parser')()。如果你想让它解析特定路径的主体那么做这个:

bodyParser = require('body-parser')()
app.post '/user', bodyParser, (req, res) ->
    res.json name: req.param('name')

Once you got the body parsing right, then you can access it either through req.body(for example req.body.name) property or req.param function(for example req.param('name')). The latter will also search through query string and url parameters.

一旦你正确解析了主体,你就可以通过req.body(例如req.body.name)属性或req.param函数(例如req.param('name'))来访问它。后者还将搜索查询字符串和url参数。

Note that if you want to parse a body with attached files(for uploading files) you need a multipart parser and not just a urlencoded one:

请注意,如果要解析带有附加文件的主体(用于上载文件),则需要一个多部分解析器,而不仅仅是一个urlencoded的解析器:

#1


2  

tl;dr: You need to parse the body to get that, you can do app.use(require('body-parser')()) and then change your code with name: req.param('name').

tl; dr:你需要解析身体才能得到它,你可以做app.use(require('body-parser')())然后用名字改变你的代码:req.param('name')。

So express(and actually node.js http server) will call the request call whenever a http request header have been received and not the body of the request. You have to explicitly read and wait for the body to come and parse it yourself.

因此,只要收到http请求头而不是请求正文,express(实际上是node.js http服务器)就会调用请求调用。你必须明确地阅读并等待身体来自己解析它。

Luckily there are express middleware modules that parses the body for you. In this case you are sending a urlencoded/form body, so you need to parse it as such. Use any of these modules according to their examples:

幸运的是,有快速的中间件模块可以为您解析正文。在这种情况下,您将发送urlencoded / form正文,因此您需要解析它。根据他们的示例使用任何这些模块:

Assuming you use body-parser, then if you want it to parse the body for all routes then just do app.use(require('body-parser')(). If you want it to parse the body for a particular route then do this:

假设你使用了body-parser,那么如果你想让它解析所有路由的主体,那么只需要做app.use(require('body-parser')()。如果你想让它解析特定路径的主体那么做这个:

bodyParser = require('body-parser')()
app.post '/user', bodyParser, (req, res) ->
    res.json name: req.param('name')

Once you got the body parsing right, then you can access it either through req.body(for example req.body.name) property or req.param function(for example req.param('name')). The latter will also search through query string and url parameters.

一旦你正确解析了主体,你就可以通过req.body(例如req.body.name)属性或req.param函数(例如req.param('name'))来访问它。后者还将搜索查询字符串和url参数。

Note that if you want to parse a body with attached files(for uploading files) you need a multipart parser and not just a urlencoded one:

请注意,如果要解析带有附加文件的主体(用于上载文件),则需要一个多部分解析器,而不仅仅是一个urlencoded的解析器: