All files / test test.ts

57.14% Statements 24/42
0% Branches 0/13
38.46% Functions 5/13
60.53% Lines 23/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1031x 1x 1x 1x 1x 1x   1x   1x           1x               1x     1x           1x 1x                                                                     1x 1x       1x 1x 1x 1x 1x                                         1x 1x   1x  
import test from 'ava';
import * as sinon from 'sinon';
const Koa = require('koa');
const KoaSession2 = require('koa-session2');
const { randomBytes } = require('crypto');
import {HandleKoaSession2, HandleKoaGenericSession, HandleKoaSession} from '../src/index';
 
const app = new Koa();
 
const KoaCtx: any = {
    cookies: {
        get: (key: string) => {}
    }
};
 
const KoaApp: any = {
    use: (...args: any[]) => {},
    keys: [],
    createContext: (req: any, res: any) => {
        return KoaCtx;
    }
};
 
test.beforeEach(t => {
});
 
test('test koa-session2', async t => {
    // session store for koa-session2
    class Store {
        sessions: any;
        __timer: any;
        constructor() {
            this.sessions = new Map();
            this.__timer = new Map();
        }
 
        getID(length: number) {
            return randomBytes(length).toString('hex');
        }
 
        async get(sid: string) {
            if (!this.sessions.has(sid)) return undefined;
            return JSON.parse(this.sessions.get(sid));
        }
 
        async set(session: any, { sid =  this.getID(24), maxAge = 86400000 } = {}) {
            if (this.sessions.has(sid) && this.__timer.has(sid)) {
                const __timeout = this.__timer.get(sid);
                if (__timeout) clearTimeout(__timeout);
            }
 
            if (maxAge) {
                this.__timer.set(sid, setTimeout(() => this.destroy(sid), maxAge));
            }
            try {
                this.sessions.set(sid, JSON.stringify(session));
            } catch (err) {
                console.log('Set session error:', err);
            }
 
            return sid;
        }
 
        destroy(sid: string) {
            this.sessions.delete(sid);
            this.__timer.delete(sid);
        }
    }
    KoaApp.keys = ['koa2', 'socketio', 'koa-session2'];
    const sessionOpt = {
        store: new Store(),
        key: '4lKSd^Qma*3'
    };
    KoaApp.use(KoaSession2(sessionOpt));
    let mock1 = sinon.mock(KoaApp);
    mock1.expects('createContext').once();
    const ioMiddleware = HandleKoaSession2(KoaApp, sessionOpt.store, sessionOpt.key);
    let socket = {
        request: {},
        reponse: {},
        handshake: {
            headers: {
                upgrade: 'websocket',
                connection: 'upgrade',
                host: '127.0.0.1:3000',
                pragma: 'no-cache',
                'cache-control': 'no-cache',
                origin: 'http://localhost:8080',
                'sec-websocket-version': '13',
                'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
                'accept-encoding': 'gzip, deflate, br',
                'accept-language': 'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4',
                cookie: 'true=U3m-ZiUIGqU0JcLEAAAF; 4lKSd^Qma*3=twsVN6rKPz5CJhra9pOn8nczyFww_SN5; 4lKSd^Qma*3.sig=Lnv3NfZtqXArCKbqEM0oucyuMe0; koa:sess=0N1Gwg6wHCmm8bjomEm-PQorSGrFaDQN; koa:sess.sig=DyP5wWdGPFKrHeSWzBoo8UcO4n8',
                'sec-websocket-key': 'fH5WKbyXMpF5oVPwD9865g==',
                'sec-websocket-extensions': 'permessage-deflate; client_max_window_bits'
            }
        }
    };
    let md = ioMiddleware(socket, () => {});
    mock1.verify();
 
    t.pass();
});