在使用Chai-HTTP进行开机时,似乎有错误的内容types

我期待利用Chai-HTTP进行一些testing。 当然,我想比我的GET更多的testing,但是当我尝试做POST时,我似乎遇到了一个重大的障碍。

在试图找出为什么我的POST不起作用,我开始打他们对POSTtesting服务器。

这是一个POST尝试,使用完全不同的工具链(Jasmine-Node和Frisby)进行testing(工作得很好):

frisby.create('LOGIN') .post('http://posttestserver.com/post.php', { grant_type:'password', username:'helllo@world.com', password:'password' }) .addHeader("Token", "text/plain") .expectStatus(200) }) .toss(); 

其结果是:

 Time: Mon, 27 Jun 16 13:40:54 -0700 Source ip: 204.191.154.66 Headers (Some may be inserted by server) REQUEST_URI = /post.php QUERY_STRING = REQUEST_METHOD = POST GATEWAY_INTERFACE = CGI/1.1 REMOTE_PORT = 19216 REMOTE_ADDR = 204.191.154.66 HTTP_CONNECTION = close CONTENT_LENGTH = 64 HTTP_HOST = posttestserver.com HTTP_TOKEN = text/plain CONTENT_TYPE = application/x-www-form-urlencoded UNIQUE_ID = V3GPVkBaMGUAAB1Uf04AAAAc REQUEST_TIME_FLOAT = 1467060054.9575 REQUEST_TIME = 1467060054 Post Params: key: 'grant_type' value: 'password' key: 'username' value: 'hello@world.com' key: 'password' value: 'password' Empty post body. Upload contains PUT data: grant_type=password&username=hello%40world.com&password=password 

这里是使用Chai和Chai-HTTP的POST尝试。 我期望这与上面的例子使用Jasmine和Frisby一样,但是,你会看到实际的请求在几个方面有所不同。

 describe('/post.php', function() { var endPointUnderTest = '/post.php'; it('should return an auth token', function(done) { chai.request('http://posttestserver.com') .post(endPointUnderTest) .set('Token', 'text/plain') .send({ grant_type: 'password', username: 'hello@world.com', password: 'password' }) .end(function(err, res) { console.log(res); res.should.have.status(200); done(); }); }); }); 

其结果是:

 Time: Tue, 28 Jun 16 06:55:50 -0700 Source ip: 204.191.154.66 Headers (Some may be inserted by server) REQUEST_URI = /post.php QUERY_STRING = REQUEST_METHOD = POST GATEWAY_INTERFACE = CGI/1.1 REMOTE_PORT = 1409 REMOTE_ADDR = 204.191.154.66 HTTP_CONNECTION = close CONTENT_LENGTH = 76 CONTENT_TYPE = application/json HTTP_TOKEN = text/plain HTTP_USER_AGENT = node-superagent/2.0.0 HTTP_ACCEPT_ENCODING = gzip, deflate HTTP_HOST = posttestserver.com UNIQUE_ID = V3KB5kBaMGUAAErPF6IAAAAF REQUEST_TIME_FLOAT = 1467122150.9125 REQUEST_TIME = 1467122150 No Post Params. == Begin post body == {"grant_type":"password","username":"hello@world.com","password":"password"} == End post body == Upload contains PUT data: {"grant_type":"password","username":"hello@world.com","password":"password"} 

注意CONTENT_TYPE,Post Params和PUT数据的差别(我认为这是我的问题的根源)。

在Jasmine / Frisby使用'application / x-www-form-urlencoded'格式提交POST时,Chai-HTTP似乎使用'application / json'格式。

我不知道怎么滥用Chai-HTTP的POSTfunction? 或者,Chai-HTTP不允许“application / x-www-form-urlencoded”POST请求? 我似乎无法解决这个问题,这是我跳转到使用摩卡/柴工具链进行testing的最后一道障碍(这是目标,我宁愿不使用不同的库,除非这是绝对必要的)。

在Chai-HTTP的Git-Hub页面上进一步讨论了这一点后,我发现这是Chai-HTTP引擎下的HTTP请求库SuperAgent的预期行为,该请求库基于什么内容自动检测内容types.send()调用中包含一些数据。

我偶然发现了这个特定的问题 ,这有助于澄清内容types之间的差别。

如果有其他人遇到这个问题,我已经知道Chai-HTTP的POST请求可以很容易地改变( 这里是meeber的帮助),使用这样的调用:

 //Override auto-detection by specifying the header explicitly .set('content-type', 'application/x-www-form-urlencoded') //Select the type 'form' .type('form') //Pass multiple strings in send instead of using an object .send('grant_type=password') .send('username=hello@world.com') .send('password=password') 

创build一个如下所示的请求:

 describe('/post.php', function() { var endPointUnderTest = '/post.php'; it('should return an auth token', function(done) { chai.request('http://posttestserver.com') .post(endPointUnderTest) .set('Token', 'text/plain') .set('content-type', 'application/x-www-form-urlencoded') .type('form') .send('grant_type=password') .send('username=hello@world.com') .send('password=password') .end(function(err, res) { console.log(res); res.should.have.status(200); done(); }); }); });