|
| 1 | +/* |
| 2 | + * Copyright 2012 Comcast Cable Communications Management, LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.comcast.money.jakarta.servlet |
| 18 | + |
| 19 | +import com.comcast.money.api.{Span, SpanId} |
| 20 | +import com.comcast.money.core.formatters.FormatterUtils.randomRemoteSpanId |
| 21 | +import com.comcast.money.core.internal.SpanLocal |
| 22 | +import org.mockito.Mockito._ |
| 23 | +import org.mockito.stubbing.OngoingStubbing |
| 24 | +import org.scalatest.OptionValues._ |
| 25 | +import org.scalatest.matchers.should.Matchers |
| 26 | +import org.scalatest.wordspec.AnyWordSpec |
| 27 | +import org.scalatest.{BeforeAndAfter, OneInstancePerTest} |
| 28 | +import org.scalatestplus.mockito.MockitoSugar |
| 29 | + |
| 30 | +import java.util.Collections |
| 31 | +import jakarta.servlet.http.{HttpServletRequest, HttpServletResponse} |
| 32 | +import jakarta.servlet.{FilterChain, FilterConfig, ServletRequest, ServletResponse} |
| 33 | + |
| 34 | +class TraceFilterSpec extends AnyWordSpec with Matchers with OneInstancePerTest with BeforeAndAfter with MockitoSugar { |
| 35 | + |
| 36 | + val mockRequest = mock[HttpServletRequest] |
| 37 | + val mockResponse = mock[HttpServletResponse] |
| 38 | + val mockFilterChain = mock[FilterChain] |
| 39 | + val existingSpanId = randomRemoteSpanId() |
| 40 | + val underTest = new TraceFilter() |
| 41 | + val MoneyTraceFormat = "trace-id=%s;parent-id=%s;span-id=%s" |
| 42 | + val filterChain: FilterChain = (_: ServletRequest, _: ServletResponse) => capturedSpan = SpanLocal.current |
| 43 | + var capturedSpan: Option[Span] = None |
| 44 | + |
| 45 | + def traceParentHeader(spanId: SpanId): String = { |
| 46 | + val traceId = spanId.traceId.replace("-", "").toLowerCase |
| 47 | + f"00-$traceId%s-${spanId.selfId}%016x-00" |
| 48 | + } |
| 49 | + |
| 50 | + before { |
| 51 | + capturedSpan = None |
| 52 | + val empty: java.util.Enumeration[_] = Collections.emptyEnumeration() |
| 53 | + // The raw type seems to confuse the Scala compiler so the cast is required to compile successfully |
| 54 | + when(mockRequest.getHeaderNames).asInstanceOf[OngoingStubbing[java.util.Enumeration[_]]].thenReturn(empty) |
| 55 | + } |
| 56 | + |
| 57 | + "A TraceFilter" should { |
| 58 | + "clear the trace context when an http request arrives" in { |
| 59 | + underTest.doFilter(mockRequest, mockResponse, filterChain) |
| 60 | + SpanLocal.current shouldBe None |
| 61 | + } |
| 62 | + |
| 63 | + "always call the filter chain" in { |
| 64 | + underTest.doFilter(mockRequest, mockResponse, mockFilterChain) |
| 65 | + verify(mockFilterChain).doFilter(mockRequest, mockResponse) |
| 66 | + } |
| 67 | + |
| 68 | + "set the trace context to the money trace header if present" in { |
| 69 | + when(mockRequest.getHeader("X-MoneyTrace")) |
| 70 | + .thenReturn(MoneyTraceFormat.format(existingSpanId.traceId, existingSpanId.parentId, existingSpanId.selfId)) |
| 71 | + underTest.doFilter(mockRequest, mockResponse, filterChain) |
| 72 | + capturedSpan.value.info.id shouldEqual existingSpanId |
| 73 | + } |
| 74 | + |
| 75 | + "set the trace context to the traceparent header if present" in { |
| 76 | + when(mockRequest.getHeader("traceparent")) |
| 77 | + .thenReturn(traceParentHeader(existingSpanId)) |
| 78 | + underTest.doFilter(mockRequest, mockResponse, filterChain) |
| 79 | + |
| 80 | + val actualSpanId = capturedSpan.value.info.id |
| 81 | + actualSpanId.traceId shouldEqual existingSpanId.traceId |
| 82 | + actualSpanId.parentId shouldEqual existingSpanId.selfId |
| 83 | + } |
| 84 | + |
| 85 | + "prefer the money trace header over the W3C Trace Context header" in { |
| 86 | + when(mockRequest.getHeader("X-MoneyTrace")) |
| 87 | + .thenReturn(MoneyTraceFormat.format(existingSpanId.traceId, existingSpanId.parentId, existingSpanId.selfId)) |
| 88 | + when(mockRequest.getHeader("traceparent")) |
| 89 | + .thenReturn(traceParentHeader(SpanId.createNew())) |
| 90 | + underTest.doFilter(mockRequest, mockResponse, filterChain) |
| 91 | + capturedSpan.value.info.id shouldEqual existingSpanId |
| 92 | + } |
| 93 | + |
| 94 | + "not set the trace context if the money trace header could not be parsed" in { |
| 95 | + when(mockRequest.getHeader("X-MoneyTrace")).thenReturn("can't parse this") |
| 96 | + underTest.doFilter(mockRequest, mockResponse, filterChain) |
| 97 | + capturedSpan shouldBe None |
| 98 | + } |
| 99 | + |
| 100 | + "adds Money header to response" in { |
| 101 | + when(mockRequest.getHeader("X-MoneyTrace")) |
| 102 | + .thenReturn(MoneyTraceFormat.format(existingSpanId.traceId, existingSpanId.parentId, existingSpanId.selfId)) |
| 103 | + underTest.doFilter(mockRequest, mockResponse, mockFilterChain) |
| 104 | + verify(mockResponse).addHeader( |
| 105 | + "X-MoneyTrace", |
| 106 | + MoneyTraceFormat.format(existingSpanId.traceId, existingSpanId.parentId, existingSpanId.selfId)) |
| 107 | + } |
| 108 | + |
| 109 | + "adds Trace Context header to response" in { |
| 110 | + when(mockRequest.getHeader("traceparent")) |
| 111 | + .thenReturn(traceParentHeader(existingSpanId)) |
| 112 | + underTest.doFilter(mockRequest, mockResponse, mockFilterChain) |
| 113 | + verify(mockResponse).addHeader( |
| 114 | + "traceparent", |
| 115 | + traceParentHeader(existingSpanId)) |
| 116 | + } |
| 117 | + |
| 118 | + "loves us some test coverage" in { |
| 119 | + val mockConf = mock[FilterConfig] |
| 120 | + underTest.init(mockConf) |
| 121 | + underTest.destroy() |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments