Skip to content

Commit

Permalink
tests: added cache predicate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 13, 2021
1 parent 5ddf9ed commit bb552c8
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/util/cache-predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export type CachePredicateObject = {
statusCheck?: [start: number, end: number] | ((status: number) => boolean);

/**
* Matches if the response header container all keys. A tuple also checks for values.
* Matches if the response header container all keys.
* A tuple also checks for values.
* Can also be a predicate.
*/
containsHeaders?: (string | [string, string])[];
containsHeaders?: Record<string, true | string | ((header: string) => boolean)>;

/**
* Check if the desired response matches this predicate.
Expand All @@ -39,16 +41,25 @@ export function checkPredicateObject(
}

if (containsHeader) {
for (const entry of containsHeader) {
if (typeof entry === 'string') {
if (!response.headers[entry]) {
return false;
}
} else {
const [key, value] = entry;
if (!response.headers[key] || response.headers[key] == value) {
return false;
}
for (const [headerName, value] of Object.entries(containsHeader)) {
const header = response.headers[headerName];

// At any case, if the header is not found, the predicate fails.
if (!header) {
return false;
}

switch (typeof value) {
case 'string':
if (header != value) {
return false;
}
break;
case 'function':
if (!value(header)) {
return false;
}
break;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/mocks/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function mockAxios(
...options
});

// Axios interceptors are a stack, so apply this after the cache interceptor
cachedApi.interceptors.request.use((config) => {
config.adapter = async (config) => ({
data: true,
Expand Down
136 changes: 136 additions & 0 deletions test/util/cache-predicate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// /**
// * The status predicate, if a tuple is returned,
// * the first and seconds value means the interval (inclusive) accepted.
// * Can also be a function.
// */
// statusCheck?: [start: number, end: number] | ((status: number) => boolean);

// /**
// * Matches if the response header container all keys. A tuple also checks for values.
// */
// containsHeaders?: (string | [string, string])[];

// /**
// * Check if the desired response matches this predicate.
// */
// responseMatch?: <T = any>(res: T | undefined) => boolean;

import { AxiosResponse } from 'axios';
import { checkPredicateObject } from '../../src/util/cache-predicate';

const Response = (config: Partial<AxiosResponse>): AxiosResponse => {
return {
status: 200,
headers: {},
config: {},
data: {},
statusText: '',
request: {},
...config
};
};

describe('tests cache predicate object', () => {
it('tests statusCheck with tuples', () => {
const response = Response({ status: 764 });

const falsyTest = checkPredicateObject(response, { statusCheck: [200, 299] });
const truthyTest = checkPredicateObject(response, { statusCheck: [760, 769] });

expect(falsyTest).toBeFalsy();
expect(truthyTest).toBeTruthy();
});

it('tests statusCheck with a predicate', () => {
const response = Response({ status: 764 });

const falsyTest = checkPredicateObject(response, {
statusCheck: (status) => status >= 200 && status <= 299
});

const truthyTest = checkPredicateObject(response, {
statusCheck: (status) => status >= 760 && status <= 769
});

expect(falsyTest).toBeFalsy();
expect(truthyTest).toBeTruthy();
});

it('tests containsHeader with string array', () => {
const response = Response({
headers: { 'Content-Type': 'application/json' }
});

const hasContentTypeLowercase = checkPredicateObject(response, {
containsHeaders: { 'content-type': true }
});

const hasContentType = checkPredicateObject(response, {
containsHeaders: { 'Content-Type': true }
});

expect(hasContentTypeLowercase).toBeFalsy();
expect(hasContentType).toBeTruthy();
});

it('tests containsHeader with string tuple', () => {
const response = Response({
headers: { 'Content-Type': 'application/json' }
});

const headerExists = checkPredicateObject(response, {
containsHeaders: { 'content-type': 'application/json' }
});

const isXmlContent = checkPredicateObject(response, {
containsHeaders: { 'Content-Type': 'application/xml' }
});

const isJsonContent = checkPredicateObject(response, {
containsHeaders: { 'Content-Type': 'application/json' }
});

expect(headerExists).toBeFalsy();
expect(isXmlContent).toBeFalsy();
expect(isJsonContent).toBeTruthy();
});

it('tests containsHeader with string predicate', () => {
const response = Response({
headers: { 'Content-Type': 'application/json' }
});

const headerExists = checkPredicateObject(response, {
containsHeaders: { 'content-type': (header) => header == 'application/json' }
});

const isXmlContent = checkPredicateObject(response, {
containsHeaders: { 'Content-Type': (header) => header == 'application/xml' }
});

const isJsonContent = checkPredicateObject(response, {
containsHeaders: { 'Content-Type': (header) => header == 'application/json' }
});

expect(headerExists).toBeFalsy();
expect(isXmlContent).toBeFalsy();
expect(isJsonContent).toBeTruthy();
});

it('tests responseMatch', () => {
const response = Response({
data: { a: true, b: 1 }
});

const testStrict = checkPredicateObject(response, {
responseMatch: (data: any) => data && data.a === true && data.b === 1
});

const testError = checkPredicateObject(response, {
responseMatch: (data: any) => data && (data.a !== true || data.b !== 1)
});

expect(testStrict).toBeTruthy();
expect(testError).toBeFalsy();
});
});

0 comments on commit bb552c8

Please sign in to comment.