1616
1717package fr .davit .pekko .http .metrics .core .scaladsl .server
1818
19- import org .apache .pekko .http .scaladsl .marshalling .PredefinedToEntityMarshallers ._
19+ import fr .davit .pekko .http .metrics .core .AttributeLabeler
20+ import fr .davit .pekko .http .metrics .core .PathLabeler
21+ import fr .davit .pekko .http .metrics .core .TestRegistry
22+ import org .apache .pekko .http .scaladsl .marshalling .PredefinedToEntityMarshallers .*
23+ import org .apache .pekko .http .scaladsl .model .HttpResponse
2024import org .apache .pekko .http .scaladsl .model .StatusCodes
21- import org .apache .pekko .http .scaladsl .server .Directives ._
25+ import org .apache .pekko .http .scaladsl .model .headers .HttpEncoding
26+ import org .apache .pekko .http .scaladsl .model .headers .HttpEncodings
27+ import org .apache .pekko .http .scaladsl .model .headers .`Accept-Encoding`
28+ import org .apache .pekko .http .scaladsl .model .headers .`Content-Encoding`
29+ import org .apache .pekko .http .scaladsl .server .Directives .*
2230import org .apache .pekko .http .scaladsl .testkit .ScalatestRouteTest
23- import fr .davit .pekko .http .metrics .core .{AttributeLabeler , PathLabeler , TestRegistry }
2431import org .scalatest .flatspec .AnyFlatSpec
32+ import org .scalatest .matchers .Matcher
2533import org .scalatest .matchers .should .Matchers
34+ import org .apache .pekko .http .scaladsl .coding .Coders
35+ import org .apache .pekko .http .scaladsl .unmarshalling .Unmarshal
36+ import scala .concurrent .Await
37+ import scala .concurrent .duration .*
2638
2739class HttpMetricsDirectivesSpec extends AnyFlatSpec with Matchers with ScalatestRouteTest {
2840
2941 import HttpMetricsDirectives ._
3042
43+ def haveNoContentEncoding : Matcher [HttpResponse ] =
44+ be(None ).compose { (_ : HttpResponse ).header[`Content-Encoding`] }
45+ def haveContentEncoding (encoding : HttpEncoding ): Matcher [HttpResponse ] =
46+ be(Some (`Content-Encoding`(encoding))).compose { (_ : HttpResponse ).header[`Content-Encoding`] }
47+
3148 " HttpMetricsDirectives" should " expose the registry" in {
3249 implicit val marshaller = StringMarshaller .compose[TestRegistry ](r => s " active: ${r.requestsActive.value()}" )
3350 val registry = new TestRegistry ()
@@ -38,6 +55,29 @@ class HttpMetricsDirectivesSpec extends AnyFlatSpec with Matchers with Scalatest
3855 }
3956
4057 Get (" /metrics" ) ~> route ~> check {
58+ response should haveNoContentEncoding
59+ responseAs[String ] shouldBe " active: 1"
60+ }
61+
62+ // gzip
63+ Get (" /metrics" ) ~> `Accept-Encoding`(HttpEncodings .gzip) ~> route ~> check {
64+ response should haveContentEncoding(HttpEncodings .gzip)
65+ val decodedResponse = Coders .Gzip .decodeMessage(response)
66+ val data = Await .result(Unmarshal (decodedResponse).to[String ], 1 .second)
67+ data shouldBe " active: 1"
68+ }
69+
70+ // deflate
71+ Get (" /metrics" ) ~> `Accept-Encoding`(HttpEncodings .deflate) ~> route ~> check {
72+ response should haveContentEncoding(HttpEncodings .deflate)
73+ val decodedResponse = Coders .Deflate .decodeMessage(response)
74+ val data = Await .result(Unmarshal (decodedResponse).to[String ], 1 .second)
75+ data shouldBe " active: 1"
76+ }
77+
78+ // unknown -> accept and skip encoding
79+ Get (" /metrics" ) ~> `Accept-Encoding`(HttpEncodings .`x-zip`) ~> route ~> check {
80+ response should haveNoContentEncoding
4181 responseAs[String ] shouldBe " active: 1"
4282 }
4383 }
0 commit comments