-
-
Notifications
You must be signed in to change notification settings - Fork 52
Bump cookie to 1.0.2 and add server-api tests #135
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"import-map-overrides": patch | ||
--- | ||
|
||
Bump cookie to 1.0.2 and add server-api tests |
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 |
---|---|---|
|
@@ -74,7 +74,7 @@ | |
"serve": "^14.2.3" | ||
}, | ||
"dependencies": { | ||
"cookie": "^0.6.0" | ||
"cookie": "^1.0.2" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,168 @@ | ||
import { applyOverrides, getOverridesFromCookies } from "../server/server-api"; | ||
import http from "http"; | ||
import { serialize } from "cookie"; | ||
|
||
describe("applyOverrides", () => { | ||
it("should apply overrides to the import map", () => { | ||
// Arrange | ||
const importMap = { | ||
imports: { | ||
package1: "https://cdn.skypack.dev/package1", | ||
}, | ||
}; | ||
const overrides = { | ||
package1: "https://unpkg.com/package1", | ||
package2: "https://unpkg.com/package2", | ||
}; | ||
|
||
// Act | ||
const result = applyOverrides(importMap, overrides); | ||
|
||
// Assert | ||
expect(importMap.imports.package1).toBe("https://cdn.skypack.dev/package1"); | ||
expect(result.imports.package1).toBe("https://unpkg.com/package1"); | ||
expect(result.imports.package2).toBe("https://unpkg.com/package2"); | ||
expect(result.scopes).toEqual({}); | ||
}); | ||
|
||
it("should handle empty overrides", () => { | ||
// Arrange | ||
const importMap = { | ||
imports: { | ||
package1: "https://cdn.skypack.dev/package1", | ||
}, | ||
}; | ||
const overrides = {}; | ||
|
||
// Act | ||
const result = applyOverrides(importMap, overrides); | ||
|
||
// Assert | ||
expect(result.imports.package1).toBe("https://cdn.skypack.dev/package1"); | ||
expect(result.scopes).toEqual({}); | ||
}); | ||
|
||
it("should handle overrides with scopes", () => { | ||
// Arrange | ||
const importMap = { | ||
imports: { | ||
package1: "https://cdn.skypack.dev/package1", | ||
}, | ||
scopes: { | ||
"https://example.com/": { | ||
package2: "https://cdn.skypack.dev/package2", | ||
}, | ||
}, | ||
}; | ||
const overrides = { | ||
package2: "https://unpkg.com/package2", | ||
}; | ||
|
||
// Act | ||
const result = applyOverrides(importMap, overrides); | ||
|
||
// Assert | ||
expect(result.scopes["https://example.com/"].package2).toBe( | ||
"https://cdn.skypack.dev/package2", | ||
); | ||
}); | ||
}); | ||
|
||
describe("getOverridesFromCookies", () => { | ||
const req = new http.IncomingMessage(); | ||
|
||
beforeEach(() => { | ||
req.headers = {}; | ||
}); | ||
|
||
it("should return an empty object if no cookies are set", () => { | ||
// Act | ||
const result = getOverridesFromCookies(req); | ||
|
||
// Assert | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it("should return overrides from cookies", () => { | ||
// Arrange | ||
req.headers.cookie = serialize( | ||
"import-map-override:package1", | ||
"https://unpkg.com/package1", | ||
); | ||
|
||
// Act | ||
const result = getOverridesFromCookies(req); | ||
|
||
// Assert | ||
expect(result.package1).toBe("https://unpkg.com/package1"); | ||
}); | ||
|
||
it("should handle multiple overrides from cookies", () => { | ||
// Arrange | ||
req.headers.cookie = [ | ||
serialize("import-map-override:package1", "https://unpkg.com/package1"), | ||
serialize("import-map-override:package2", "https://unpkg.com/package2"), | ||
].join("; "); | ||
|
||
// Act | ||
const result = getOverridesFromCookies(req); | ||
|
||
// Assert | ||
expect(result.package1).toBe("https://unpkg.com/package1"); | ||
expect(result.package2).toBe("https://unpkg.com/package2"); | ||
}); | ||
|
||
it("should handle port numbers in cookies", () => { | ||
// Arrange | ||
req.headers.cookie = serialize("import-map-override:package1", "8080"); | ||
|
||
// Act | ||
const result = getOverridesFromCookies( | ||
req, | ||
(port, moduleName) => `http://localhost:${port}/${moduleName}.js`, | ||
); | ||
|
||
// Assert | ||
expect(result.package1).toBe("http://localhost:8080/package1.js"); | ||
}); | ||
|
||
it("should handle protocol-relative URLs in cookies", () => { | ||
// Arrange | ||
req.protocol = "https"; | ||
req.headers.cookie = serialize( | ||
"import-map-override:package1", | ||
"//unpkg.com/package1", | ||
); | ||
|
||
// Act | ||
const result = getOverridesFromCookies(req); | ||
|
||
// Assert | ||
expect(result.package1).toBe("https://unpkg.com/package1"); | ||
}); | ||
|
||
it("should ignore cookies that do not start with the correct prefix", () => { | ||
// Arrange | ||
req.headers.cookie = serialize("some-other-cookie", "some-value"); | ||
|
||
// Act | ||
const result = getOverridesFromCookies(req); | ||
|
||
// Assert | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it("should handle cookies with empty module names", () => { | ||
// Arrange | ||
req.headers.cookie = serialize( | ||
"import-map-override:", | ||
"https://unpkg.com/package1", | ||
); | ||
|
||
// Act | ||
const result = getOverridesFromCookies(req); | ||
|
||
// Assert | ||
expect(result).toEqual({}); | ||
}); | ||
}); |
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.