Skip to content

Commit

Permalink
test: add edge test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
theritikchoure committed Oct 9, 2023
1 parent 12f425c commit e0e2e52
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 19 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,34 @@ To prevent from the duplicate API requests, **CarrierJs** comes into the picture

## How it works internally

First, it checks to see whether data exists in the cache. If it exists, it returns data from the cache as a response. If it doesn't exist then it read data from the API server.
Carrier JS operates with a robust internal mechanism that optimizes API requests through caching and allows you to control data freshness. Here's a detailed breakdown of its internal workings:

Then it writes to the cache and returns the data as response. The subsequent requests will be served through the cache.
1. **Cache Check:** When you initiate an API request using Carrier JS, it first checks its cache to determine if the requested data is already stored there.

If you want fresh data everytime from the API server, you can pass an extra parameter called `refresh` with value `true` after the API server url.
2. **Cache Hit:** If the data is found in the cache, Carrier JS promptly retrieves this cached data and returns it as the response. This cache hit not only accelerates response times but also reduces the load on the API server.

This diagram shows how carrierjs internally fulfills the request with its ultimate caching feature.
3. **Cache Miss:** In cases where the data is not present in the cache, Carrier JS seamlessly proceeds to fetch the data from the API server. It makes a network request to obtain the required information.

4. **Cache Write:** After successfully fetching the data from the API server, Carrier JS writes a copy of this data into its cache. This cache write operation ensures that the data is readily available for subsequent requests.

5. **Caching Benefits:** Subsequent requests for the same data benefit from this caching mechanism. Carrier JS serves the data directly from the cache, eliminating the need to re-fetch it from the server. This results in improved performance, reduced network traffic, and efficient resource utilization.

#### Controlling Data Freshness with refresh Parameter

Carrier JS provides you with the flexibility to decide whether you want to retrieve fresh data from the API server or use cached data. You can achieve this control by utilizing the `refresh` parameter.

- **Using `refresh`:** If you require the freshest data from the API server every time, simply include an extra parameter in your request URL — `refresh=true`. This instructs Carrier JS to bypass the cache and initiate a new request to obtain the latest data from the server.

#### Ultimate Caching Feature

Carrier JS's caching feature is designed to enhance the performance and efficiency of your application. It reduces the overhead of redundant server requests, minimizes bandwidth consumption, and ensures a responsive user experience.

The diagram below illustrates the seamless flow of Carrier JS as it internally fulfills requests, highlighting the prowess of its caching feature:

![Flow of Carrierjs](./docs/assets/images/code-flow.png)

With Carrier JS, you have the power to strike the perfect balance between performance optimization and data accuracy, all while minimizing the load on your API server.

## Benefits

There are several benefits of caching your API's response. Here are some of them:
Expand Down
12 changes: 12 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Security Policy

## Supported Versions

The **latest released version** of `carrierjs` is supported.

## Reporting a Vulnerability

To report a vulnerability please send an email with the details to `ritvikchoure65+carrierjs@gmail.com`.
This will help us to assess the risk and start the necessary steps.

Thanks for helping to keep `carrierjs` secure!
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
"version": "2.5.2",
"description": "Powerful and simple promise based HTTP client for the browser with ultimate caching",
"main": "carrier.js",
"author": "theritikchoure",
"author": {
"name": "Ritik Chourasiya",
"email": "ritvikchoure65@gmail.com",
"url": "https://theritikchoure.github.io/"
},
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"publish": "npm version patch; npm publish",
"test": "start ./test/runner.html"
},
"keywords": [
"xhr",
Expand Down
1 change: 1 addition & 0 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<title>CarrierJs - Testing Page</title>
<link rel="stylesheet" href="style/mocha.css" />
<link rel="stylesheet" href="style/global.css">
<link rel="shortcut icon" href="../docs/assets/images/carrierjs-favicon.png">
</head>
<body>
<!-- 1. Placeholder node that will contain the test results -->
Expand Down
101 changes: 88 additions & 13 deletions test/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ var expect = chai.expect;
let assert = chai.assert;

let getMethod = async () => {
try {
const response = await carrier.get('https://jsonplaceholder.typicode.com/todos/');
return response;
} catch (error) {
return error;
}
}
try {
const response = await carrier.get(
"https://jsonplaceholder.typicode.com/todos/"
);
return response;
} catch (error) {
return error;
}
};

describe("Testing Get Method", function async () {
describe("Testing Get Method", function async() {
it("result.response should be an array", async function () {
let result = await getMethod();
console.log(result);
assert.isArray(result.response);
});

it("result.headers should be an object", async function () {
let result = await getMethod();
assert.isObject(result.headers);
Expand All @@ -27,9 +30,81 @@ describe("Testing Get Method", function async () {
let result = await getMethod();
assert.strictEqual(result.status, 200);
});
it("response length should be 6", async function () {

it("response length should be 200", async function () {
let result = await getMethod();
assert.lengthOf(result, 6);
assert.lengthOf(result.response, 200);
});
});

describe("Testing Post Method", function async() {
this.timeout(5000);
it("should successfully send a POST request and receive a 201 status code", async function () {
const data = { title: "Test", completed: false };
const response = await carrier.post(
"https://jsonplaceholder.typicode.com/todos/",
data
);
assert.strictEqual(response.status, 201);
});
});

describe("Testing Put Method", function async() {
this.timeout(5000);
it("should successfully send a PUT request and receive a 200 status code", async function () {
const data = { title: "Updated", completed: true };
const response = await carrier.put(
"https://jsonplaceholder.typicode.com/todos/1",
data
);
assert.strictEqual(response.status, 200);
});
});

describe("Testing Patch Method", function async() {
this.timeout(5000);

it("should successfully send a PATCH request and receive a 200 status code", async function () {
const data = { completed: true };
const response = await carrier.patch(
"https://jsonplaceholder.typicode.com/todos/1",
data
);
assert.strictEqual(response.status, 200);
});
});

describe("Testing Delete Method", function async() {
this.timeout(5000);

it("should successfully send a DELETE request and receive a 200 status code", async function () {
const response = await carrier.delete(
"https://jsonplaceholder.typicode.com/todos/1"
);
assert.strictEqual(response.status, 200);
});
});

describe("Testing Error Handling", function () {
this.timeout(5000);

it("should handle network errors gracefully", async function () {
try {
const response = await carrier.get(
"https://nonexistent-server.invalid/api/data"
);
} catch (error) {
assert.strictEqual(error.code, "NETWORK_ERROR");
}
});

it("should handle 404 errors", async function () {
try {
const response = await carrier.get(
"https://jsonplaceholder.typicode.com/nonexistent"
);
} catch (error) {
assert.strictEqual(error.response.status, 404);
}
});
});
});

0 comments on commit e0e2e52

Please sign in to comment.