I created this piece of middleware that will return next() when the only parameters for a route are defined by queryItems.
I recently found node-mocks-http which fakes expresses req and res objects. However it does not fake next. I was wondering how that should be done? Below I have an example of me opening up the next callback and defining my expect statement within it.
middleware.hasOnlyQuery = function(queryItems){
return function(req, res, next){
if(typeof queryItems == "string") queryItems = [queryItems]
if(_.hasOnly(req.query, queryItems)) return next()
return next("route")
}
}
Here's the test.
it("should only have shop query", function(done){
var req = httpMocks.createRequest({
method: 'GET',
query: {
foo: "bar"
}
});
var res = httpMocks.createResponse()
var fn = middleware.hasOnlyQuery(["foo"])(req, res, function(err){
expect(err).to.equal()
return done()
})
})
it("should not only have shop query", function(done){
var req = httpMocks.createRequest({
method: 'GET',
query: {
foo: "bar",
bar: "foo"
}
});
var res = httpMocks.createResponse()
var fn = middleware.hasOnlyQuery(["foo"])(req, res, function(err){
expect(err).to.equal("route")
return done()
})
})
Is this the proper way to do this? Is there any way to make this simpler / easier, perhaps converting it to a promise so I can use chai-as-promised?
Note: _.hasOnly is a custom underscore mixin.
Aucun commentaire:
Enregistrer un commentaire