Node Express JEST Supertest PUT和DELETE端点返回405“不允许使用方法”

问题描述 投票:0回答:1

我正在使用Express和suppertest在Node上运行一个简单的测试。有10个测试的POST和GET测试运行良好(前6个测试)。具有PUT和DELETE的其他四个返回405“不允许的方法”。这是我的代码:

test('Should login existing user', async () => {
  await request(app)
        .post('/api/auth')
        .send({ 
          email: userOne.email,
          password: userOne.password
        })
        .expect(200);
});


test('Should NOT login nonexisting user', async () => {
  await request(app)
        .post('/api/auth')
        .send({ 
          email: userThree.email,
          password: userThree.password
        })
        .expect(400);
});


test('Should get profile for authenticated user', async () => {
  await request(app)
        .get('/api/auth')
        .set('x-auth-token', userOne.token)
        .expect(200)
});


test('Should NOT get profile for unauthenticated user', async () => {
  await request(app)
        .get('/api/auth')
        .expect(401)
});


test('Should create a new user', async () => {
  await request(app)
        .post('/api/person')
        .set('x-auth-token', userOne.token)
        .send({ ...userTwo })
        .expect(201);
});


test('Should NOT create a new user', async () => {
  await request(app)
        .post('/api/person')
        .send({ ...userTwo })
        .expect(401);
});


test('Should update user for autheticated user', async () => {
  await request(app)
        .put('api/person/2')
        .set('x-auth-token', userOne.token)
        .send({ role: 1})
        .expect(200)
});


test('Should NOT update user for unauthenticated user', async () => {
  await request(app)
        .put('api/person/2')
        .send({ role: 1})
        .expect(401)
});


test('Should delete user for autheticated user', async () => {
  await request(app)
        .delete('api/person/2')
        .set('x-auth-token', userOne.token)
        .send()
        .expect(200)
});


test('Should NOT delete user for unauthenticated user', async () => {
  await request(app)
        .delete('api/person/2')
        .expect(401)
});

正如我上面说过的,无论是否通过身份验证,前六名都很棒。带有PUT和DELETE请求的底部4返回405“不允许使用方法”。与邮递员测试相同的路线我没有遇到任何问题。 DELETE和POST方法都按预期方式工作。有人知道我在做什么错吗?我在俯视什么?谢谢您的所有帮助。

express jestjs supertest
1个回答
0
投票

DELETE和PUT方法没有问题。原因是一个简单的错字(在“ api / person / 2”调用前缺少/)。不幸的是,这仅发生在DELETE和PUT方法中,使得调试花费了更长的时间来思考问题出在方法上。该错误要简单得多。路径不正确。

© www.soinside.com 2019 - 2024. All rights reserved.