File
Blob: src/rust/kj/tests/ffi-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 <workerd/rust/kj/http.rs.h> |
| 6 | #include <workerd/rust/kj/tests/lib.rs.h> |
| 7 | |
| 8 | #include <kj-rs/kj-rs.h> |
| 9 | |
| 10 | #include <kj/test.h> |
| 11 | |
| 12 | #include <cmath> |
| 13 | |
| 14 | using namespace kj_rs; |
| 15 | |
| 16 | static kj::StringPtr tlsHost; |
| 17 | |
| 18 | kj::Promise<void> startTls(kj::StringPtr hostName) { |
| 19 | tlsHost = hostName; |
| 20 | return kj::READY_NOW; |
| 21 | } |
| 22 | |
| 23 | class MockHttpService: public kj::HttpService { |
| 24 | public: |
| 25 | virtual ~MockHttpService() {} |
| 26 | kj::Promise<void> request(kj::HttpMethod method, |
| 27 | kj::StringPtr url, |
| 28 | const kj::HttpHeaders& headers, |
| 29 | kj::AsyncInputStream& requestBody, |
| 30 | kj::HttpService::Response& response) override { |
| 31 | KJ_UNIMPLEMENTED("not exercised by test"); |
| 32 | } |
| 33 | |
| 34 | kj::Promise<void> connect(kj::StringPtr host, |
| 35 | const kj::HttpHeaders& headers, |
| 36 | kj::AsyncIoStream& connection, |
| 37 | ConnectResponse& response, |
| 38 | kj::HttpConnectSettings settings) override { |
| 39 | if (settings.useTls) { |
| 40 | return kj::READY_NOW; |
| 41 | } |
| 42 | KJ_IF_SOME(tlsStarter, settings.tlsStarter) { |
| 43 | tlsStarter = startTls; |
| 44 | } |
| 45 | return kj::READY_NOW; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | class TestConnectResponse: public kj::HttpService::ConnectResponse { |
| 50 | public: |
| 51 | void accept(uint statusCode, kj::StringPtr statusText, const kj::HttpHeaders& headers) override { |
| 52 | KJ_UNIMPLEMENTED("not exercised by test"); |
| 53 | } |
| 54 | kj::Own<kj::AsyncOutputStream> reject(uint statusCode, |
| 55 | kj::StringPtr statusText, |
| 56 | const kj::HttpHeaders& headers, |
| 57 | kj::Maybe<uint64_t> expectedBodySize = kj::none) override { |
| 58 | KJ_UNIMPLEMENTED("not exercised by test"); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | KJ_TEST("get header by id round-trip through Rust") { |
| 63 | // Register a custom header and a second one we'll leave unset. |
| 64 | kj::HttpHeaderTable::Builder builder; |
| 65 | auto customId = builder.add("X-Custom-Header"); |
| 66 | auto absentId = builder.add("X-Absent-Header"); |
| 67 | auto table = builder.build(); |
| 68 | |
| 69 | kj::HttpHeaders headers(*table); |
| 70 | headers.setPtr(customId, "hello-from-cpp"); |
| 71 | |
| 72 | // Also set a builtin header to test that path. |
| 73 | headers.setPtr(kj::HttpHeaderId::HOST, "example.com"); |
| 74 | |
| 75 | // Round-trip: C++ -> Rust (get_header_value_via_id) -> C++ (get_header_by_id shim) -> value. |
| 76 | { |
| 77 | auto maybe = kj::rust::tests::get_header_value_via_id(headers, customId); |
| 78 | KJ_IF_SOME(value, maybe) { |
| 79 | auto strValue = kj::StringPtr(reinterpret_cast<const char*>(value.data()), value.size()); |
| 80 | KJ_EXPECT(strValue == "hello-from-cpp", strValue); |
| 81 | } else { |
| 82 | KJ_FAIL_EXPECT("expected Some for custom header, got None"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Absent header should return None. |
| 87 | { |
| 88 | auto maybe = kj::rust::tests::get_header_value_via_id(headers, absentId); |
| 89 | KJ_EXPECT(maybe == kj::none, "expected None for absent header"); |
| 90 | } |
| 91 | |
| 92 | // Builtin header via HttpHeaderId. |
| 93 | { |
| 94 | auto maybe = kj::rust::tests::get_header_value_via_id(headers, kj::HttpHeaderId::HOST); |
| 95 | KJ_IF_SOME(value, maybe) { |
| 96 | auto strValue = kj::StringPtr(reinterpret_cast<const char*>(value.data()), value.size()); |
| 97 | KJ_EXPECT(strValue == "example.com", strValue); |
| 98 | } else { |
| 99 | KJ_FAIL_EXPECT("expected Some for HOST header, got None"); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | KJ_TEST("assert header ids present via ArrayPtr round-trip through Rust") { |
| 105 | kj::HttpHeaderTable::Builder builder; |
| 106 | auto custom1 = builder.add("X-First"); |
| 107 | auto custom2 = builder.add("X-Second"); |
| 108 | auto table = builder.build(); |
| 109 | |
| 110 | kj::HttpHeaders headers(*table); |
| 111 | headers.setPtr(custom1, "value1"); |
| 112 | headers.setPtr(custom2, "value2"); |
| 113 | headers.setPtr(kj::HttpHeaderId::HOST, "example.com"); |
| 114 | |
| 115 | // Build an array of pointers from our HttpHeaderIds, simulating what you'd get from |
| 116 | // kj::ArrayPtr<const kj::HttpHeaderId> — CXX can't pass opaque types in slices directly, |
| 117 | // so we pass pointers instead. |
| 118 | const kj::HttpHeaderId* const idPtrs[] = {&custom1, &custom2, &kj::HttpHeaderId::HOST}; |
| 119 | rust::Slice<const kj::HttpHeaderId* const> idSlice(idPtrs, 3); |
| 120 | |
| 121 | // The Rust side wraps each pointer in HttpHeaderIdRef and asserts all are present. |
| 122 | kj::rust::tests::assert_header_ids_present(headers, idSlice); |
| 123 | } |
| 124 | |
| 125 | KJ_TEST("http connect settings") { |
| 126 | kj::EventLoop loop; |
| 127 | kj::WaitScope waitScope(loop); |
| 128 | |
| 129 | auto mock = kj::heap<MockHttpService>(); |
| 130 | auto mockRef = *mock; |
| 131 | |
| 132 | auto proxy = kj::rust::tests::new_proxy_http_service(kj::mv(mock)); |
| 133 | |
| 134 | kj::StringPtr host = "example.com"; |
| 135 | kj::HttpHeaderTable headerTable; |
| 136 | kj::HttpHeaders headers(headerTable); |
| 137 | |
| 138 | auto pipe = kj::newTwoWayPipe(); |
| 139 | kj::AsyncIoStream& connection = *pipe.ends[0]; |
| 140 | |
| 141 | TestConnectResponse tunnel; |
| 142 | |
| 143 | kj::Own<kj::TlsStarterCallback> tlsStarter = kj::heap<kj::TlsStarterCallback>(); |
| 144 | ::kj::rust::HttpConnectSettings settings = {.use_tls = false, .tls_starter = kj::none}; |
| 145 | settings.tls_starter = tlsStarter; |
| 146 | |
| 147 | auto promise = proxy->connect(host.asBytes().as<Rust>(), headers, connection, tunnel, settings); |
| 148 | KJ_ASSERT_NONNULL (*tlsStarter)(host).wait(waitScope); |
| 149 | KJ_EXPECT(tlsHost == host); |
| 150 | } |