From 15531a5214cf6a42e04eb164f950a8a6cd6ea104 Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Fri, 8 Sep 2023 11:28:15 +0530 Subject: [PATCH] fix: pull TestServer into a separate file --- server/test/TestServer.ts | 84 +++++++++++++++++++++++++++++++++++++++ server/test/support.ts | 83 +------------------------------------- 2 files changed, 85 insertions(+), 82 deletions(-) create mode 100644 server/test/TestServer.ts diff --git a/server/test/TestServer.ts b/server/test/TestServer.ts new file mode 100644 index 0000000000..1a2df84b21 --- /dev/null +++ b/server/test/TestServer.ts @@ -0,0 +1,84 @@ +import http from "http"; +import { AddressInfo } from "net"; +import Koa from "koa"; +// eslint-disable-next-line no-restricted-imports +import nodeFetch from "node-fetch"; + +class TestServer { + private server: http.Server; + private listener?: Promise | null; + + constructor(app: Koa) { + this.server = http.createServer(app.callback() as any); + } + + get address(): string { + const { port } = this.server.address() as AddressInfo; + return `http://localhost:${port}`; + } + + listen() { + if (!this.listener) { + this.listener = new Promise((resolve, reject) => { + this.server + .listen(0, () => resolve()) + .on("error", (err) => reject(err)); + }); + } + + return this.listener; + } + + fetch(path: string, opts: any) { + return this.listen().then(() => { + const url = `${this.address}${path}`; + const options = Object.assign({ headers: {} }, opts); + const contentType = + options.headers["Content-Type"] ?? options.headers["content-type"]; + // automatic JSON encoding + if (!contentType && typeof options.body === "object") { + options.headers["Content-Type"] = "application/json"; + options.body = JSON.stringify(options.body); + } + + return nodeFetch(url, options); + }); + } + + close() { + this.listener = null; + return new Promise((resolve, reject) => { + this.server.close((err) => (err ? reject(err) : resolve())); + }); + } + + delete(path: string, options?: any) { + return this.fetch(path, { ...options, method: "DELETE" }); + } + + get(path: string, options?: any) { + return this.fetch(path, { ...options, method: "GET" }); + } + + head(path: string, options?: any) { + return this.fetch(path, { ...options, method: "HEAD" }); + } + + options(path: string, options?: any) { + return this.fetch(path, { ...options, method: "OPTIONS" }); + } + + patch(path: string, options?: any) { + return this.fetch(path, { ...options, method: "PATCH" }); + } + + post(path: string, options?: any) { + return this.fetch(path, { ...options, method: "POST" }); + } + + put(path: string, options?: any) { + return this.fetch(path, { ...options, method: "PUT" }); + } +} + +export default TestServer; diff --git a/server/test/support.ts b/server/test/support.ts index abd22cfbe8..e750986a76 100644 --- a/server/test/support.ts +++ b/server/test/support.ts @@ -1,8 +1,3 @@ -import http from "http"; -import { AddressInfo } from "net"; -import Koa from "koa"; -// eslint-disable-next-line no-restricted-imports -import nodeFetch from "node-fetch"; import { WhereOptions } from "sequelize"; import sharedEnv from "@shared/env"; import env from "@server/env"; @@ -10,83 +5,7 @@ import { Event, Team } from "@server/models"; import onerror from "@server/onerror"; import webService from "@server/services/web"; import { sequelize } from "@server/storage/database"; - -class TestServer { - private server: http.Server; - private listener?: Promise | null; - - constructor(app: Koa) { - this.server = http.createServer(app.callback() as any); - } - - get address(): string { - const { port } = this.server.address() as AddressInfo; - return `http://localhost:${port}`; - } - - listen() { - if (!this.listener) { - this.listener = new Promise((resolve, reject) => { - this.server - .listen(0, () => resolve()) - .on("error", (err) => reject(err)); - }); - } - - return this.listener; - } - - fetch(path: string, opts: any) { - return this.listen().then(() => { - const url = `${this.address}${path}`; - const options = Object.assign({ headers: {} }, opts); - const contentType = - options.headers["Content-Type"] ?? options.headers["content-type"]; - // automatic JSON encoding - if (!contentType && typeof options.body === "object") { - options.headers["Content-Type"] = "application/json"; - options.body = JSON.stringify(options.body); - } - - return nodeFetch(url, options); - }); - } - - close() { - this.listener = null; - return new Promise((resolve, reject) => { - this.server.close((err) => (err ? reject(err) : resolve())); - }); - } - - delete(path: string, options?: any) { - return this.fetch(path, { ...options, method: "DELETE" }); - } - - get(path: string, options?: any) { - return this.fetch(path, { ...options, method: "GET" }); - } - - head(path: string, options?: any) { - return this.fetch(path, { ...options, method: "HEAD" }); - } - - options(path: string, options?: any) { - return this.fetch(path, { ...options, method: "OPTIONS" }); - } - - patch(path: string, options?: any) { - return this.fetch(path, { ...options, method: "PATCH" }); - } - - post(path: string, options?: any) { - return this.fetch(path, { ...options, method: "POST" }); - } - - put(path: string, options?: any) { - return this.fetch(path, { ...options, method: "PUT" }); - } -} +import TestServer from "./TestServer"; export function getTestServer() { const app = webService();