验证从Node后端的frisby测试发布的数据

时间:2016-05-20 16:19:25

标签: node.js unit-testing testing request

我想通过我的Frisby测试检查数据是否实际成功发布到我的后端。我可以在控制台中看到URL调用成功,响应代码为201.但是,当我打印req.body时,它是空的。

Jasmine / Frisby Tests

describe("POST /createSecret", function() {

  it('returns a status code of 201', function(done) {

    //Send a POST req. to create secret data
    frisby.create('GET JSON data from an endpoint')
      .post('http://localhost:3000/createSecret', {

            'secret_name'       : barbicanRequestData.secret_name,
            'expiration'        : barbicanRequestData.expiration,
            'algorithm'         : barbicanRequestData.algorithm,
            'bit_length'        : barbicanRequestData.bit_length,
            'mode'              : barbicanRequestData.mode,
            'payload'           : barbicanRequestData.payload,
            'content_type'      : barbicanRequestData.content_type,
            'content_encoding'  : barbicanRequestData.content_encoding,
            'secret_type'       : barbicanRequestData.secret_type
      })
      .expectStatus(201)
      .expectHeader('Content-Type', 'application/json; charset=utf-8')
      .expectJSON({ 'secret_ref': 'Secret was successfully created!' })
    .toss();
    done();
  });
});

后端

  else if (req.method == "POST") {

    console.log("Request is POST");

    console.log(res.body);

    //Test a POST response for creating a secret using the Barbican API
    res.status(201).send({
      'secret_ref': "Secret was successfully created!",
      'Content-Type': 'application/json; charset=utf-8'
    });
  }

1 个答案:

答案 0 :(得分:0)

var bodyParser    = require('body-parser');

app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());

这使我能够成功访问来自body的数据!