我正在用玩笑来测试我的nodejs rest API。
The problem I have currently is that when I try testing my post routes, I always receive a status code of 302 due to res.redirect("/")
.
我的POST路线示例:
app.post("/login", async (req, res) => {
try {
let username = 'example'
...
return res.redirect("/");
} catch (error) {
return res.redirect("/");
}
});
开玩笑的测试文件:
'use strict';
const request = require('supertest');
const app = require('./index');
...
describe('Test', () => {
test('POST /login', () => {
return request(app)
.post('/login')
.set('username','example')
.expect(?)
});
});
如何测试页面已成功重定向?
另外,我还可以在登录POST路由中测试其他任何内容,即如何测试无效的用户名?
As per the Express docs, you can specify a response code as such:
docs状态为“如果未指定,则状态默认为“ 302”已找到”。
Edit: HTTP codes 301 and 302 indicate successful redirection; 301 is permanent and 302 is temporary. Both are "successful" as far as a computer is concerned.