Browse Source

raw: Fix exceptions when no path info is given to raw servlet

tags/v1.9.2
Florian Zschocke 3 years ago
parent
commit
2394397184

+ 4
- 1
src/main/java/com/gitblit/servlet/RawServlet.java View File



protected String getBranch(String repository, HttpServletRequest request) { protected String getBranch(String repository, HttpServletRequest request) {
String pi = request.getPathInfo(); String pi = request.getPathInfo();
if (pi == null || pi.isEmpty() || pi.equals("/")) return "";
String branch = pi.substring(pi.indexOf(repository) + repository.length() + 1); String branch = pi.substring(pi.indexOf(repository) + repository.length() + 1);
int fs = branch.indexOf('/'); int fs = branch.indexOf('/');
if (fs > -1) { if (fs > -1) {


protected String getPath(String repository, String branch, HttpServletRequest request) { protected String getPath(String repository, String branch, HttpServletRequest request) {
String base = repository + "/" + branch; String base = repository + "/" + branch;
String pi = request.getPathInfo().substring(1);
String pi = request.getPathInfo();
if (pi == null || pi.isEmpty() || pi.equals("/")) return "";
pi = pi.substring(1);
if (pi.equals(base)) { if (pi.equals(base)) {
return ""; return "";
} }

+ 111
- 1
src/test/java/com/gitblit/servlet/RawServletTest.java View File

import com.gitblit.Constants; import com.gitblit.Constants;
import com.gitblit.IStoredSettings; import com.gitblit.IStoredSettings;
import com.gitblit.Keys; import com.gitblit.Keys;
import com.gitblit.manager.IRepositoryManager;
import com.gitblit.tests.mock.MockGitblitContext; import com.gitblit.tests.mock.MockGitblitContext;
import com.gitblit.tests.mock.MockRuntimeManager; import com.gitblit.tests.mock.MockRuntimeManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;




import javax.servlet.http.HttpServletRequest;

import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


public class RawServletTest public class RawServletTest
{ {
private final static String FSC = "!"; private final static String FSC = "!";


private static MockRuntimeManager mockRuntimeManager = new MockRuntimeManager();
private static IStoredSettings settings; private static IStoredSettings settings;


private IRepositoryManager repositoryMngr;

private RawServlet rawServlet;



@BeforeClass @BeforeClass
public static void init() public static void init()
{ {
MockGitblitContext gitblitContext = new MockGitblitContext(); MockGitblitContext gitblitContext = new MockGitblitContext();
MockRuntimeManager mockRuntimeManager = new MockRuntimeManager();
gitblitContext.addManager(mockRuntimeManager); gitblitContext.addManager(mockRuntimeManager);
settings = mockRuntimeManager.getSettings(); settings = mockRuntimeManager.getSettings();
} }
public void setUp() public void setUp()
{ {
settings.overrideSetting(Keys.web.forwardSlashCharacter, "/"); settings.overrideSetting(Keys.web.forwardSlashCharacter, "/");

repositoryMngr = mock(IRepositoryManager.class);
rawServlet = new RawServlet(mockRuntimeManager, repositoryMngr);
} }




public void getPath() public void getPath()
{ {
} }

@Test
public void getBranch_Repo()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn("/test.git/");

String branch = rawServlet.getBranch("test.git", request);

assertEquals("Branch was supposed to be empty as no branch was given.", "", branch);
}

@Test
public void getBranch_RepoNoTrailingSlash()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn("/test.git");

String branch = rawServlet.getBranch("test.git", request);

assertEquals("Branch was supposed to be empty as no branch was given.", "", branch);
}

@Test
public void getBranch_PiNull()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn(null);

String branch = rawServlet.getBranch("test.git", request);

assertEquals("Branch was supposed to be empty as path info is null.", "", branch);
}


@Test
public void getBranch_PiEmpty()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn("");

String branch = rawServlet.getBranch("test.git", request);

assertEquals("Branch was supposed to be empty as no path info exists.", "", branch);
}






@Test
public void getPath_Repo()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn("/test.git/");

String path = rawServlet.getPath("test.git", "", request);

assertEquals("Path was supposed to be empty as no path was given.", "", path);
}

@Test
public void getPath_RepoNoTrailingSlash()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn("/test.git");

String path = rawServlet.getPath("test.git", "", request);

assertEquals("Path was supposed to be empty as no path was given.", "", path);
}

@Test
public void getPath_PiNull()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn(null);

String path = rawServlet.getPath("test.git", "", request);

assertEquals("Path was supposed to be empty as path info is null.", "", path);
}


@Test
public void getPath_PiEmpty()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getPathInfo()).thenReturn("");

String path = rawServlet.getPath("test.git", "", request);

assertEquals("Path was supposed to be empty as no path info exists.", "", path);
}

} }

Loading…
Cancel
Save