-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: James Culveyhouse <jculveyhouse@cloudflare.com>
- Loading branch information
1 parent
321c7ed
commit a14bd1d
Showing
7 changed files
with
247 additions
and
135 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
feature: add a `cf` field to the `getBindingsProxy` result | ||
|
||
Add a new `cf` field to the `getBindingsProxy` result that people can use to mock the production | ||
`cf` (`IncomingRequestCfProperties`) object. | ||
|
||
Example: | ||
|
||
```ts | ||
const { cf } = await getBindingsProxy(); | ||
|
||
console.log(`country = ${cf.country}; colo = ${cf.colo}`); // logs 'country = GB ; colo = LHR' | ||
``` |
43 changes: 43 additions & 0 deletions
43
fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts
This file contains 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,43 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { getBindingsProxy } from "./shared"; | ||
|
||
describe("getBindingsProxy - cf", () => { | ||
it("should provide mock data", async () => { | ||
const { cf, dispose } = await getBindingsProxy(); | ||
try { | ||
expect(cf).toMatchObject({ | ||
colo: "DFW", | ||
city: "Austin", | ||
regionCode: "TX", | ||
}); | ||
} finally { | ||
await dispose(); | ||
} | ||
}); | ||
|
||
it("should match the production runtime cf object", async () => { | ||
const { cf, dispose } = await getBindingsProxy(); | ||
try { | ||
expect(cf.constructor.name).toBe("Object"); | ||
|
||
expect(() => { | ||
cf.city = "test city"; | ||
}).toThrowError( | ||
"Cannot assign to read only property 'city' of object '#<Object>'" | ||
); | ||
expect(cf.city).not.toBe("test city"); | ||
|
||
expect(() => { | ||
cf.newField = "test new field"; | ||
}).toThrowError("Cannot add property newField, object is not extensible"); | ||
expect("newField" in cf).toBe(false); | ||
|
||
expect(cf.botManagement).toMatchObject({ | ||
score: 99, | ||
}); | ||
expect(Object.isFrozen(cf.botManagement)).toBe(true); | ||
} finally { | ||
await dispose(); | ||
} | ||
}); | ||
}); |
This file contains 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 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 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 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
Oops, something went wrong.