use of spark.Request in project spark by perwendel.
the class FilterExample method main.
public static void main(String[] args) {
usernamePasswords.put("foo", "bar");
usernamePasswords.put("admin", "admin");
before(new Filter() {
@Override
public void handle(Request request, Response response) {
String user = request.queryParams("user");
String password = request.queryParams("password");
String dbPassword = usernamePasswords.get(user);
if (!(password != null && password.equals(dbPassword))) {
halt(401, "You are not welcome here!!!");
}
}
});
before("/hello", (request, response) -> {
response.header("Foo", "Set by second before filter");
});
get("/hello", (request, response) -> {
return "Hello World!";
});
after("/hello", (request, response) -> {
response.header("spark", "added by after-filter");
});
}
use of spark.Request in project searchcode-server by boyter.
the class CodeRouteServiceTest method testHtmlNoQueryString.
public void testHtmlNoQueryString() {
CodeRouteService codeRouteService = new CodeRouteService();
Request request = Mockito.mock(Request.class);
ModelAndView modelAndView = codeRouteService.html(request, null);
Map<String, Object> model = (Map<String, Object>) modelAndView.getModel();
String viewName = modelAndView.getViewName();
assertThat(model.get("photoId")).isInstanceOf(Integer.class);
assertThat((int) model.get("photoId")).isGreaterThanOrEqualTo(0);
assertThat((int) model.get("photoId")).isLessThanOrEqualTo(4);
assertThat(model.get("numDocs")).isInstanceOf(Integer.class);
assertThat((int) model.get("numDocs")).isGreaterThanOrEqualTo(0);
assertThat(model.get("logoImage")).isNotNull();
assertThat(model.get("isCommunity")).isEqualTo(App.ISCOMMUNITY);
assertThat(viewName).isEqualTo("index.ftl");
}
use of spark.Request in project searchcode-server by boyter.
the class ApiRouteServiceTest method testGetIndexTime.
public void testGetIndexTime() {
Request mockRequest = mock(Request.class);
Repo repoMock = mock(Repo.class);
when(mockRequest.queryParams("reponame")).thenReturn("somename");
when(mockRequest.queryParams()).thenReturn((new HashMap<String, String>() {
{
put("reponame", "reponame");
}
}).keySet());
when(repoMock.getRepoByName("somename")).thenReturn(new RepoResult(0, "name", "scm", "url", "username", "password", "source", "branch", "\n" + "{\"rowId\":1,\"name\":\"test\",\"scm\":\"git\",\"url\":\"/test/\",\"username\":\"\",\"password\":\"\",\"source\":\"\",\"branch\":\"master\",\"data\":{\"averageIndexTimeSeconds\":9,\"indexStatus\":\"success\",\"jobRunTime\":{\"seconds\":1496356541,\"nanos\":188000000}}}"));
ApiRouteService apiRouteService = new ApiRouteService(null, null, repoMock, null, null);
String averageIndexTimeSeconds = apiRouteService.getIndexTime(mockRequest, null);
assertThat(averageIndexTimeSeconds).isEqualTo("0 seconds ago");
}
use of spark.Request in project searchcode-server by boyter.
the class ApiRouteServiceTest method testRepoAddNoAuthSucessful.
public void testRepoAddNoAuthSucessful() {
Request mockRequest = Mockito.mock(Request.class);
Repo mockRepo = Mockito.mock(Repo.class);
ValidatorService mockValidatorService = Mockito.mock(ValidatorService.class);
when(mockValidatorService.validate(any())).thenReturn(new ValidatorResult(true, ""));
ApiRouteService apiRouteService = new ApiRouteService(null, null, mockRepo, null, mockValidatorService);
apiRouteService.apiEnabled = true;
apiRouteService.apiAuth = false;
when(mockRequest.queryParams("reponame")).thenReturn("test");
when(mockRequest.queryParams("repourl")).thenReturn("test");
when(mockRequest.queryParams("repotype")).thenReturn("test");
when(mockRequest.queryParams("repousername")).thenReturn("test");
when(mockRequest.queryParams("repopassword")).thenReturn("test");
when(mockRequest.queryParams("reposource")).thenReturn("test");
when(mockRequest.queryParams("repobranch")).thenReturn("test");
ApiResponse apiResponse = apiRouteService.repoAdd(mockRequest, null);
assertThat(apiResponse.getMessage()).isEqualTo("added repository successfully");
assertThat(apiResponse.isSucessful()).isTrue();
verify(mockRepo, times(1)).saveRepo(Matchers.<RepoResult>anyObject());
}
use of spark.Request in project searchcode-server by boyter.
the class CodeRouteServiceTest method testGetCodeWithParamsWithMatch.
public void testGetCodeWithParamsWithMatch() {
Request request = Mockito.mock(Request.class);
Response response = Mockito.mock(Response.class);
CodeSearcher codeSearcher = Mockito.mock(CodeSearcher.class);
CodeRouteService codeRouteService = new CodeRouteService(codeSearcher);
CodeResult codeResult = new CodeResult(new ArrayList<String>(), new ArrayList<CodeMatchResult>());
codeResult.setCodeLines("100");
codeResult.setLanguageName("LanguageName");
codeResult.setMd5hash("md5hash");
codeResult.setRepoName("myRepo");
codeResult.setRepoLocation("repoLocation");
codeResult.setCodeOwner("codeOwner");
when(request.params(":codeid")).thenReturn("MATCH-MOCK");
when(codeSearcher.getByCodeId("MATCH-MOCK")).thenReturn(codeResult);
Map<String, Object> map = codeRouteService.getCode(request, response);
assertThat(map.get("codePath")).isEqualTo("/");
assertThat(map.get("codeLength")).isEqualTo("100");
assertThat(map.get("languageName")).isEqualTo("LanguageName");
assertThat(map.get("md5Hash")).isEqualTo("md5hash");
assertThat(map.get("repoName")).isEqualTo("myRepo");
assertThat(map.get("highlight")).isEqualTo(true);
assertThat(map.get("repoLocation")).isEqualTo("repoLocation");
assertThat(map.get("codeValue")).isEqualTo("");
assertThat(map.get("highligher")).isNotNull();
assertThat(map.get("codeOwner")).isEqualTo("codeOwner");
assertThat(map.get("owaspResults")).isNotNull();
assertThat(map.get("logoImage")).isNotNull();
assertThat(map.get("isCommunity")).isEqualTo(App.ISCOMMUNITY);
assertThat(map.get("estimatedCost")).isNull();
}
Aggregations