Created
December 19, 2018 18:36
-
-
Save theaccordance/d604aaf5ef8e51b0285df47da141c066 to your computer and use it in GitHub Desktop.
Express.js Middleware Unit Test Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sinon from 'sinon'; | |
import { mockReq, mockRes } from 'sinon-express-mock'; | |
import {handleSAMLPost, validateSubdomain} from "./login.handler"; | |
describe("login.spec", () => { | |
describe("validateSubdomain Middleware", () => { | |
test("should allow request to continue if request comes from a valid subdomain", () => { | |
const request = mockReq({ | |
hostname: "octan.localhost" | |
}); | |
const response = mockRes(); | |
const next = jest.fn(); | |
validateSubdomain(request, response, next); | |
expect(next).toHaveBeenCalledWith(); | |
sinon.assert.notCalled(response.sendStatus); | |
}); | |
test("should return a 404 if request comes from an unknown/invalid subdomain", () => { | |
const request = mockReq({ | |
hostname: "doesnotexist.localhost" | |
}); | |
const response = mockRes(); | |
const next = jest.fn(); | |
validateSubdomain(request, response, next); | |
expect(next).not.toHaveBeenCalled(); | |
sinon.assert.calledOnce(response.sendStatus); | |
sinon.assert.calledWith(response.sendStatus, 404); | |
}); | |
}); | |
describe("initSAMLRequest", () => { | |
test("should return 200 status", () => { | |
const request = mockReq(); | |
const response = mockRes(); | |
handleSAMLPost(request, response); | |
sinon.assert.calledOnce(response.sendStatus); | |
sinon.assert.calledWith(response.sendStatus, 200); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment