UNPKG

digipinjs

Version:

A comprehensive TypeScript library for encoding and decoding Indian geographic coordinates into DIGIPIN format (Indian Postal Digital PIN system). Features CLI tools, caching, batch processing, and Express middleware for seamless integration.

89 lines (88 loc) 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const middleware_1 = require("../middleware"); describe('DigiPin Middleware', () => { let mockRequest; let mockResponse; let nextFunction; beforeEach(() => { mockRequest = { header: ((name) => undefined) }; mockResponse = { setHeader: (name, value) => { return mockResponse; }, status: (code) => { return mockResponse; }, json: (body) => { return mockResponse; } }; nextFunction = () => { }; }); it('should set X-DIGIPIN header for valid coordinates', () => { const lat = '28.6139'; const lng = '77.2090'; let headerSet = false; let headerValue = ''; mockRequest.header = ((name) => { if (name === 'x-lat') return lat; if (name === 'x-lng') return lng; return undefined; }); mockResponse.setHeader = (name, value) => { if (name === 'X-DIGIPIN') { headerSet = true; headerValue = value; } return mockResponse; }; const middleware = (0, middleware_1.digiPinMiddleware)(); middleware(mockRequest, mockResponse, nextFunction); (0, chai_1.expect)(headerSet).to.be.true; (0, chai_1.expect)(headerValue).to.match(/^[FC98J327K456LMPT]{3}-[FC98J327K456LMPT]{3}-[FC98J327K456LMPT]{4}$/); }); it('should not set X-DIGIPIN header for missing coordinates', () => { let headerSet = false; mockResponse.setHeader = (name, value) => { headerSet = true; return mockResponse; }; const middleware = (0, middleware_1.digiPinMiddleware)(); middleware(mockRequest, mockResponse, nextFunction); (0, chai_1.expect)(headerSet).to.be.false; }); it('should not set X-DIGIPIN header for invalid coordinates', () => { let headerSet = false; mockRequest.header = (() => 'invalid'); mockResponse.setHeader = (name, value) => { headerSet = true; return mockResponse; }; const middleware = (0, middleware_1.digiPinMiddleware)(); middleware(mockRequest, mockResponse, nextFunction); (0, chai_1.expect)(headerSet).to.be.false; }); it('should handle out of bounds coordinates', () => { let headerSet = false; mockRequest.header = ((name) => { if (name === 'x-lat') return '1.0'; // Below minLat if (name === 'x-lng') return '77.2090'; return undefined; }); mockResponse.setHeader = (name, value) => { headerSet = true; return mockResponse; }; const middleware = (0, middleware_1.digiPinMiddleware)(); middleware(mockRequest, mockResponse, nextFunction); (0, chai_1.expect)(headerSet).to.be.false; }); });