我希望利用柴HTTP进行一些测试。当然,我想测试的比我得到的更多,然而,当我试图发布帖子时,我似乎遇到了一个主要的障碍。
为了找出我的帖子不起作用的原因,我开始攻击一个测试服务器。
下面是使用完全不同的工具链(Jasmine和Frisby)格式化的POST尝试,用于测试(工作得很好):
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下面是使用柴和柴-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、Params和PUT数据的不同(我认为这是我问题的根源)。
Jasmine/Frisby将使用‘application/x form-urlencoded’格式提交帖子,而柴HTTP似乎使用的是'application/json‘格式。
我是不是滥用了柴HTTP的POST功能?还是说柴HTTP不允许'application/x-www-form-urlencoded‘POST请求?我似乎无法解决这个问题,这是我跳转到使用Mocha/柴工具链进行测试的最后一个障碍(这是我的目标,除非是绝对必要的,否则我宁愿不使用不同的库)。
发布于 2016-06-28 14:49:53
在柴HTTP的Git-Hub页面上对此进行了进一步的讨论后,我发现这是SuperAgent的预期行为,这是柴HTTP引擎下的HTTP请求库,它根据.send()调用中包含的数据类型自动检测内容类型。
我也无意中发现了这个特殊的问题,这有助于澄清内容类型之间的实际区别。
如果有其他人遇到这个问题,我已经了解到可以很容易地更改柴HTTP的POST请求(对meeber's help 这里的评价),可以使用以下调用:
//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')创建如下所示的请求:
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();
});
});
});https://stackoverflow.com/questions/38078569
复制相似问题