Skip to content

Commit 6e12b4a

Browse files
committed
Add additional HttpServer extensions and tests
Signed-off-by: Dmitry Sulman <[email protected]>
1 parent b4aae7a commit 6e12b4a

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
package io.github.dmitrysulman.logback.access.reactor.netty
22

33
import reactor.netty.http.server.HttpServer
4+
import java.net.URL
45

56
/**
67
* Extension for [HttpServer] providing [HttpServer.accessLog] method alternative.
78
*
89
* @param reactorNettyAccessLogFactory The [ReactorNettyAccessLogFactory] instance for access log configuration.
10+
* @return a new [HttpServer]
911
*/
1012
fun HttpServer.enableLogbackAccess(reactorNettyAccessLogFactory: ReactorNettyAccessLogFactory): HttpServer =
1113
accessLog(true, reactorNettyAccessLogFactory)
14+
15+
/**
16+
* Extension for [HttpServer] providing [HttpServer.accessLog] method alternative with a default
17+
* [ReactorNettyAccessLogFactory] configuration.
18+
*
19+
* @return a new [HttpServer]
20+
*/
21+
fun HttpServer.enableLogbackAccess(): HttpServer = enableLogbackAccess(ReactorNettyAccessLogFactory())
22+
23+
/**
24+
* Extension for [HttpServer] providing [HttpServer.accessLog] method alternative.
25+
*
26+
* @param fileName The file name of the configuration file to load.
27+
* @return a new [HttpServer].
28+
*/
29+
fun HttpServer.enableLogbackAccess(fileName: String): HttpServer = enableLogbackAccess(ReactorNettyAccessLogFactory(fileName))
30+
31+
/**
32+
* Extension for [HttpServer] providing [HttpServer.accessLog] method alternative.
33+
*
34+
* @param config the [URL] pointing to the configuration file to be used for setup.
35+
* @return a new [HttpServer].
36+
*/
37+
fun HttpServer.enableLogbackAccess(config: URL): HttpServer = enableLogbackAccess(ReactorNettyAccessLogFactory(config))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.dmitrysulman.logback.access.reactor.netty
2+
3+
import io.kotest.matchers.string.shouldEndWith
4+
import io.mockk.mockk
5+
import io.mockk.slot
6+
import io.mockk.verify
7+
import org.junit.jupiter.api.Test
8+
import reactor.netty.http.server.HttpServer
9+
10+
class ExtensionsTests {
11+
@Test
12+
fun `test HttpServer#enableLogbackAccess() extension`() {
13+
val httpServerMock = mockk<HttpServer>(relaxed = true)
14+
val factoryCapture = slot<ReactorNettyAccessLogFactory>()
15+
httpServerMock.enableLogbackAccess()
16+
verify(exactly = 1) { httpServerMock.accessLog(true, capture(factoryCapture)) }
17+
factoryCapture.captured.accessContext.name shouldEndWith "logback-access.xml"
18+
}
19+
20+
@Test
21+
fun `test HttpServer#enableLogbackAccess(factory) extension`() {
22+
val factory = ReactorNettyAccessLogFactory()
23+
val httpServerMock = mockk<HttpServer>(relaxed = true)
24+
httpServerMock.enableLogbackAccess(factory)
25+
verify(exactly = 1) { httpServerMock.accessLog(true, factory) }
26+
}
27+
28+
@Test
29+
fun `test HttpServer#enableLogbackAccess(filename) extension`() {
30+
val httpServerMock = mockk<HttpServer>(relaxed = true)
31+
val factoryCapture = slot<ReactorNettyAccessLogFactory>()
32+
httpServerMock.enableLogbackAccess("logback-access-resource.xml")
33+
verify(exactly = 1) { httpServerMock.accessLog(true, capture(factoryCapture)) }
34+
factoryCapture.captured.accessContext.name shouldEndWith "logback-access-resource.xml"
35+
}
36+
37+
@Test
38+
fun `test HttpServer#enableLogbackAccess(url) extension`() {
39+
val httpServerMock = mockk<HttpServer>(relaxed = true)
40+
val factoryCapture = slot<ReactorNettyAccessLogFactory>()
41+
httpServerMock.enableLogbackAccess(this::class.java.classLoader.getResource("logback-access-url.xml")!!)
42+
verify(exactly = 1) { httpServerMock.accessLog(true, capture(factoryCapture)) }
43+
factoryCapture.captured.accessContext.name shouldEndWith "logback-access-url.xml"
44+
}
45+
}

logback-access-reactor-netty/src/test/kotlin/io/github/dmitrysulman/logback/access/reactor/netty/integration/IntegrationTests.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import ch.qos.logback.access.common.joran.JoranConfigurator
44
import ch.qos.logback.access.common.spi.IAccessEvent
55
import ch.qos.logback.core.spi.FilterReply
66
import io.github.dmitrysulman.logback.access.reactor.netty.ReactorNettyAccessLogFactory
7-
import io.github.dmitrysulman.logback.access.reactor.netty.enableLogbackAccess
87
import io.kotest.assertions.nondeterministic.continually
98
import io.kotest.assertions.nondeterministic.eventually
109
import io.kotest.matchers.collections.shouldBeEmpty
@@ -199,7 +198,7 @@ class IntegrationTests {
199198
responseContent: String,
200199
) = HttpServer
201200
.create()
202-
.enableLogbackAccess(accessLogFactory)
201+
.accessLog(true, accessLogFactory)
203202
.handle { request, response ->
204203
val remoteHost = request.remoteAddress()!!.hostString
205204
val remoteAddress = request.remoteAddress()!!.address.hostAddress

0 commit comments

Comments
 (0)