Skip to content

Commit e202218

Browse files
committed
added tests
1 parent 9d6e3b4 commit e202218

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

node_package/src/ReactOnRails.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ ctx.ReactOnRails = {
7878
*/
7979

8080
getAuthenticityToken() {
81-
/*const token = _.find(document.querySelectorAll('meta'), ['name', 'csrf-token']);*/
8281
const token = document.querySelector('meta[name="csrf-token"]');
8382
return token ? token.content : null;
8483
},

node_package/tests/ReactOnRails.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,25 @@ test('setStore and getStore', (assert) => {
124124

125125
assert.deepEqual(ReactOnRails.stores(), expected);
126126
});
127+
128+
test('getAuthenticityToken and authenticityHeader', (assert) => {
129+
assert.plan(2);
130+
131+
const testToken = 'TEST_CSRF_TOKEN';
132+
133+
var meta = document.createElement('meta');
134+
meta.name = 'csrf-token';
135+
meta.content = testToken;
136+
document.head.appendChild(meta);
137+
138+
var realToken = ReactOnRails.getAuthenticityToken();
139+
140+
assert.equal(realToken, testToken,
141+
'getAuthenticityToken can read Rails CSRF token from <meta>');
142+
143+
var realHeader = ReactOnRails.authenticityHeader();
144+
145+
assert.deepEqual(realHeader, { 'X-CSRF-Token': testToken, 'X-Requested-With': 'XMLHttpRequest' },
146+
'authenticityHeader returns valid header with CFRS token'
147+
);
148+
});

0 commit comments

Comments
 (0)