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

Kris Wong

10/21/2020, 3:07 PM
is it possible to update the routing on the
Applicaiton
after the server is stated?
j

jorge.rego

10/21/2020, 3:18 PM
@John O'Reilly was asking about dynamically enable/disable routes in this channel before. Is this something that could meet your requirements?
j

John O'Reilly

10/21/2020, 3:18 PM
k

Kris Wong

10/21/2020, 3:19 PM
no. I would prefer to design my API to add routes after the
ApplicationEngine
is started. however, I can adapt my API if that does not work properly.
I see that the APIs exist to add routes, but not sure what the expectation is
j

jorge.rego

10/21/2020, 3:23 PM
If we assume the server will send the request through a pipeline to see if it matches or not the defined routes, defining the routes anyway and flag them enabled/disabled (returning 404 manually if not enabled), this could work in the same way. Am I missing something?
Are you trying to support multiple API versions?
k

Kris Wong

10/21/2020, 3:24 PM
this is a very different use case. the routes are not known when the server is started
i can change that, but it impacts the APIs that I am writing
j

jorge.rego

10/21/2020, 3:27 PM
Wooow, ok. Then I guess you need to step out of the regular way a route matching pipeline. How and when would you know which routes will be available?
k

Kris Wong

10/21/2020, 3:28 PM
I have an API to add them
I am using embedded CIO server to support unit testing/mocking HTTP calls
j

jorge.rego

10/21/2020, 3:30 PM
Why are you referring to testing now? is this relevant?
k

Kris Wong

10/21/2020, 3:30 PM
i am just informing you of my use case
in case that helps
j

jorge.rego

10/21/2020, 3:32 PM
Mmmm testing should not impact your code, only you need to make it clean injecting dependencies, to assure it is testable. You want to modify your API because of the use case you are providing, or because you want to test something?
k

Kris Wong

10/21/2020, 3:32 PM
my use case is testing. this is only used to enable testing.
j

jorge.rego

10/21/2020, 3:34 PM
Then you don’t need to go into that direction, I believe. I’ve written an article about route testing with Ktor that could help you. Have a look: https://medium.com/@math21/testing-a-ktor-server-part-i-route-testing-84f8e82454d7 This is based in an architecture proposal I’ve done, but it may fit what you are doing.
Let me know if that helps
k

Kris Wong

10/21/2020, 3:35 PM
unfortunately no, i don't want to test a Ktor server
j

jorge.rego

10/21/2020, 3:36 PM
mmm then I don’t follow you. Your use case is testing? what does that mean?
k

Kris Wong

10/21/2020, 3:38 PM
the server I am starting is used to mock HTTP responses for unit testing my application
j

jorge.rego

10/21/2020, 3:40 PM
oh, now I understand. Why don’t you go with something like this in your Android app? https://github.com/square/okhttp/tree/master/mockwebserver
k

Kris Wong

10/21/2020, 3:40 PM
6 of one, half dozen of the other. I started with Ktor since I was already using it for other things.
there's also Wiremock
j

jorge.rego

10/21/2020, 3:43 PM
In testing, you want to avoid as much toxicity as possible, so I would avoid all the communications with any server to test your app. If you also want to check your API, there are other tools (like RestAssured in case of REST API) that could deal with API testing.
k

Kris Wong

10/21/2020, 3:44 PM
that's what I am doing, avoiding network requests by embedding a server
j

jorge.rego

10/21/2020, 3:46 PM
MockWebserver does exactly what you describe. We are using it professionally in our projects and it works fine.
k

Kris Wong

10/21/2020, 3:46 PM
I don't doubt that
to close the loop on this, adding routes after the server is started does function as expected
👏 1
2 Views