Skip to content

Instantly share code, notes, and snippets.

@nicosommi
Created May 26, 2015 18:44
Show Gist options
  • Save nicosommi/6917b308ecaf1a64fd5f to your computer and use it in GitHub Desktop.
Save nicosommi/6917b308ecaf1a64fd5f to your computer and use it in GitHub Desktop.
unirest-header
"use strict";
var express = require("express");
var bodyParser = require("body-parser");
var unirest = require("unirest");
var sinon = require("sinon");
require("should");
var app = express(), server;
var host, port, url;
describe("bodyparser", function unirestDescribe () {
var postCall,
exampleRequest;
before(function beforeAll(done) {
postCall = sinon.spy();
exampleRequest = {data: {
id: 1,
type: "Person",
name: "Nico"
}
};
app.use(bodyParser.json({ type: "application/vnd.api+json" }));
app.post("/", function handleRoot(req, res) {
console.log("body is ", req.body);
postCall(req.body);
res.send({});
});
server = app.listen(3000, function liftServer () {
host = server.address().address;
port = server.address().port;
//url = `http://${host}:${port}`;
url = "http://localhost:3000";
console.log("Example app listening at http://%s:%s", host, port);
done();
});
});
after(function afterAll(done) {
server.close(function closeCallback() {
done();
});
});
it("should send a custom content type as a header using the type function", function jsonTest (done) {
unirest
.post(url + "/")
.type("application/vnd.api+json")
.send(exampleRequest)
.end(function endJsonTest() {
postCall.calledWith(exampleRequest).should.be.true;
done();
});
});
it("should send a custom header with the header function", function jsonTest (done) {
unirest
.post(url + "/")
.header("Content-Type", "application/vnd.api+json")
.send(exampleRequest)
.end(function endJsonTest() {
postCall.calledWith(exampleRequest).should.be.true;
done();
});
});
});
{
"name": "unirest-cmt-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha index.js"
},
"author": "nicosommi",
"license": "MIT",
"dependencies": {
"body-parser": "^1.12.3",
"express": "^4.12.3",
"mocha": "^2.2.4",
"should": "^6.0.1",
"sinon": "^1.14.1",
"unirest": "^0.4.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment