rest-api-boilerplate
Version:
Boilerplate for building REST APIs with node.js and MongoDB
95 lines (86 loc) • 3.36 kB
HTML
<section class="suite">
<h1>POST, GET, UPDATE, DELETE Cars: </h1>
<dl>
<section class="suite">
<h1>POST car request: </h1>
<dl>
<dt>should return success message</dt>
<dd><pre><code>server.post("/api/cars")
.send(valid_car_specs)
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
res.status.should.equal(200);
res.body.message.should.equal("car added successfully");
done();
});</code></pre></dd>
</dl>
</section>
<section class="suite">
<h1>POST car request: </h1>
<dl>
<dt>should return error message</dt>
<dd><pre><code>server.post("/api/cars")
.send(invalid_car_specs)
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
res.status.should.equal(200);
res.body.error.message.should.equal("car validation failed");
done();
});</code></pre></dd>
</dl>
</section>
<section class="suite">
<h1>GET all cars request: </h1>
<dl>
<dt>should return success message</dt>
<dd><pre><code>server.get("/api/cars")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
// status code should be 200
res.status.should.equal(200);
// type of results should be of type array
(Array.isArray(res.body.results)).should.be.true();
// all elements of array should have 3 properties: model, make and year per the spec
for (var i = 0; i < res.body.results.length; i++){
(Object.keys(res.body.results[i]).length === car_properties.length).should.be.true();
for (var j = 0; j < car_properties.length; j++){
(res.body.results[i].hasOwnProperty(car_properties[j])).should.be.true();
}
}
done();
});</code></pre></dd>
</dl>
</section>
</dl>
</section>
<section class="suite">
<h1>POST, GET, UPDATE, DELETE Ratings: </h1>
<dl>
<section class="suite">
<h1>GET all ratings request: </h1>
<dl>
<dt>should return success message</dt>
<dd><pre><code>server.get("/api/ratings")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
// status code should be 200
res.status.should.equal(200);
// type of results should be of type array
(Array.isArray(res.body.results)).should.be.true();
// all elements of array should have all ratings properties per the spec
for (var i = 0; i < res.body.results.length; i++){
(Object.keys(res.body.results[i]).length === rating_properties.length).should.be.true();
for (var j = 0; j < rating_properties.length; j++){
(res.body.results[i].hasOwnProperty(rating_properties[j])).should.be.true();
}
}
done();
});</code></pre></dd>
</dl>
</section>
</dl>
</section>