From e44ccefdead1246f9e4ff05b3d3146ffada8a01f Mon Sep 17 00:00:00 2001 From: Jo Colina Date: Thu, 10 Mar 2022 22:44:04 +0100 Subject: [PATCH] Test: add headers to testing utils --- test/test-utils/test-utils.js | 45 +++++++++++++++++++++++++++++++++++ test/utils/http.js | 27 +++++++++++++++++---- 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 test/test-utils/test-utils.js diff --git a/test/test-utils/test-utils.js b/test/test-utils/test-utils.js new file mode 100644 index 0000000..b10e88d --- /dev/null +++ b/test/test-utils/test-utils.js @@ -0,0 +1,45 @@ +const { expect } = require('chai'); +const { Request, Response, Headers } = require('../utils/http'); + +describe('Testing utilities', () => { + describe('Headers', () => { + it('Should instanciate with multiple headers', () => { + const headers = new Headers({ + Authorization: 'plep', + 'Content-Type': 'plop' + }); + + expect(headers.headers).to.not.have.property('Authorization'); + expect(headers.headers).to.have.property('authorization'); + expect(headers.get('Authorization')).to.be.eql('plep'); + expect(headers.get('content-type')).to.be.eql('plop'); + }); + + it('Should be able to add keys to headers', () => { + const headers = new Headers(); + expect(Object.keys(headers.headers)).to.have.length(0); + headers.set('With-Uppercase', 'plep'); + headers.set('lowercase', 'plop'); + + expect(headers.headers).to.have.property('with-uppercase'); + expect(headers.get('With-uppercase')).to.be.eql('plep'); + expect(headers.get('LOWERCASE')).to.be.eql('plop'); + }); + }); + + describe('Response', () => { + it('Should have empty headers', () => { + const response = new Response(); + expect(response.headers).to.be.instanceof(Headers); + expect(Object.keys(response.headers.headers)).to.have.length(0); + }); + }); + + describe('Request', () => { + it('Should have empty headers', () => { + const request = new Request(); + expect(request.headers).to.be.instanceof(Headers); + expect(Object.keys(request.headers.headers)).to.have.length(0); + }); + }); +}); diff --git a/test/utils/http.js b/test/utils/http.js index ead4ebb..525c4d1 100644 --- a/test/utils/http.js +++ b/test/utils/http.js @@ -1,9 +1,27 @@ // Mocks for request & Response +class Headers { + constructor(headers={}) { + this.headers = {}; + for(const [k, v] of Object.entries(headers)) { + this.set(k, v); + } + } + + get(name) { + return this.headers[name.toLowerCase()] || null; + } + + set(name, value) { + this.headers[name.toLowerCase()] = value; + } +} + class Request { - constructor({ method='get', body, url }) { + constructor({ method='get', body, url, headers={} }={}) { this.url = url; this.body = body; this.method = method; + this.headers = new Headers(headers); } json() { @@ -12,15 +30,16 @@ class Request { } class Response { - constructor(body, { headers, method, status }) { + constructor(body, { headers={}, method, status }={}) { this.body = body; - this.headers = headers; this.method = method; this.status = status; + this.headers = new Headers(headers); } } module.exports = { Response, - Request + Request, + Headers };