Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhaligowski committed Dec 4, 2023
1 parent 02a3c5d commit 23a366a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
8 changes: 4 additions & 4 deletions lib/http-mock.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ export interface RequestOptions {

export type MockRequest<T extends IncomingMessage> = T & {
_setParameter: (key: string, value?: string) => void;
_setSessionVariable: (variable: string, value: string) => void;
_setCookiesVariable: (variable: string, value: string) => void;
_setSignedCookiesVariable: (variable: string, value: string) => void;
_setSessionVariable: (variable: string, value?: string) => void;
_setCookiesVariable: (variable: string, value?: string) => void;
_setSignedCookiesVariable: (variable: string, value?: string) => void;
_setHeadersCookiesVariable: (variable: string, value: string) => void;
_setFilesCookiesVariable: (variable: string, value: string) => void;
_setFilesCookiesVariable: (variable: string, value?: string) => void;
_setMethod: (method?: string) => void;
_setURL: (value?: string) => void;
_setOriginalUrl: (value?: string) => void;
Expand Down
59 changes: 28 additions & 31 deletions test/lib/mockRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,29 +445,26 @@ describe('mockRequest', function() {
});

describe('.acceptsLanguages()', function() {
let request: mockRequest.MockRequest<IncomingMessage>;

beforeEach(function() {
const request = mockRequest.createRequest({ headers: { 'Accept-Language': 'en-GB' }});
});

it('returns type if the same as Accept-Language header', function() {
const request = mockRequest.createRequest({ headers: { 'Accept-Language': 'en-GB' }});
expect(request.acceptsLanguages('en-GB')).to.equal('en-GB');
});

it('returns the first matching type of an array of types', function() {
const request = mockRequest.createRequest({ headers: { 'Accept-Language': 'en-GB' }});
expect(request.acceptsLanguages(['de-DE', 'en-GB'])).to.equal('en-GB');
});

it('returns false when types dont match', function() {
const request = mockRequest.createRequest({ headers: { 'Accept-Language': 'en-GB' }});
expect(request.acceptsLanguages(['de-DE', 'fr-FR'])).to.equal(false);
});
});

describe('.range()', function() {
it('returns undefined if there is no Range header', function() {
const request = mockRequest.createRequest();
expect(request.range).to.be.undefined;
expect(request.range(10)).to.be.undefined;
});

const tests = [
Expand Down Expand Up @@ -625,107 +622,107 @@ describe('mockRequest', function() {

});

describe('._setSessionconstiable()', function() {
describe('._setSessionVariable()', function() {
it('should set session constiable, when called with key and value', function() {
const request = mockRequest.createRequest();
request._setSessionconstiable('key', 'value');
request._setSessionVariable('key', 'value');
expect(request.session.key).to.equal('value');
});

it('should unset session constiable, when called with key and no value', function() {
const request = mockRequest.createRequest();
request._setSessionconstiable('key', 'value');
request._setSessionconstiable('key');
request._setSessionVariable('key', 'value');
request._setSessionVariable('key');
expect(request.param('key')).to.not.exist;
});

it('should throw an error, when called with no arguments', function () {
const request = mockRequest.createRequest();
expect(request._setSessionconstiable).to.throw;
expect(request._setSessionVariable).to.throw;
});

});

describe('._setCookiesconstiable()', function() {
describe('._setCookiesVariable()', function() {

it('should set cookie, when called with key and value', function() {
const request = mockRequest.createRequest();
request._setCookiesconstiable('key', 'value');
request._setCookiesVariable('key', 'value');
expect(request.cookies.key).to.equal('value');
});

it('should unset cookie, when called with key and no value', function() {
const request = mockRequest.createRequest();
request._setCookiesconstiable('key', 'value');
request._setCookiesconstiable('key');
request._setCookiesVariable('key', 'value');
request._setCookiesVariable('key');
expect(request.cookies.key).to.not.exist;
});

it('should throw an error, when called with no arguments', function () {
const request = mockRequest.createRequest();
expect(request._setCookiesconstiable).to.throw;
expect(request._setCookiesVariable).to.throw;
});

});

describe('._setSignedCookiesconstiable()', function() {
describe('._setSignedCookiesVariable()', function() {

it('should set signed cookie, when called with key and value', function() {
const request = mockRequest.createRequest();
request._setSignedCookiesconstiable('key', 'value');
request._setSignedCookiesVariable('key', 'value');
expect(request.signedCookies.key).to.equal('value');
});

it('should unset signed cookie, when called with key and no value', function() {
const request = mockRequest.createRequest();
request._setSignedCookiesconstiable('key', 'value');
request._setSignedCookiesconstiable('key');
request._setSignedCookiesVariable('key', 'value');
request._setSignedCookiesVariable('key');
expect(request.signedCookies.key).to.not.exist;
});

it('should throw an error, when called with no arguments', function () {
const request = mockRequest.createRequest();
expect(request._setSignedCookiesconstiable).to.throw;
expect(request._setSignedCookiesVariable).to.throw;
});

});

describe('._setHeadersconstiable()', function() {
describe('._setHeadersVariable()', function() {

it('should set header, when called with key and value', function() {
const request = mockRequest.createRequest();
request._setHeadersconstiable('key', 'value');
request._setHeadersVariable('key', 'value');
expect(request.get('key')).to.equal('value');
expect(request.header('key')).to.equal('value');
expect(request.getHeader('key')).to.equal('value');
});

it('should throw an error, when called with missing arguments', function () {
const request = mockRequest.createRequest();
expect(request._setHeadersconstiable).to.throw;
expect(request._setHeadersconstiable.bind(null, 'key')).to.throw;
expect(request._setHeadersVariable).to.throw;
expect(request._setHeadersVariable.bind(null, 'key')).to.throw;
});

});

describe('._setFilesconstiable()', function() {
describe('._setFilesVariable()', function() {

it('should set file, when called with key and value', function() {
const request = mockRequest.createRequest();
request._setFilesconstiable('key', 'value');
request._setFilesVariable('key', 'value');
expect(request.files.key).to.equal('value');
});

it('should unset file, when called with key and no value', function() {
const request = mockRequest.createRequest();
request._setFilesconstiable('key', 'value');
request._setFilesconstiable('key');
request._setFilesVariable('key', 'value');
request._setFilesVariable('key');
expect(request.files.key).to.not.exist;
});

it('should throw an error, when called with no arguments', function () {
const request = mockRequest.createRequest();
expect(request._setFilesconstiable).to.throw;
expect(request._setFilesVariable).to.throw;
});

});
Expand Down

0 comments on commit 23a366a

Please sign in to comment.