| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { promises as fs } from "fs" | ||
| import { tmpdir } from "../../test/utils/helpers" | ||
|
|
||
| /** | ||
| * This file is for testing test helpers (not core code). | ||
| */ | ||
| describe("test helpers", () => { | ||
| it("should return a temp directory", async () => { | ||
| const testName = "temp-dir" | ||
| const pathToTempDir = await tmpdir(testName) | ||
| expect(pathToTempDir).toContain(testName) | ||
| expect(fs.access(pathToTempDir)).resolves.toStrictEqual(undefined) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { promises as fs } from "fs" | ||
| import * as path from "path" | ||
| import { tmpdir } from "../../utils/helpers" | ||
| import * as httpserver from "../../utils/httpserver" | ||
| import * as integration from "../../utils/integration" | ||
|
|
||
| describe("/static", () => { | ||
| let _codeServer: httpserver.HttpServer | undefined | ||
| function codeServer(): httpserver.HttpServer { | ||
| if (!_codeServer) { | ||
| throw new Error("tried to use code-server before setting it up") | ||
| } | ||
| return _codeServer | ||
| } | ||
|
|
||
| let testFile: string | undefined | ||
| let testFileContent: string | undefined | ||
| let nonExistentTestFile: string | undefined | ||
|
|
||
| // The static endpoint expects a commit and then the full path of the file. | ||
| // The commit is just for cache busting so we can use anything we want. `-` | ||
| // and `development` are specially recognized in that they will cause the | ||
| // static endpoint to avoid sending cache headers. | ||
| const commit = "-" | ||
|
|
||
| beforeAll(async () => { | ||
| const testDir = await tmpdir("static") | ||
| testFile = path.join(testDir, "test") | ||
| testFileContent = "static file contents" | ||
| nonExistentTestFile = path.join(testDir, "i-am-not-here") | ||
| await fs.writeFile(testFile, testFileContent) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| if (_codeServer) { | ||
| await _codeServer.close() | ||
| _codeServer = undefined | ||
| } | ||
| }) | ||
|
|
||
| function commonTests() { | ||
| it("should return a 404 when a commit and file are not provided", async () => { | ||
| const resp = await codeServer().fetch("/static") | ||
| expect(resp.status).toBe(404) | ||
|
|
||
| const content = await resp.json() | ||
| expect(content).toStrictEqual({ error: "Not Found" }) | ||
| }) | ||
|
|
||
| it("should return a 404 when a file is not provided", async () => { | ||
| const resp = await codeServer().fetch(`/static/${commit}`) | ||
| expect(resp.status).toBe(404) | ||
|
|
||
| const content = await resp.json() | ||
| expect(content).toStrictEqual({ error: "Not Found" }) | ||
| }) | ||
| } | ||
|
|
||
| describe("disabled authentication", () => { | ||
| beforeEach(async () => { | ||
| _codeServer = await integration.setup(["--auth=none"], "") | ||
| }) | ||
|
|
||
| commonTests() | ||
|
|
||
| it("should return a 404 for a nonexistent file", async () => { | ||
| const resp = await codeServer().fetch(`/static/${commit}/${nonExistentTestFile}`) | ||
| expect(resp.status).toBe(404) | ||
|
|
||
| const content = await resp.json() | ||
| expect(content.error).toMatch("ENOENT") | ||
| }) | ||
|
|
||
| it("should return a 200 and file contents for an existent file", async () => { | ||
| const resp = await codeServer().fetch(`/static/${commit}${testFile}`) | ||
| expect(resp.status).toBe(200) | ||
|
|
||
| const content = await resp.text() | ||
| expect(content).toStrictEqual(testFileContent) | ||
| }) | ||
| }) | ||
|
|
||
| describe("enabled authentication", () => { | ||
| // Store whatever might be in here so we can restore it afterward. | ||
| // TODO: We should probably pass this as an argument somehow instead of | ||
| // manipulating the environment. | ||
| const previousEnvPassword = process.env.PASSWORD | ||
|
|
||
| beforeEach(async () => { | ||
| process.env.PASSWORD = "test" | ||
| _codeServer = await integration.setup(["--auth=password"], "") | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| process.env.PASSWORD = previousEnvPassword | ||
| }) | ||
|
|
||
| commonTests() | ||
|
|
||
| describe("inside code-server root", () => { | ||
| it("should return a 404 for a nonexistent file", async () => { | ||
| const resp = await codeServer().fetch(`/static/${commit}/${__filename}-does-not-exist`) | ||
| expect(resp.status).toBe(404) | ||
|
|
||
| const content = await resp.json() | ||
| expect(content.error).toMatch("ENOENT") | ||
| }) | ||
|
|
||
| it("should return a 200 and file contents for an existent file", async () => { | ||
| const resp = await codeServer().fetch(`/static/${commit}${__filename}`) | ||
| expect(resp.status).toBe(200) | ||
|
|
||
| const content = await resp.text() | ||
| expect(content).toStrictEqual(await fs.readFile(__filename, "utf8")) | ||
| }) | ||
| }) | ||
|
|
||
| describe("outside code-server root", () => { | ||
| it("should return a 401 for a nonexistent file", async () => { | ||
| const resp = await codeServer().fetch(`/static/${commit}/${nonExistentTestFile}`) | ||
| expect(resp.status).toBe(401) | ||
|
|
||
| const content = await resp.json() | ||
| expect(content).toStrictEqual({ error: "Unauthorized" }) | ||
| }) | ||
|
|
||
| it("should return a 401 for an existent file", async () => { | ||
| const resp = await codeServer().fetch(`/static/${commit}${testFile}`) | ||
| expect(resp.status).toBe(401) | ||
|
|
||
| const content = await resp.json() | ||
| expect(content).toStrictEqual({ error: "Unauthorized" }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,3 @@ | ||
| import * as fs from "fs" | ||
| import * as os from "os" | ||
| import * as path from "path" | ||
|
|
||
| export const CODE_SERVER_ADDRESS = process.env.CODE_SERVER_ADDRESS || "http://localhost:8080" | ||
| export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab" | ||
| export const STORAGE = process.env.STORAGE || "" | ||
|
|
||
| export async function tmpdir(testName: string): Promise<string> { | ||
| const dir = path.join(os.tmpdir(), "code-server") | ||
| await fs.promises.mkdir(dir, { recursive: true }) | ||
|
|
||
| return await fs.promises.mkdtemp(path.join(dir, `test-${testName}-`), { encoding: "utf8" }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,32 @@ | ||
| export const loggerModule = { | ||
| field: jest.fn(), | ||
| level: 2, | ||
| logger: { | ||
| debug: jest.fn(), | ||
| error: jest.fn(), | ||
| info: jest.fn(), | ||
| trace: jest.fn(), | ||
| warn: jest.fn(), | ||
| }, | ||
| import * as fs from "fs" | ||
| import * as os from "os" | ||
| import * as path from "path" | ||
|
|
||
| /** | ||
| * Return a mock of @coder/logger. | ||
| */ | ||
| export function createLoggerMock() { | ||
| return { | ||
| field: jest.fn(), | ||
| level: 2, | ||
| logger: { | ||
| debug: jest.fn(), | ||
| error: jest.fn(), | ||
| info: jest.fn(), | ||
| trace: jest.fn(), | ||
| warn: jest.fn(), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a uniquely named temporary directory. | ||
| * | ||
| * These directories are placed under a single temporary code-server directory. | ||
| */ | ||
| export async function tmpdir(testName: string): Promise<string> { | ||
| const dir = path.join(os.tmpdir(), "code-server/tests") | ||
| await fs.promises.mkdir(dir, { recursive: true }) | ||
|
|
||
| return await fs.promises.mkdtemp(path.join(dir, `${testName}-`), { encoding: "utf8" }) | ||
| } |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.