https://kotlinlang.org logo
#http4k
Title
# http4k
v

Vojtěch Knyttl

11/19/2020, 11:37 PM
is ti possible to test on top of
ContractRoute
with
Path.string()
parameters? Basically, I can do:
Copy code
val response = myContractRoute(request)
but when accessing
Path.string()
parameters I am getting:
Copy code
Request was not routed, so no uri-template present
I tried then with what I found in https://www.http4k.org/cookbook/test_driven_apps/:
Copy code
val root = routes(myContractRoute)
but it seems the
routes()
method cannot work with ContractRoute. is there a way?
d

dave

11/20/2020, 4:49 AM
You need a contract block to wrap the contract route - a simple empty one will do it - TBH am not sure if this has been changed by the recent routing changes, because I think I used to work without that. Will have to investigate..
v

Vojtěch Knyttl

11/20/2020, 9:40 AM
do you want to create an issue for investigation?
Copy code
val app = routes(contract { routes += myContractRoute })
val response = app(request)
still:
Request was not routed, so no uri-template present
d

dave

11/20/2020, 5:42 PM
I've just tried this and it seems to work fine:
Copy code
@Test
    fun `test route`() {
        val route = <http://Path.int|Path.int>().of("sue") bindContract GET to { path -> { Response(OK).body(path.toString()) } }
        assertThat(route(Request(GET, "/123")), hasBody("123"))
    }
in a contract route, the parameters are already extracted and passed into the bound handler creation function, so you don't need to get them again
v

Vojtěch Knyttl

11/20/2020, 7:32 PM
Ok, I can see the problem now – because the method is too complex, it is easier for me to use
request.path("sue")
- but path is empty within the tests.
so is it correct that the path is empty?
15 Views