简体   繁体   English

使用Mocha Test在javascript中进行身份验证的http发布

[英]http post for auth in javascript using mocha test

I received a test suite written in js using mocha. 我收到了使用Mocha用JS编写的测试套件。 This is the test method i am attempting to get to work. 这是我尝试上班的测试方法。 They are sending the data as form encoded. 他们以表单编码形式发送数据。 My controller accepts data in the form body(method below) My model contains the same properties. 我的控制器接受主体形式的数据(下面的方法)我的模型包含相同的属性。 How do i rewrite the js method to match my controller? 如何重写js方法以匹配我的控制器?

  function loginUser (auth) {
      return function (done) {
        request('https://mysites/api/').post('login')
          .set('Content-Type', 'application/x-www-form-urlencoded')
          .set('emulateJSON', true)
          .timeout({response: 30000, deadline: 30000})
          .send({grant_type: 'password', Username: 'TheUserName', Password: 'Password'})
          .end(function (err, response) {
            assert.equal(response.status, 200)
            console.log('Response: ', response.body)
            auth.token = response.body.access_token
            done()
          })
      };
    }



[AllowAnonymous]
[HttpPost]
public IActionResult Post([FromBody]LoginDto loginViewModel)
{
    if (ModelState.IsValid)
    {
        //This method returns user id from username and password.
        var attemptedUser = GetUserInfoFromLoginViewModel(loginViewModel);
        if (attemptedUser.Username == null)
        {
            return BadRequest("invalid_grant, The user name or password is incorrect.");
        }

In case anyone is wondering the answer is to add 如果有人想知道答案是添加

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

to the top of your tests.specs.js file. 到tests.specs.js文件的顶部。 It is a self signed certificate issue. 这是一个自签名证书问题。 I found the answer posted in another question 我在另一个问题中找到了答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM