Using Junit and Kotlin, the following test throws NoHostException
@MicronautTest
class BasicAuthTest {
@Inject
@Client("/")
lateinit var client: RxHttpClient
@Test
fun verifyHttpBasicAuthWorks() {
val e = Executable { client.toBlocking().exchange<Any, Any>(HttpRequest.GET<Any>("/").accept(MediaType.TEXT_PLAIN)) } // <3>
....
}
It works for Java / Junit
@MicronautTest
public class BasicAuthTest {
@Inject
@Client("/")
RxHttpClient client;
@Test
void verifyHttpBasicAuthWorks() {
Executable e = () -> client.toBlocking().exchange(HttpRequest.GET("/").accept(MediaType.TEXT_PLAIN));
It works for Groovy with Spock
@MicronautTest
class BasicAuthSpec extends Specification {
@Inject
@Client("/")
RxHttpClient client
def "by default every endpoint is secured"() {
when: 'Accessing a secured URL without authenticating'
client.toBlocking().exchange(HttpRequest.GET('/'))
This works for Kotlin
@MicronautTest
class BasicAuthTest {
@Inject
lateinit var embeddedServer: EmbeddedServer
@Test
fun verifyHttpBasicAuthWorks() {
val client : RxHttpClient = embeddedServer.applicationContext.createBean(RxHttpClient::class.java, embeddedServer.url)
val e = Executable { client.toBlocking().exchange<Any, Any>(HttpRequest.GET<Any>("/").accept(MediaType.TEXT_PLAIN)) } // <3>
Sample project: https://github.com/micronaut-guides/micronaut-security-basicauth-kotlin
Micronaut version: 2.0.0.BUILD-SNAPSHOT
Has to be @field:Client(“/“)
@graemerocher Why has to be @field:Client("/")??
Ok, I saw in the docs, thanks!
Most helpful comment
Has to be @field:Client(“/“)