如何使用身份验证

时间:2017-10-23 10:12:07

标签: node.js mocha chai

我正在使用mocha和chai测试我的节点js应用程序的一些get和post apis。到目前为止,我一直在我的localserver中运行它,每个post和get api的示例代码如下:

process.env.NODE_ENV = 'test';
var mongoose = require("mongoose");
var db_model = require('../models/myproject.model');

var chai = require('chai');
var chaiHttp = require('chai-http');
var server = require('../app');
var should = chai.should();
var expect = chai.expect;

chai.use(chaiHttp);


describe('First POST', ()=> {
    it('This is the first post', (done) => {
        chai.request(server)
    //chai.request('http://localhost:8000') //this also works
        .post('/data/myproject')
        .send(db_model)
        .end((err, res) => {
            //expect(true).to.be.true;
            expect(res.statusCode).to.equal(200);

        done();
        });
    });


});


describe('First GET', () => {
    it('This is the first get', (done)=> {
        chai.request(server)
        .get('/data/myproject')
        .end((err, res) => {
            //expect(true).to.be.true;
            expect(res.statusCode).to.equal(200);


        done();
        });

    });

}); 

现在,我需要从实际服务器运行上述代码,即http://myproject.webportal.com

我的POST请求将通过以下参数传递:
1. id =“any_numeric_id”
2. name =“some_name”
3.需要验证(用户名,密码) 4. content = {“k1”:“v1”,“k2”,“v2”} //基本上是json数据

对我的GET请求只有上面列表中的#1,#2和#3。 基本上,当我在浏览器中手动触发此URL时,它看起来像:
http://myproject.webportal.com/data/myproject?id=405&name=smith

我如何更改我的代码以满足此要求,我已经浏览了几个关于此的链接,但却感到困惑,无法获得结果。

有人可以帮我解释这个编码,让我对如何正确地做到这一点有一个公平的想法吗?

感谢。

0 个答案:

没有答案