Search in sources :

Example 31 with Request

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");
    });
}
Also used : Response(spark.Response) Filter(spark.Filter) Request(spark.Request)

Example 32 with Request

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");
}
Also used : CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request) ModelAndView(spark.ModelAndView) Map(java.util.Map)

Example 33 with Request

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");
}
Also used : Repo(com.searchcode.app.dao.Repo) ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Request(spark.Request) RepoResult(com.searchcode.app.model.RepoResult)

Example 34 with Request

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());
}
Also used : Repo(com.searchcode.app.dao.Repo) ApiRouteService(com.searchcode.app.service.route.ApiRouteService) ValidatorResult(com.searchcode.app.model.ValidatorResult) Request(spark.Request) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Example 35 with Request

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();
}
Also used : Response(spark.Response) CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Aggregations

Request (spark.Request)57 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)34 RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)32 Repo (com.searchcode.app.dao.Repo)28 ApiResponse (com.searchcode.app.dto.api.ApiResponse)27 RepoResult (com.searchcode.app.model.RepoResult)10 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)8 Response (spark.Response)8 AdminRouteService (com.searchcode.app.service.route.AdminRouteService)7 HashSet (java.util.HashSet)6 Map (java.util.Map)5 ModelAndView (spark.ModelAndView)5 RouteMatch (spark.routematch.RouteMatch)4 ValidatorResult (com.searchcode.app.model.ValidatorResult)3 UniqueRepoQueue (com.searchcode.app.util.UniqueRepoQueue)3 FilterImpl (spark.FilterImpl)3 HaltException (spark.HaltException)3 Values (com.searchcode.app.config.Values)2 Data (com.searchcode.app.dao.Data)2 CodeResult (com.searchcode.app.dto.CodeResult)2