|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* eslint-env node, mocha */ |
| 4 | + |
| 5 | +const io = require('socket.io-client') |
| 6 | +const http = require('http') |
| 7 | + |
| 8 | +const mock = require('mock-require') |
| 9 | +const assert = require('assert') |
| 10 | + |
| 11 | +function makeMockSocket (headers, query) { |
| 12 | + return { |
| 13 | + handshake: { |
| 14 | + headers: Object.assign({}, headers), |
| 15 | + query: Object.assign({}, query) |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe('realtime', function () { |
| 21 | + describe('extractNoteIdFromSocket', function () { |
| 22 | + beforeEach(() => { |
| 23 | + mock('../lib/logger', {}) |
| 24 | + mock('../lib/history', {}) |
| 25 | + mock('../lib/models', {}) |
| 26 | + }) |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + delete require.cache[require.resolve('../lib/realtime')] |
| 30 | + mock.stopAll() |
| 31 | + }) |
| 32 | + |
| 33 | + describe('urlPath not set', function () { |
| 34 | + beforeEach(() => { |
| 35 | + mock('../lib/config', {}) |
| 36 | + realtime = require('../lib/realtime') |
| 37 | + }) |
| 38 | + |
| 39 | + let realtime |
| 40 | + |
| 41 | + it('return false if socket or socket.handshake not exists', function () { |
| 42 | + let noteId = realtime.extractNoteIdFromSocket() |
| 43 | + assert.strictEqual(false, noteId) |
| 44 | + |
| 45 | + noteId = realtime.extractNoteIdFromSocket({}) |
| 46 | + assert.strictEqual(false, noteId) |
| 47 | + }) |
| 48 | + |
| 49 | + it('return false if query not set and referer not set', function () { |
| 50 | + let noteId = realtime.extractNoteIdFromSocket(makeMockSocket({ |
| 51 | + otherHeader: 1 |
| 52 | + }, { |
| 53 | + otherQuery: 1 |
| 54 | + })) |
| 55 | + assert.strictEqual(false, noteId) |
| 56 | + }) |
| 57 | + |
| 58 | + it('return noteId from query', function () { |
| 59 | + // Arrange |
| 60 | + const incomingNoteId = 'myNoteId' |
| 61 | + const incomingSocket = makeMockSocket(undefined, { noteId: incomingNoteId }) |
| 62 | + |
| 63 | + // Act |
| 64 | + const noteId = realtime.extractNoteIdFromSocket(incomingSocket) |
| 65 | + // Assert |
| 66 | + assert.strictEqual(noteId, incomingNoteId) |
| 67 | + }) |
| 68 | + |
| 69 | + it('return noteId from old method (referer)', function () { |
| 70 | + // Arrange |
| 71 | + const incomingNoteId = 'myNoteId' |
| 72 | + const incomingSocket = makeMockSocket({ |
| 73 | + referer: `https://localhost:3000/${incomingNoteId}` |
| 74 | + }) |
| 75 | + |
| 76 | + // Act |
| 77 | + const noteId = realtime.extractNoteIdFromSocket(incomingSocket) |
| 78 | + // Assert |
| 79 | + assert.strictEqual(noteId, incomingNoteId) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + describe('urlPath is set', function () { |
| 84 | + let realtime |
| 85 | + it('return noteId from old method (referer) and urlPath set', function () { |
| 86 | + // Arrange |
| 87 | + const urlPath = 'hello' |
| 88 | + mock('../lib/config', { |
| 89 | + urlPath: urlPath |
| 90 | + }) |
| 91 | + realtime = require('../lib/realtime') |
| 92 | + const incomingNoteId = 'myNoteId' |
| 93 | + const incomingSocket = makeMockSocket({ |
| 94 | + referer: `https://localhost:3000/${urlPath}/${incomingNoteId}` |
| 95 | + }) |
| 96 | + |
| 97 | + // Act |
| 98 | + const noteId = realtime.extractNoteIdFromSocket(incomingSocket) |
| 99 | + // Assert |
| 100 | + assert.strictEqual(noteId, incomingNoteId) |
| 101 | + }) |
| 102 | + }) |
| 103 | + }) |
| 104 | +}) |
0 commit comments