forked from zio/zio-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middlewares to add log annotations
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
zio-http/src/test/scala/zio/http/LogAnnotationMiddlewareSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package zio.http | ||
|
||
import zio._ | ||
import zio.test._ | ||
|
||
object LogAnnotationMiddlewareSpec extends ZIOSpecDefault { | ||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("LogAnnotationMiddlewareSpec")( | ||
test("add static log annotation") { | ||
val response = Routes | ||
.singleton( | ||
handler(ZIO.logWarning("Oh!") *> ZIO.succeed(Response.text("Hey logging!"))), | ||
) | ||
.@@(Middleware.logAnnotate("label", "value")) | ||
.toHttpApp | ||
.runZIO(Request.get("/")) | ||
|
||
for { | ||
_ <- response | ||
logs <- ZTestLogger.logOutput | ||
log = logs.filter(_.message() == "Oh!").head | ||
} yield assertTrue(log.annotations.get("label").contains("value")) | ||
|
||
}, | ||
test("add request method and path as annotation") { | ||
val response = Routes | ||
.singleton( | ||
handler(ZIO.logWarning("Oh!") *> ZIO.succeed(Response.text("Hey logging!"))), | ||
) | ||
.@@( | ||
Middleware.logAnnotate(req => | ||
Set(LogAnnotation("method", req.method.name), LogAnnotation("path", req.path.encode)), | ||
), | ||
) | ||
.toHttpApp | ||
.runZIO(Request.get("/")) | ||
|
||
for { | ||
_ <- response | ||
logs <- ZTestLogger.logOutput | ||
log = logs.filter(_.message() == "Oh!").head | ||
} yield assertTrue( | ||
log.annotations.get("method").contains("GET"), | ||
log.annotations.get("path").contains("/"), | ||
) | ||
}, | ||
test("add headers as annotation") { | ||
val response = Routes | ||
.singleton( | ||
handler(ZIO.logWarning("Oh!") *> ZIO.succeed(Response.text("Hey logging!"))), | ||
) | ||
.@@(Middleware.logAnnotateHeaders("header")) | ||
.@@(Middleware.logAnnotateHeaders(Header.UserAgent.name)) | ||
.toHttpApp | ||
.runZIO { | ||
Request | ||
.get("/") | ||
.addHeader("header", "value") | ||
.addHeader(Header.UserAgent.Product("zio-http", Some("3.0.0"))) | ||
} | ||
|
||
for { | ||
_ <- response | ||
logs <- ZTestLogger.logOutput | ||
log = logs.filter(_.message() == "Oh!").head | ||
} yield assertTrue( | ||
log.annotations.get("header").contains("value"), | ||
log.annotations.get(Header.UserAgent.name).contains("zio-http/3.0.0"), | ||
) | ||
}, | ||
) | ||
} |