UNPKG

@webda/core

Version:

Expose API with Lambda

122 lines 5.88 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 * as fs from "fs"; import { WebdaTest } from "../test.js"; import { ResourceServiceParameters } from "./resource.js"; let ResourceTest = class ResourceTest extends WebdaTest { async before(init = true) { await super.before(init); this.resource = this.getService("ResourceService"); this.resourceModel = this.getService("ModelsResource"); this.ctx = await this.newContext(); } async parentFolder() { let executor = this.getExecutor(this.ctx, "test.webda.io", "GET", "/resources/../config.json"); assert.notStrictEqual(executor, undefined); await assert.rejects(() => executor.execute(this.ctx), (err) => err.getResponseCode() === 401); } params() { let params = new ResourceServiceParameters({ url: "/test/", rootRedirect: true }); assert.strictEqual(params.url, "/test/"); assert.strictEqual(params.folder, "./test/"); } async redirect() { this.resource.getParameters().rootRedirect = true; this.webda.getRouter().removeRoute("/"); assert.ok(this.webda.getRouter().getRouteFromUrl(this.ctx, "GET", "/") === undefined); this.resource.initRoutes(); assert.ok(this.webda.getRouter().getRouteFromUrl(this.ctx, "GET", "/") !== undefined); this.resource._redirect(this.ctx); assert.strictEqual(this.ctx.getResponseHeaders().Location, "http://test.webda.io/resources/"); } async index() { let executor = this.getExecutor(this.ctx, "test.webda.io", "GET", "/resources/"); // index.html does not exist in our case await assert.rejects(() => executor.execute(this.ctx), (err) => err.getResponseCode() === 404); } async unknownFile() { this.getService("ResourceService").getParameters().indexFallback = true; let executor = this.getExecutor(this.ctx, "test.webda.io", "GET", "/resources/config.unknown.json"); assert.notStrictEqual(executor, undefined); await assert.rejects(() => executor.execute(this.ctx), (err) => err.getResponseCode() === 404); } async jsonFile() { let executor = this.getExecutor(this.ctx, "test.webda.io", "GET", "/resources/config.json"); assert.notStrictEqual(executor, undefined); await executor.execute(this.ctx); assert.strictEqual(this.ctx.getResponseBody().toString(), fs.readFileSync("./test/config.json").toString()); assert.strictEqual(this.ctx.getResponseHeaders()["content-type"], "application/json"); } async jsFile() { let executor = this.getExecutor(this.ctx, "test.webda.io", "GET", "/resources/moddas/voidstore.js"); assert.notStrictEqual(executor, undefined); await executor.execute(this.ctx); assert.strictEqual(this.ctx.getResponseBody().toString(), fs.readFileSync("./test/moddas/voidstore.js").toString()); assert.strictEqual(this.ctx.getResponseHeaders()["content-type"], "application/javascript"); } async textFile() { let executor = this.getExecutor(this.ctx, "test.webda.io", "GET", "/resources/data/test.txt"); assert.notStrictEqual(executor, undefined); await executor.execute(this.ctx); assert.strictEqual(this.ctx.getResponseBody().toString(), fs.readFileSync("./test/data/test.txt").toString()); assert.strictEqual(this.ctx.getResponseHeaders()["content-type"], "text/plain; charset=UTF-8"); } async pngFile() { let executor = this.getExecutor(this.ctx, "test.webda.io", "GET", "/resources/data/test.png"); assert.notStrictEqual(executor, undefined); await executor.execute(this.ctx); assert.strictEqual(this.ctx.getResponseBody().toString(), fs.readFileSync("./test/data/test.png").toString()); assert.strictEqual(this.ctx.getResponseHeaders()["content-type"], "image/png"); } // Check Store HTTP mapping async testStoreHttpMapping() { let executor = this.getExecutor(this.ctx, "test.webda.io", "GET", "/templates/PASSPORT_EMAIL_RECOVERY/html.mustache"); assert.notStrictEqual(executor, undefined); await executor.execute(this.ctx); assert.strictEqual(this.ctx.getResponseBody().toString(), fs.readFileSync("./templates/PASSPORT_EMAIL_RECOVERY/html.mustache").toString()); assert.strictEqual(this.ctx.getResponseHeaders()["content-type"], "application/octet-stream"); } }; __decorate([ test ], ResourceTest.prototype, "parentFolder", null); __decorate([ test ], ResourceTest.prototype, "params", null); __decorate([ test ], ResourceTest.prototype, "redirect", null); __decorate([ test ], ResourceTest.prototype, "index", null); __decorate([ test ], ResourceTest.prototype, "unknownFile", null); __decorate([ test ], ResourceTest.prototype, "jsonFile", null); __decorate([ test ], ResourceTest.prototype, "jsFile", null); __decorate([ test ], ResourceTest.prototype, "textFile", null); __decorate([ test ], ResourceTest.prototype, "pngFile", null); __decorate([ test ], ResourceTest.prototype, "testStoreHttpMapping", null); ResourceTest = __decorate([ suite ], ResourceTest); //# sourceMappingURL=resource.spec.js.map