A. *_*nan 1 javascript rest json node.js chai
我希望利用Chai-HTTP进行一些测试.当然,我想测试的不仅仅是我的GET,但是在尝试发布POST时,我似乎遇到了一个主要障碍.
为了弄清楚为什么我的POST不起作用,我开始在POST测试服务器上点击它们.
这是一个使用完全不同的工具链(Jasmine-Node和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();
Run Code Online (Sandbox Code Playgroud)
结果如下:
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
Run Code Online (Sandbox Code Playgroud)
这是使用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();
});
});
});
Run Code Online (Sandbox Code Playgroud)
结果如下:
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"}
Run Code Online (Sandbox Code Playgroud)
请注意CONTENT_TYPE,Post Params和PUT数据的区别(我认为这是我的问题的根源).
Jasmine/Frisby将使用'application/x-www-form-urlencoded'格式提交POST,Chai-HTTP似乎使用'application/json'格式.
我是否在某种程度上滥用了Chai-HTTP的POST功能?或者Chai-HTTP不允许'application/x-www-form-urlencoded'POST请求?我似乎无法解决这个问题,这是我跳转到使用Mocha/Chai工具链进行测试的最后一个障碍(这是目标,我宁愿不使用不同的库,除非这是绝对必要的).
在Chai-HTTP的Git-Hub页面上进一步讨论后,我发现这是SuperAgent的预期行为,这是Chai-HTTP引擎盖下的HTTP请求库,它基于什么自动检测内容类型.send()调用中包含一些数据.
我偶然发现了这个特定的问题,这有助于澄清内容类型之间的差异.
如果有其他人遇到这个问题,我已经知道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')
Run Code Online (Sandbox Code Playgroud)
创建如下所示的请求:
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();
});
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1647 次 |
| 最近记录: |