UNPKG

@icebro/actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

88 lines (79 loc) 2.52 kB
import * as request from "request-promise-native"; import { Process, config } from "./../../../src/index"; const actionhero = new Process(); let url: string; jest.mock("./../../../src/config/web.ts", () => ({ __esModule: true, test: { web: () => { return { enabled: true, secure: false, automaticRoutes: ["get"], allowedRequestHosts: ["https://www.site.com"], urlPathForActions: "api", urlPathForFiles: "public", rootEndpointType: "file", port: 18080 + parseInt(process.env.JEST_WORKER_ID || "0"), matchExtensionMime: true, metadataOptions: { serverInformation: true, requesterInformation: false, }, fingerprintOptions: { cookieKey: "sessionID", }, }; }, }, })); describe("Server: Web", () => { beforeAll(async () => { await actionhero.start(); url = "http://localhost:" + config.web.port; }); afterAll(async () => await actionhero.stop()); describe("request redirection (allowedRequestHosts)", () => { test("will redirect clients if they do not request the proper host", async () => { try { await request.get({ followRedirect: false, url: url + "/api/randomNumber", headers: { Host: "lalala.site.com" }, }); throw new Error("should not get here"); } catch (error) { expect(error.statusCode).toEqual(302); expect(error.response.body).toMatch( /You are being redirected to https:\/\/www.site.com\/api\/randomNumber/ ); } }); test("will redirect clients if they do not request the proper protocol", async () => { try { await request.get({ followRedirect: false, url: url + "/api/randomNumber", headers: { Host: "www.site.com" }, }); throw new Error("should not get here"); } catch (error) { expect(error.statusCode).toEqual(302); expect(error.response.body).toMatch( /You are being redirected to https:\/\/www.site.com\/api\/randomNumber/ ); } }); test("will allow API access from the proper hosts", async () => { const response = await request.get({ followRedirect: false, url: url + "/api/randomNumber", headers: { Host: "www.site.com", "x-forwarded-proto": "https", }, }); expect(response).toMatch(/randomNumber/); }); }); });