mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-05-07 14:25:57 +02:00
Pass transaction id from request to mercurial hooks
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user