2018-09-10 08:37:31 +02:00
|
|
|
package sonia.scm.web.protocol;
|
|
|
|
|
|
2019-07-22 14:26:15 +02:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
2019-07-22 14:41:12 +02:00
|
|
|
import org.junit.jupiter.api.Nested;
|
2019-07-22 14:26:15 +02:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
2018-09-10 08:37:31 +02:00
|
|
|
import org.mockito.InjectMocks;
|
|
|
|
|
import org.mockito.Mock;
|
2019-07-22 14:26:15 +02:00
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
2018-10-18 16:08:49 +02:00
|
|
|
import sonia.scm.NotFoundException;
|
2018-09-10 08:37:31 +02:00
|
|
|
import sonia.scm.PushStateDispatcher;
|
|
|
|
|
import sonia.scm.repository.DefaultRepositoryProvider;
|
|
|
|
|
import sonia.scm.repository.NamespaceAndName;
|
|
|
|
|
import sonia.scm.repository.Repository;
|
2019-07-22 14:41:12 +02:00
|
|
|
import sonia.scm.repository.RepositoryTestData;
|
2018-09-10 08:37:31 +02:00
|
|
|
import sonia.scm.repository.api.RepositoryService;
|
|
|
|
|
import sonia.scm.repository.api.RepositoryServiceFactory;
|
|
|
|
|
import sonia.scm.repository.spi.HttpScmProtocol;
|
|
|
|
|
import sonia.scm.web.UserAgent;
|
|
|
|
|
import sonia.scm.web.UserAgentParser;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.IOException;
|
2019-07-22 14:41:12 +02:00
|
|
|
import java.util.Optional;
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:26:15 +02:00
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
|
|
|
class HttpProtocolServletTest {
|
2018-09-10 08:37:31 +02:00
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private RepositoryServiceFactory serviceFactory;
|
2019-07-22 14:41:12 +02:00
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private NamespaceAndNameFromPathExtractor extractor;
|
|
|
|
|
|
2018-09-10 08:37:31 +02:00
|
|
|
@Mock
|
|
|
|
|
private PushStateDispatcher dispatcher;
|
2019-07-22 14:41:12 +02:00
|
|
|
|
2018-09-10 08:37:31 +02:00
|
|
|
@Mock
|
|
|
|
|
private UserAgentParser userAgentParser;
|
|
|
|
|
|
|
|
|
|
@InjectMocks
|
|
|
|
|
private HttpProtocolServlet servlet;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private RepositoryService repositoryService;
|
2019-07-22 14:41:12 +02:00
|
|
|
|
2018-09-10 08:37:31 +02:00
|
|
|
@Mock
|
|
|
|
|
private UserAgent userAgent;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private HttpServletRequest request;
|
2019-07-22 14:41:12 +02:00
|
|
|
|
2018-09-10 08:37:31 +02:00
|
|
|
@Mock
|
|
|
|
|
private HttpServletResponse response;
|
2019-07-22 14:41:12 +02:00
|
|
|
|
2018-09-10 08:37:31 +02:00
|
|
|
@Mock
|
|
|
|
|
private HttpScmProtocol protocol;
|
|
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
@Nested
|
|
|
|
|
class Browser {
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void prepareMocks() {
|
|
|
|
|
when(userAgentParser.parse(request)).thenReturn(userAgent);
|
|
|
|
|
when(userAgent.isBrowser()).thenReturn(true);
|
|
|
|
|
when(request.getRequestURI()).thenReturn("uri");
|
|
|
|
|
}
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
@Test
|
|
|
|
|
void shouldDispatchBrowserRequests() throws ServletException, IOException {
|
|
|
|
|
when(userAgent.isBrowser()).thenReturn(true);
|
|
|
|
|
when(request.getRequestURI()).thenReturn("uri");
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
servlet.service(request, response);
|
|
|
|
|
|
|
|
|
|
verify(dispatcher).dispatch(request, response, "uri");
|
|
|
|
|
}
|
2018-09-10 08:37:31 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
@Nested
|
|
|
|
|
class ScmClient {
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
@BeforeEach
|
|
|
|
|
void prepareMocks() {
|
|
|
|
|
when(userAgentParser.parse(request)).thenReturn(userAgent);
|
|
|
|
|
when(userAgent.isBrowser()).thenReturn(false);
|
|
|
|
|
}
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
@Test
|
|
|
|
|
void shouldHandleBadPaths() throws IOException, ServletException {
|
|
|
|
|
when(request.getPathInfo()).thenReturn("/illegal");
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
servlet.service(request, response);
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
verify(response).setStatus(400);
|
|
|
|
|
}
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
@Test
|
|
|
|
|
void shouldHandleNotExistingRepository() throws IOException, ServletException {
|
|
|
|
|
when(request.getPathInfo()).thenReturn("/not/exists");
|
|
|
|
|
|
|
|
|
|
NamespaceAndName repo = new NamespaceAndName("not", "exists");
|
|
|
|
|
when(extractor.fromUri("/not/exists")).thenReturn(Optional.of(repo));
|
|
|
|
|
when(serviceFactory.create(repo)).thenThrow(new NotFoundException("Test", "a"));
|
|
|
|
|
|
|
|
|
|
servlet.service(request, response);
|
|
|
|
|
|
|
|
|
|
verify(response).setStatus(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldDelegateToProvider() throws IOException, ServletException {
|
|
|
|
|
NamespaceAndName repo = new NamespaceAndName("space", "name");
|
|
|
|
|
when(extractor.fromUri("/space/name")).thenReturn(Optional.of(repo));
|
|
|
|
|
when(serviceFactory.create(repo)).thenReturn(repositoryService);
|
|
|
|
|
|
|
|
|
|
when(request.getPathInfo()).thenReturn("/space/name");
|
|
|
|
|
Repository repository = RepositoryTestData.createHeartOfGold();
|
|
|
|
|
when(repositoryService.getRepository()).thenReturn(repository);
|
|
|
|
|
when(repositoryService.getProtocol(HttpScmProtocol.class)).thenReturn(protocol);
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
servlet.service(request, response);
|
2018-09-10 08:37:31 +02:00
|
|
|
|
2019-07-22 14:41:12 +02:00
|
|
|
verify(request).setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repository);
|
|
|
|
|
verify(protocol).serve(request, response, null);
|
|
|
|
|
verify(repositoryService).close();
|
|
|
|
|
}
|
2018-09-10 08:37:31 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|