(re)Creating a Ktor DSL
ฝัง
- เผยแพร่เมื่อ 25 เม.ย. 2024
- My mission at the moment is to understand the JetBrains Ktor HTTP library. So last week I wrote some tests for one of the example projects (ktor.io/docs/server-create-ht..., with a view to comparing Ktor with http4k, the server-as-a-function alternative.
To be a fair comparison I want to make sure that I run the same tests against Ktor and http4k. This is something of a problem, as they have a quite different approach to in-memory testing.
This week then I’ll look at reimplementing part of Ktor’s domain specific language to target http4k, so that the same tests can be used to ensure that we implement the same behaviours. On the way we’ll learn how to implement a cheap and cheerful DSL in Kotlin.
In this episode
00:00:52 Reviewing our tests
00:02:13 I have a cunning plan
00:02:38 Substitute our DSL entry point
00:03:03 Change the receiver of the DSL body block
00:04:07 Add the properties we require in the DSL block
00:04:33 Adapt http4k HttpHandler to look like Ktor HttpClient
00:06:04 We can extend assertions to avoid other conversions
00:07:37 Rinse and repeat for other operations
00:08:27 Nesting DSLs
00:09:12 Add another context for the nesting
00:14:14 That turns out to compile
00:14:29 and our test source is unchanged
00:15:33 Review
This video is in a playlist of Ktor episodes ( • Ktor ) and http ( • http4k )
If you like this, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (java-to-kotlin.dev). It's about far more than just the syntax differences between the languages - it shows how to upgrade your thinking to a more functional style. - วิทยาศาสตร์และเทคโนโลยี
You are kotlin artist.
Thank you, that means a lot.
👍🏻
I don't really see why you couldn't just use ktor's httpclient to test http4k server code... you could have just instantiated the httpclient fully to use a port instead of Ktor's simulated server start... it was interesting how you adapted two completely different apis though!
Yeah I think I got a bit carried away. I originally wanted to migrate the routes one by one and that coloured my thinking.