Is using Ktor to build a fake HTTP server a good i...
# ktor
m
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
Ktor has a test server so you can make requests without actual http while all other features work as in a real server
m
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
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
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
Haven't tested the startup time but cio will be faster i think
๐Ÿ‘ 1
5 sec .. hmmm.. it shouldn't be that slow
m
4 secs with CIO... Will try on an empty project and report back.
i
if you're doing integration tests, is a fake server really a correct integration test?
m
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
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
Contract tests is a separate question. But I don't want my build to depend on the availability of the external services.
b
I've written this for simillar issue. Might be useful to you too https://github.com/mpetuska/rest-mock-server
m
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
that's pretty cool @Big Chungus, does it allow you to set response headers too? Glancing at the docs, it's not clear.
b
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
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
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
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
@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
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
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
i used wiremock back in the java days
207 Views