Skip to content

Commit 39efd69

Browse files
authored
Chore: setting headers no cache (#144)
* Setting headers to prevent caching on makeHttpRequest
1 parent 3c4a600 commit 39efd69

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Handful of utilities you should keep in your toolbelt to handle offline/online connectivity in React Native. It supports iOS, Android and Windows platforms. You can leverage all the functionalities provided or just the ones that suits your needs, the modules are conveniently decoupled.
77

88
## Important (Please read)
9-
**This is the documentation for version 4.0.0-beta.1. If you are migrating from v3 to v4, check the [release notes](https://github.com/rgommezz/react-native-offline/releases/tag/v4.0.0-beta.0)**
9+
**This is the documentation for version 4.0.0-beta.2. If you are migrating from v3 to v4, check the [release notes](https://github.com/rgommezz/react-native-offline/releases/tag/v4.0.0-beta.0)**
1010

1111
Please try to use v4. Some of the core has been rewritten from scratch using TDD approach and it fixes some of the outstanding [issues](https://github.com/rgommezz/react-native-offline/pull/141) that v3 presented. If you are using RN v0.56 or higher v3 won't work. Don't be misled by the word *beta*, it's a precaution measure that I've set myself because I am human and I may have made some unwitting mistake :robot:. It'd be appreciated to gather feedback early on to draft the final *stable* release. Also, an example application is coming soon to better illustrate real case scenarios of usage of the library. **Check out the [installation details](#installation).**
1212

@@ -75,7 +75,7 @@ This gives you the power to prioritize our work and support the project contribu
7575
## Installation
7676
This library supports React Native v0.55 or higher.
7777
```
78-
$ yarn add [email protected].1
78+
$ yarn add [email protected].2
7979
```
8080

8181
#### Android

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-offline",
3-
"version": "4.0.0-beta.1",
3+
"version": "4.0.0-beta.2",
44
"description": "Handy toolbelt to deal with offline mode in React Native applications. Cross-platform, provides a smooth redux integration.",
55
"main": "./src/index.js",
66
"author": "Raul Gomez Acuña <[email protected]> (https://github.com/rgommezz)",

src/utils/makeHttpRequest.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ type ResolvedValue = {
2323
status: number,
2424
};
2525

26+
export const headers = {
27+
'Cache-Control': 'no-cache, no-store, must-revalidate',
28+
Pragma: 'no-cache',
29+
Expires: 0,
30+
};
31+
2632
/**
2733
* Utility that promisifies XMLHttpRequest in order to have a nice API that supports cancellation.
2834
* @param method
@@ -65,6 +71,9 @@ export default function makeHttpRequest({
6571
status: this.status,
6672
});
6773
};
74+
Object.keys(headers).forEach((key: string) => {
75+
xhr.setRequestHeader(key, headers[key]);
76+
});
6877
xhr.send(null);
6978
},
7079
);

test/makeHttpRequest.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import makeHttpRequest from '../src/utils/makeHttpRequest';
1+
import makeHttpRequest, { headers } from '../src/utils/makeHttpRequest';
22
import {
33
DEFAULT_HTTP_METHOD,
44
DEFAULT_PING_SERVER_URL,
55
DEFAULT_TIMEOUT,
66
} from '../src/utils/constants';
77

88
const mockOpen = jest.fn();
9+
const mockSetRequestHeader = jest.fn();
910
const mockSend = jest.fn();
1011
const mockSetTimeout = jest.fn();
1112
const mockOnLoad = jest.fn();
@@ -34,22 +35,26 @@ global.XMLHttpRequest = class MockXMLHttpRequest {
3435
break;
3536
}
3637
}
38+
3739
set timeout(t) {
3840
mockSetTimeout(t);
3941
this.t = t;
4042
}
43+
4144
set onload(fn) {
4245
mockOnLoad();
4346
if (this.callbackToFire.includes('onload')) {
4447
fn.call(this);
4548
}
4649
}
50+
4751
set onerror(fn) {
4852
mockOnError();
4953
if (this.callbackToFire === 'onerror') {
5054
fn.call(this);
5155
}
5256
}
57+
5358
set ontimeout(fn) {
5459
mockOnTimeout();
5560
if (this.callbackToFire === 'ontimeout') {
@@ -58,6 +63,7 @@ global.XMLHttpRequest = class MockXMLHttpRequest {
5863
}
5964
};
6065
global.XMLHttpRequest.prototype.open = mockOpen;
66+
global.XMLHttpRequest.prototype.setRequestHeader = mockSetRequestHeader;
6167
global.XMLHttpRequest.prototype.send = mockSend;
6268

6369
describe('makeHttpRequest', () => {
@@ -75,12 +81,21 @@ describe('makeHttpRequest', () => {
7581
timeout: 5000,
7682
};
7783
it('sets up the XMLHttpRequest configuration properly', async () => {
84+
const headerKeys = Object.keys(headers);
7885
makeHttpRequest(params);
7986
expect(mockOpen).toHaveBeenCalledWith(params.method, params.url);
8087
expect(mockSetTimeout).toHaveBeenCalledWith(params.timeout);
8188
expect(mockOnLoad).toHaveBeenCalledTimes(1);
8289
expect(mockOnError).toHaveBeenCalledTimes(1);
8390
expect(mockOnTimeout).toHaveBeenCalledTimes(1);
91+
expect(mockSetRequestHeader).toHaveBeenCalledTimes(3);
92+
headerKeys.forEach((key, index) => {
93+
expect(mockSetRequestHeader).toHaveBeenNthCalledWith(
94+
index + 1,
95+
key,
96+
headers[key],
97+
);
98+
});
8499
expect(mockSend).toHaveBeenCalledWith(null);
85100
});
86101

0 commit comments

Comments
 (0)