axios-cache-interceptor/test/util/cache-predicate.test.ts
Arthur Fiorette 48e33c5d5a
feat: etag and if-modified-since support (#53)
* feat(WIP): etag and if-modified-since support

* test: fixed test name

* fix: merge response headers

* test: add etag / last-modified tests

* test: add must-revalidate tests

* fix: handle expirationTime 0 as true.

* tests: refactored some tests

* test: added keepIfStale tests

* fix: remove axios error for 304 requests

* fix: possible infinite loop on validateStatus function

* tests: ignore code that is hard test from coverage

* fix: use Last-Modified header for modifiedSince

If-Modified-Since is never sent by a server, it's
a client only header. However Last-Modified is sent
by the server to indicate the last time the returned
version was modified (it might not be the last version
depending on cache configuration on intermediate
servers)

* test: use validateStatus in mock

This more closely match default axios adapter.

* fix: validateStatus handling all cases

* refactor: use cache.createdAt if the last-modified header isn't present

* test: etag with predefined value

* test: added more test cases

* fix: fixed logic in some tests

* docs: initial documentation

* fix: manual values work out of the box

This removes header requirement from server.

* docs: add details to etag and modifiedSince features

* fix: delete custom headers from response

* feat: accept all configurations from global axios

Merging global into local configuration enable user
to use global configuration for all options and remove
the need to check local and global values every time.

* fix: preserve original types from AxiosInstance

The only value axios needs is a URL, and in the second definition of the method, there is already a URL parameter, so it can be undefined.

* Fix: defaults modifiedSince back to false.

Avoids breaking changes.

* docs: fix etag examples

* docs: document internal headers

* refactor: ternary operator :)

* style: prettified code

* test: remove modifiedSince: false in tests since this is the default

* docs: fix headers examples

* docs: fixed example formatting

* tests: split tests into multiple files to test them faster

* docs: correct jsdoc empty object

Co-authored-by: Charly Koza <cka@f4-group.com>
2021-11-15 10:43:20 -03:00

108 lines
3.2 KiB
TypeScript

import { checkPredicateObject } from '../../src/util/cache-predicate';
import { createResponse } from '../utils';
describe('tests cache predicate object', () => {
it('tests statusCheck with tuples', () => {
const response = createResponse({ 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 = createResponse({ 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 = createResponse({
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 = createResponse({
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 = createResponse({
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 = createResponse({
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();
});
});