-
Notifications
You must be signed in to change notification settings - Fork 6k
Test static route #3297
New issue
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
Merged
Merged
Test static route #3297
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e4672f
Move health route tests to routes directory
code-asher 52cf2fc
Move tmpdir test helper to test helpers file
code-asher 1789cd1
Move temp test dirs under a `tests` sub-directory
code-asher 4925e97
Add static route tests
code-asher ad4a70c
Use warn log level for integration tests
code-asher e8443e2
Fix helpers not working in e2e tests
code-asher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.