https://kotlinlang.org logo
#ktor
Title
# ktor
m

Mykola Gurov

05/07/2021, 5:31 PM
Is using Ktor to build a fake HTTP server a good idea? In particular, the light-weightness and the startup time are of a concern. Context: Kotlin backend, integration tests; want to replace in-proc mocks of connectors to external dependencies with something more convenient for programming than WireMock or MockServer.
c

cy

05/07/2021, 5:33 PM
Ktor has a test server so you can make requests without actual http while all other features work as in a real server
m

Mykola Gurov

05/07/2021, 5:36 PM
The code under test isn't Ktor'ed., e.g. SpringTemplates making calls to external HTTP endpoints. Will the ktor test server work in such case?
c

cy

05/07/2021, 5:37 PM
Unfortunately no, you need to start a real ktor server or use ktor client of the test engine. Ktor server startup is low but not that good for large amount of small tests
m

Mykola Gurov

05/07/2021, 5:39 PM
What would be the lightest engine to try? The fake (ktor) would be reused, but the 5 secs startup time I see on my first test with Netty is a bit concerning...
c

cy

05/07/2021, 5:41 PM
Haven't tested the startup time but cio will be faster i think
👍 1
5 sec .. hmmm.. it shouldn't be that slow
m

Mykola Gurov

05/07/2021, 5:50 PM
4 secs with CIO... Will try on an empty project and report back.
i

ian.shaun.thomas

05/07/2021, 5:56 PM
if you're doing integration tests, is a fake server really a correct integration test?
m

Mykola Gurov

05/07/2021, 5:58 PM
Define "correct". I do prefer my Spring context to be as close as possible to the production one, which rules the mocks out. WireMock is an option, but not convenient for the tests that don't care much about the contract.
i

ian.shaun.thomas

05/07/2021, 5:58 PM
to state it more clearly, I've gone down the path it sounds like you're taking and ultimately it just became unmanageable as the things I was effectively mocking with a fake server changed, I had to change my code and my mock code and then what did I even test or prove? I would strongly urge you to look at ways to have an env you can manipulate and test against directly.
def possible im not understanding your use case so I apologize if I'm down a pointless tangent
m

Mykola Gurov

05/07/2021, 6:00 PM
Contract tests is a separate question. But I don't want my build to depend on the availability of the external services.
b

Big Chungus

05/07/2021, 6:00 PM
I've written this for simillar issue. Might be useful to you too https://github.com/mpetuska/rest-mock-server
m

Mykola Gurov

05/07/2021, 6:01 PM
Thank you @Big Chungus, but I guess that would be something alike WireMock? I prefer simple fake http server to avoid the need to program every response.
👍 1
i

ian.shaun.thomas

05/07/2021, 6:01 PM
that's pretty cool @Big Chungus, does it allow you to set response headers too? Glancing at the docs, it's not clear.
b

Big Chungus

05/07/2021, 6:02 PM
It does, you can programmit with js snippet and do whatever you want with response
👍 1
See handler mocking
If you just need static responses, why not just boot up mock api from raml or swagger?
m

Mykola Gurov

05/07/2021, 6:05 PM
I see - that's a nice option indeed (handler replacement) 👍. Not for my current case since I'd prefer to stay in the realm of kotlin/JVM for easier control of the fake test server backend.
i

ian.shaun.thomas

05/07/2021, 6:05 PM
But I don't want my build to depend on the availability of the external services.
totally reasonable, I assume it's not possible to replicate those services as part of your build test phase?
b

Big Chungus

05/07/2021, 6:06 PM
But then instead of managing mocks via http you're left doing so via code
Isn't that even more work then? Or am I missing something?
m

Mykola Gurov

05/07/2021, 6:07 PM
@ian.shaun.thomas yes, you can replicate. The range of the replication can be from in-proc mocking (mokk or Mockito), via http mocks (TestServer, WireMock, etc.) via fake http implementation (I'm considering) all the way to a separate app running in a docker instance (too much for my case).
👍 1
c

christophsturm

05/07/2021, 6:08 PM
thats a also a nice way to do it, but only for okhttp
it would be great to have something that can record real life traffic and is agnostic to http servers
m

Mykola Gurov

05/07/2021, 6:12 PM
this thread is deviating very far from the original question, but I'm liking it 😜
@christophsturm do you mean something like http://wiremock.org/docs/record-playback/ ?
👀 1
c

christophsturm

05/07/2021, 6:13 PM
i used wiremock back in the java days
188 Views