File
Blob: src/workerd/api/cf-property-test.c++
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #include "cf-property.h" |
| 6 | |
| 7 | #include <workerd/jsg/jsg.h> |
| 8 | #include <workerd/tests/test-fixture.h> |
| 9 | |
| 10 | namespace workerd::api { |
| 11 | namespace { |
| 12 | |
| 13 | KJ_TEST("Test that CfProperty is frozen by default") { |
| 14 | TestFixture fixture({.mainModuleSource = R"SCRIPT( |
| 15 | export default { |
| 16 | async fetch(request) { |
| 17 | request.cf.foo = 100; |
| 18 | return new Response(`OK`); |
| 19 | }, |
| 20 | }; |
| 21 | )SCRIPT"_kj}); |
| 22 | |
| 23 | try { |
| 24 | auto result = fixture.runRequest(kj::HttpMethod::POST, "http://www.example.com"_kj, "TEST"_kj); |
| 25 | KJ_FAIL_REQUIRE("exception expected"); |
| 26 | } catch (kj::Exception& e) { |
| 27 | KJ_EXPECT(e.getDescription() == |
| 28 | "jsg.TypeError: Cannot add property foo, object is not extensible"_kj); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | KJ_TEST("Test that CfProperty::deepClone returns editable object") { |
| 33 | TestFixture fixture({.mainModuleSource = R"SCRIPT( |
| 34 | export default { |
| 35 | async fetch(request) { |
| 36 | const req = new Request(request); |
| 37 | req.cf.foo = 100; |
| 38 | return new Response(`OK`); |
| 39 | }, |
| 40 | }; |
| 41 | )SCRIPT"_kj}); |
| 42 | auto result = fixture.runRequest(kj::HttpMethod::POST, "http://www.example.com"_kj, "TEST"_kj); |
| 43 | KJ_EXPECT(result.statusCode == 200); |
| 44 | } |
| 45 | |
| 46 | } // namespace |
| 47 | } // namespace workerd::api |