Skip to content

[Swift Testing] Make it easy to use HTTPServer from Swift#63391

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
rr-codes:eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift
Apr 24, 2026
Merged

[Swift Testing] Make it easy to use HTTPServer from Swift#63391
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
rr-codes:eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift

Conversation

@rr-codes
Copy link
Copy Markdown
Contributor

@rr-codes rr-codes commented Apr 23, 2026

bb3490f

[Swift Testing] Make it easy to use HTTPServer from Swift
https://bugs.webkit.org/show_bug.cgi?id=313093
rdar://175390188

Reviewed by Aditya Keerthi and Abrar Rahman Protyasha.

Design and implement a Swift overlay of HTTPServer to make it so much nicer and easier to use from Swift.

* Tools/TestWebKitAPI/Helpers/cocoa/HTTPServer.h:
(TestWebKitAPI::HTTPServer::HTTPServer):
* Tools/TestWebKitAPI/Helpers/cocoa/HTTPServer.mm:
(TestWebKitAPI::HTTPServer::RequestData::RequestData):
(TestWebKitAPI::HTTPServer::startListening):
(TestWebKitAPI::HTTPServer::cancel):
(TestWebKitAPI::HTTPServer::HTTPServer):
(TestWebKitAPI::m_protocol):
(hashMapSet):
(TestWebKitAPI::startListening): Deleted.

- Swift is unable to create an `std::initializer_list`, so add a `HashMap` overload
for the constructor.

- The `cancel` and `startListening` functions were masquarading as sync functions while
in actuality they are async; change them to reflect their true async nature.

- Change the Swift name of the Cxx HTTPServer so that Swift clients find the Swift
version instead of the Cxx version.

- Work around a limitation of Swift-Cxx interop with templated types by adding a helper
setter function for a hashmap.

* Tools/TestWebKitAPI/Helpers/cocoa/HTTPServer.swift: Added.
(RouteBuilder.buildBlock(_:)):
(HTTPServer.storage):

Implement the Swift HTTPServer.

* Tools/TestWebKitAPI/Helpers/cocoa/StdLibExtras.swift: Added.
(WTF.description):

Temporarily duplicate some code to make it easy to convert between WTF String and Swift String.

* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:

Add the new file.

* Tools/TestWebKitAPI/Tests/WebKit/WKWebView/AppPrivacyReport.mm:
* Tools/TestWebKitAPI/Tests/WebKit/WKWebView/Download.mm:
(TestWebKitAPI::ResumeCantReconnect)):
* Tools/TestWebKitAPI/Tests/WebKit/WKWebView/ServiceWorkerBasic.mm:
((ServiceWorkers, ChangeOfServerCertificate)):

Adjust some tests to accurately use the async cancel and startListening functions.

* Tools/TestWebKitAPI/Tests/WebKit/WebPage/AppKitGesturesTests.swift:
* Tools/TestWebKitAPI/Tests/WebKit/WebPage/URLSchemeHandlerTests.swift:
* Tools/TestWebKitAPI/Tests/WebKit/WebPage/WebPageNavigationTests.swift:

Disambiguate the `Task` type.

Canonical link: https://commits.webkit.org/311958@main

87df6dd

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows Apple Internal
❌ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win ⏳ 🛠 ios-apple
✅ 🧪 bindings ✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests ⏳ 🛠 mac-apple
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe ⏳ 🛠 vision-apple
✅ 🧪 ios-wk2-wpt ✅ 🧪 api-mac-debug ✅ 🛠 gtk3-libwebrtc
✅ 🧪 api-ios ✅ 🧪 mac-wk1 ✅ 🛠 gtk
✅ 🧪 mac-wk2 ✅ 🧪 gtk-wk2
✅ 🛠 vision ✅ 🧪 mac-AS-debug-wk2 ✅ 🧪 api-gtk
✅ 🛠 🧪 merge ✅ 🛠 vision-sim ✅ 🧪 mac-wk2-stress ✅ 🛠 playstation
✅ 🧪 vision-wk2 ✅ 🧪 mac-intel-wk2
✅ 🛠 tv
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@rr-codes rr-codes self-assigned this Apr 23, 2026
@rr-codes rr-codes added the WebKit Misc. For miscellaneous bugs in the WebKit framework (and not JavaScriptCore or WebCore). label Apr 23, 2026
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes removed the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from 2a35830 to 50fc6f9 Compare April 23, 2026 03:49
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes removed the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from 50fc6f9 to 2675ab2 Compare April 23, 2026 03:56
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from 2675ab2 to 23b4dff Compare April 23, 2026 04:00
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes removed the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes changed the title [Swift Testing] Make it easy to use HTTPServer from Swift Revert "Speed up check-webkit-style (and git webkit pr)" Apr 23, 2026
@rr-codes rr-codes changed the title Revert "Speed up check-webkit-style (and git webkit pr)" Revert "a Style checker should not suggest using swift format to fix 'unsafe' warnings" Apr 23, 2026
@rr-codes rr-codes changed the title Revert "a Style checker should not suggest using swift format to fix 'unsafe' warnings" [Swift Testing] Make it easy to use HTTPServer from Swift Apr 23, 2026
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes removed the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from cbc7059 to 72abd4d Compare April 23, 2026 05:09
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes removed the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from 72abd4d to 23b4dff Compare April 23, 2026 05:31
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from cfe89d7 to 778781c Compare April 23, 2026 20:56
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from 778781c to 1470c37 Compare April 23, 2026 20:59
Comment thread Tools/TestWebKitAPI/Helpers/cocoa/HTTPServer.mm
Comment thread Tools/TestWebKitAPI/Helpers/cocoa/HTTPServer.h
Comment on lines +66 to +67
HTTPServer(std::initializer_list<std::pair<String, HTTPResponse>>, Protocol = Protocol::Http, CertificateVerifier&& = nullptr, SecIdentityRef = nullptr, std::optional<uint16_t> port = { }, bool startListeningImmediately = true);
HTTPServer(ResponseMap&& responses, Protocol = Protocol::Http, CertificateVerifier&& = nullptr, SecIdentityRef = nullptr, std::optional<uint16_t> port = { }, bool startListeningImmediately = true);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have a sync cancel() helper, we don't need this forked constructor logic.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also now make startListening() private

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and you can drop this section from Swift run? Since the server is already listening from init?

        await withCheckedContinuation { continuation in
            unsafe self.storage.startListening(
                consuming: .init(
                    {
                        continuation.resume()
                    },
                    WTF.ThreadLikeAssertion(WTF.CurrentThreadLike())
                )
            )
        }

@@ -205,4 +213,6 @@ RetainPtr<SecIdentityRef> testIdentity();
RetainPtr<SecIdentityRef> testIdentity2();
void verifyCertificateAndPublicKey(SecTrustRef);

void hashMapSet(HashMap<WTF::String, TestWebKitAPI::HTTPResponse>&, WTF::String&&, TestWebKitAPI::HTTPResponse&&);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two things: (1) does this need to be in a global namespace? (2) this feels like a general utility that could be housed in something like CxxInteropHelpers.h?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose this deliberately because it is not general enough to go in a different file since it uses the specific types defined here, but it is general enough that it is unrelated to the core of TestWebKitAPI itself. Plus it’s temporary anyways so 🤷

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, temporary how? I think I missed that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it’s just a swift compiler interop bug atm that it doesn’t work well with HashMap/generic types, so in the future this won’t be needed

Comment on lines +66 to +67
HTTPServer(std::initializer_list<std::pair<String, HTTPResponse>>, Protocol = Protocol::Http, CertificateVerifier&& = nullptr, SecIdentityRef = nullptr, std::optional<uint16_t> port = { }, bool startListeningImmediately = true);
HTTPServer(ResponseMap&& responses, Protocol = Protocol::Http, CertificateVerifier&& = nullptr, SecIdentityRef = nullptr, std::optional<uint16_t> port = { }, bool startListeningImmediately = true);
Copy link
Copy Markdown
Member

@aprotyas aprotyas Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making this an enum param? Makes it clearer from call sites

Suggested change
HTTPServer(std::initializer_list<std::pair<String, HTTPResponse>>, Protocol = Protocol::Http, CertificateVerifier&& = nullptr, SecIdentityRef = nullptr, std::optional<uint16_t> port = { }, bool startListeningImmediately = true);
HTTPServer(ResponseMap&& responses, Protocol = Protocol::Http, CertificateVerifier&& = nullptr, SecIdentityRef = nullptr, std::optional<uint16_t> port = { }, bool startListeningImmediately = true);
enum class DeferListening : bool { No, Yes };
HTTPServer(std::initializer_list<std::pair<String, HTTPResponse>>, Protocol = Protocol::Http, CertificateVerifier&& = nullptr, SecIdentityRef = nullptr, std::optional<uint16_t> port = { }, DeferListening = DeferListening::No);
HTTPServer(ResponseMap&& responses, Protocol = Protocol::Http, CertificateVerifier&& = nullptr, SecIdentityRef = nullptr, std::optional<uint16_t> port = { }, DeferListening = DeferListening::No);

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Apr 23, 2026
@rr-codes rr-codes removed the merging-blocked Applied to prevent a change from being merged label Apr 24, 2026
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from 1470c37 to 87df6dd Compare April 24, 2026 01:03
@rr-codes rr-codes requested review from aprotyas and pxlcoder April 24, 2026 01:05
@rr-codes rr-codes added the merge-queue Applied to send a pull request to merge-queue label Apr 24, 2026
https://bugs.webkit.org/show_bug.cgi?id=313093
rdar://175390188

Reviewed by Aditya Keerthi and Abrar Rahman Protyasha.

Design and implement a Swift overlay of HTTPServer to make it so much nicer and easier to use from Swift.

* Tools/TestWebKitAPI/Helpers/cocoa/HTTPServer.h:
(TestWebKitAPI::HTTPServer::HTTPServer):
* Tools/TestWebKitAPI/Helpers/cocoa/HTTPServer.mm:
(TestWebKitAPI::HTTPServer::RequestData::RequestData):
(TestWebKitAPI::HTTPServer::startListening):
(TestWebKitAPI::HTTPServer::cancel):
(TestWebKitAPI::HTTPServer::HTTPServer):
(TestWebKitAPI::m_protocol):
(hashMapSet):
(TestWebKitAPI::startListening): Deleted.

- Swift is unable to create an `std::initializer_list`, so add a `HashMap` overload
for the constructor.

- The `cancel` and `startListening` functions were masquarading as sync functions while
in actuality they are async; change them to reflect their true async nature.

- Change the Swift name of the Cxx HTTPServer so that Swift clients find the Swift
version instead of the Cxx version.

- Work around a limitation of Swift-Cxx interop with templated types by adding a helper
setter function for a hashmap.

* Tools/TestWebKitAPI/Helpers/cocoa/HTTPServer.swift: Added.
(RouteBuilder.buildBlock(_:)):
(HTTPServer.storage):

Implement the Swift HTTPServer.

* Tools/TestWebKitAPI/Helpers/cocoa/StdLibExtras.swift: Added.
(WTF.description):

Temporarily duplicate some code to make it easy to convert between WTF String and Swift String.

* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:

Add the new file.

* Tools/TestWebKitAPI/Tests/WebKit/WKWebView/AppPrivacyReport.mm:
* Tools/TestWebKitAPI/Tests/WebKit/WKWebView/Download.mm:
(TestWebKitAPI::ResumeCantReconnect)):
* Tools/TestWebKitAPI/Tests/WebKit/WKWebView/ServiceWorkerBasic.mm:
((ServiceWorkers, ChangeOfServerCertificate)):

Adjust some tests to accurately use the async cancel and startListening functions.

* Tools/TestWebKitAPI/Tests/WebKit/WebPage/AppKitGesturesTests.swift:
* Tools/TestWebKitAPI/Tests/WebKit/WebPage/URLSchemeHandlerTests.swift:
* Tools/TestWebKitAPI/Tests/WebKit/WebPage/WebPageNavigationTests.swift:

Disambiguate the `Task` type.

Canonical link: https://commits.webkit.org/311958@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/Swift-Testing-Make-it-easy-to-use-HTTPServer-from-Swift branch from 87df6dd to bb3490f Compare April 24, 2026 16:44
@webkit-commit-queue
Copy link
Copy Markdown
Collaborator

Committed 311958@main (bb3490f): https://commits.webkit.org/311958@main

Reviewed commits have been landed. Closing PR #63391 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit bb3490f into WebKit:main Apr 24, 2026
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WebKit Misc. For miscellaneous bugs in the WebKit framework (and not JavaScriptCore or WebCore).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants