Pass transaction id from request to mercurial hooks

This commit is contained in:
Sebastian Sdorra
2020-11-19 14:30:39 +01:00
parent 29faa5ec6c
commit 1311061c82
12 changed files with 195 additions and 27 deletions

View File

@@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.filter;
import com.github.sdorra.shiro.ShiroRule;
@@ -34,6 +34,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.MDC;
import sonia.scm.AbstractTestBase;
import sonia.scm.SCMContext;
import sonia.scm.TransactionId;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
@@ -50,28 +51,28 @@ import static org.mockito.Mockito.when;
/**
* Unit tests for {@link MDCFilter}.
*
*
* @author Sebastian Sdorra <sebastian.sdorra@gmail.com>
*/
@RunWith(MockitoJUnitRunner.Silent.class)
public class MDCFilterTest extends AbstractTestBase {
@Rule
public ShiroRule shiro = new ShiroRule();
@Mock
private HttpServletRequest request;
@Mock
private HttpServletResponse response;
private final MDCFilter filter = new MDCFilter();
/**
* Tests {@link MDCFilter#doFilter(HttpServletRequest, HttpServletResponse, FilterChain)}.
*
*
* @throws IOException
* @throws ServletException
* @throws ServletException
*/
@Test
@SubjectAware(
@@ -85,44 +86,44 @@ public class MDCFilterTest extends AbstractTestBase {
when(request.getRemoteAddr()).thenReturn("127.0.0.1");
when(request.getRemoteHost()).thenReturn("localhost");
when(request.getMethod()).thenReturn("GET");
MDCCapturingFilterChain chain = new MDCCapturingFilterChain();
filter.doFilter(request, response, chain);
assertNotNull(chain.ctx);
assertEquals("trillian", chain.ctx.get(MDCFilter.MDC_USERNAME));
assertEquals("api/v1/repositories", chain.ctx.get(MDCFilter.MDC_REQUEST_URI));
assertEquals("127.0.0.1", chain.ctx.get(MDCFilter.MDC_CLIENT_IP));
assertEquals("localhost", chain.ctx.get(MDCFilter.MDC_CLIENT_HOST));
assertEquals("GET", chain.ctx.get(MDCFilter.MDC_REQUEST_METHOD));
assertNotNull(chain.ctx.get(MDCFilter.MDC_TRANSACTION_ID));
assertNotNull(chain.ctx.get(TransactionId.KEY));
}
/**
* Tests {@link MDCFilter#doFilter(HttpServletRequest, HttpServletResponse, FilterChain)} as anonymous user.
*
*
* @throws IOException
* @throws ServletException
* @throws ServletException
*/
@Test
@SubjectAware
public void testDoFilterAsAnonymous() throws IOException, ServletException {
MDCCapturingFilterChain chain = new MDCCapturingFilterChain();
filter.doFilter(request, response, chain);
assertNotNull(chain.ctx);
assertEquals(SCMContext.USER_ANONYMOUS, chain.ctx.get(MDCFilter.MDC_USERNAME));
}
private static class MDCCapturingFilterChain implements FilterChain {
private Map<String, String> ctx;
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
this.ctx = MDC.getCopyOfContextMap();
}
}
}