Skip to content

Commit

Permalink
Test: add headers to testing utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmrcaga committed Mar 10, 2022
1 parent 7f50a15 commit e44ccef
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
45 changes: 45 additions & 0 deletions test/test-utils/test-utils.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
27 changes: 23 additions & 4 deletions test/utils/http.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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
};

0 comments on commit e44ccef

Please sign in to comment.