|
| 1 | +package com.amazonaws.serverless.proxy.internal.servlet; |
| 2 | + |
| 3 | + |
| 4 | +import com.amazonaws.serverless.proxy.model.ApiGatewayRequestIdentity; |
| 5 | +import com.amazonaws.serverless.proxy.model.AwsProxyRequest; |
| 6 | +import com.amazonaws.serverless.proxy.model.AwsProxyRequestContext; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import javax.servlet.http.HttpServletRequest; |
| 11 | +import javax.servlet.http.HttpServletResponse; |
| 12 | + |
| 13 | +import java.time.Clock; |
| 14 | +import java.time.Instant; |
| 15 | +import java.time.ZoneId; |
| 16 | + |
| 17 | +import static com.amazonaws.serverless.proxy.RequestReader.API_GATEWAY_EVENT_PROPERTY; |
| 18 | +import static org.hamcrest.CoreMatchers.containsString; |
| 19 | +import static org.junit.Assert.assertThat; |
| 20 | +import static org.mockito.Matchers.eq; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +public class ApacheCombinedServletLogFormatterTest { |
| 25 | + |
| 26 | + private ApacheCombinedServletLogFormatter sut; |
| 27 | + |
| 28 | + private HttpServletRequest mockServletRequest; |
| 29 | + private HttpServletResponse mockServletResponse; |
| 30 | + private AwsProxyRequest proxyRequest; |
| 31 | + private AwsProxyRequestContext context; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setup() { |
| 35 | + proxyRequest = new AwsProxyRequest(); |
| 36 | + Clock fixedClock = Clock.fixed(Instant.ofEpochSecond(665888523L), ZoneId.of("UTC")); |
| 37 | + mockServletRequest = mock(HttpServletRequest.class); |
| 38 | + when(mockServletRequest.getAttribute(eq(API_GATEWAY_EVENT_PROPERTY))) |
| 39 | + .thenReturn(proxyRequest); |
| 40 | + when(mockServletRequest.getMethod()) |
| 41 | + .thenReturn("GET"); |
| 42 | + mockServletResponse = mock(HttpServletResponse.class); |
| 43 | + context = new AwsProxyRequestContext(); |
| 44 | + context.setIdentity(new ApiGatewayRequestIdentity()); |
| 45 | + proxyRequest.setRequestContext(context); |
| 46 | + |
| 47 | + sut = new ApacheCombinedServletLogFormatter(fixedClock); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void logsCurrentTimeWhenContextNull() { |
| 52 | + // given |
| 53 | + proxyRequest.setRequestContext(null); |
| 54 | + |
| 55 | + // when |
| 56 | + String actual = sut.format(mockServletRequest, mockServletResponse, null); |
| 57 | + |
| 58 | + // then |
| 59 | + assertThat(actual, containsString("[07/02/1991:01:02:03Z]")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void logsCurrentTimeWhenRequestTimeZero() { |
| 64 | + // given |
| 65 | + context.setRequestTimeEpoch(0); |
| 66 | + |
| 67 | + // when |
| 68 | + String actual = sut.format(mockServletRequest, mockServletResponse, null); |
| 69 | + |
| 70 | + // then |
| 71 | + assertThat(actual, containsString("[07/02/1991:01:02:03Z]")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void logsRequestTimeWhenRequestTimeEpochGreaterThanZero() { |
| 76 | + // given |
| 77 | + context.setRequestTimeEpoch(1563023494000L); |
| 78 | + |
| 79 | + // when |
| 80 | + String actual = sut.format(mockServletRequest, mockServletResponse, null); |
| 81 | + |
| 82 | + // then |
| 83 | + assertThat(actual, containsString("[13/07/2019:13:11:34Z]")); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments