UNPKG

@webda/core

Version:

Expose API with Lambda

78 lines 4.62 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { suite, test } from "@testdeck/mocha"; import * as assert from "assert"; import { serialize as cookieSerialize } from "cookie"; import { CookieOptions, SecureCookie } from "../index.js"; import { WebdaTest } from "../test.js"; import { SimpleOperationContext } from "./context.js"; import { WebContextMock } from "./context.spec.js"; import { HttpContext } from "./httpcontext.js"; import { Session } from "./session.js"; const SECRET = "Lp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5ENLp4B72FPU5n6q4EpVRGyPFnZp5cgLRPScVWixW52Yq84hD4MmnfVfgxKQ5EN"; let CookieTest = class CookieTest extends WebdaTest { async before() { await super.before(); this._ctx = await this.webda.newWebContext(new HttpContext("test.webda.io", "GET", "/")); this.webda.updateContextWithRoute(this._ctx); } async testBadSecret() { let ctx = await this.newContext(); ctx.getHttpContext().cookies = {}; ctx.getHttpContext().cookies["test"] = "plop"; let session = await SecureCookie.load("test", ctx, undefined); console.log(session); assert.strictEqual(Object.keys(session).length, 0); } async cov() { let session = await SecureCookie.load("test", new WebContextMock(this.webda, undefined, undefined), undefined); assert.strictEqual(Object.keys(session).length, 0); assert.strictEqual(await new SimpleOperationContext(this.webda).setInput(Buffer.from("plop")).getRawInputAsString(), "plop"); let ctx = await this.newContext(); assert.ok(!new CookieOptions({}, ctx.getHttpContext()).secure); ctx.getHttpContext().protocol = "https:"; assert.ok(new CookieOptions({}, ctx.getHttpContext()).secure); let opts = new CookieOptions({ domain: true }, ctx.getHttpContext()); assert.strictEqual(opts.domain, "test.webda.io"); opts = new CookieOptions({}, ctx.getHttpContext()); assert.strictEqual(opts.domain, undefined); opts = new CookieOptions({ domain: "google.com" }, ctx.getHttpContext()); assert.strictEqual(opts.domain, "google.com"); } async testOversize() { var cookie = new Session().getProxy(); cookie.identUsed = "PLOP".repeat(3005); assert.strictEqual(cookie.isDirty(), true); await SecureCookie.save("test", this._ctx, cookie); assert.strictEqual(Object.keys(this._ctx.getResponseCookies()).length, 5); for (let i = 1; i < 5; i++) { const cookie = this._ctx.getResponseCookies()[`test${i > 1 ? i : ""}`]; assert.strictEqual(cookieSerialize(cookie.name, cookie.value, cookie.options || {}).length, 4096); } let ctx = await this.newContext(); let cookies = this._ctx.getResponseCookies(); ctx.getHttpContext().cookies = {}; Object.keys(cookies).forEach(k => { ctx.getHttpContext().cookies[k] = cookies[k].value; }); await ctx.init(true); assert.ok(ctx.getSession().identUsed === "PLOP".repeat(3005)); } }; __decorate([ test("Bad cookie") ], CookieTest.prototype, "testBadSecret", null); __decorate([ test ], CookieTest.prototype, "cov", null); __decorate([ test("Oversize cookie") ], CookieTest.prototype, "testOversize", null); CookieTest = __decorate([ suite ], CookieTest); //# sourceMappingURL=cookie.spec.js.map