Java Code Examples for org.springframework.mock.web.MockHttpServletRequest
The following are 21 Jave code examples that show how to use the org.springframework.mock.web.MockHttpServletRequest class. These examples are extracted from open source projects. You can click to vote up the examples you like. Your votes will be used in an intelligent system to get more and better code examples. Thanks!
Example 1
From project alfresco, under directory /root/projects/remote-api/source/java/org/alfresco/repo/webdav/, in source file WebDAVHelperTest.java
@Test public void canGetUrlPathPrefixFromServletPath(){ davHelper=new WebDAVHelper("",serviceRegistry,authService,tenantService); MockHttpServletRequest request=new MockHttpServletRequest("GET","/before/the-servlet/folder/filename.txt"); request.setServletPath("/the-servlet"); String prefix=davHelper.getUrlPathPrefix(request); assertEquals("/before/the-servlet/",prefix); }
Example 2
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CardFormControllerTest.java
/** * Test save card. * @throws Exception the exception */ @Test public void testSaveCard() throws Exception { final MockHttpServletRequest request=this.newPost("/cardform"); request.setRemoteUser("admin"); final BindingResult errors=new DataBinder(this.alphaCard).getBindingResult(); final String view=this.c.saveCard(this.alphaCard,errors,request,new MockHttpServletResponse()); Assert.assertEquals("redirect:/caseform?activeCardId=" + CardFormControllerTest.CARD_ID + "&caseId="+ CardFormControllerTest.CASE_ID,view); final MockHttpServletRequest request2=this.newPost("/cardform"); request2.setRemoteUser("admin"); final AlphaCardIdentifier identifier=new AlphaCardIdentifier(CardFormControllerTest.CASE_ID); final AlphaCard alphaCard2=new AlphaCard(); alphaCard2.setAlphaCardIdentifier(identifier); this.c.saveCard(alphaCard2,null,request2,new MockHttpServletResponse()); }
Example 3
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CaseFormControllerTest.java
/** * Test new. * @throws Exception the exception */ @Test public void testNew() throws Exception { final MockHttpServletRequest request=this.newGet("/caseform"); request.setRemoteUser("admin"); final HttpServletResponse response=new MockHttpServletResponse(); final ModelAndView mv=this.form.showForm(this.filters,request,response); Assert.assertNotNull(mv); Assert.assertEquals("caseform",mv.getViewName()); Assert.assertEquals(new AlphaCase(),mv.getModel().get("case")); }
Example 4
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CaseFormControllerTest.java
/** * Test last. * @throws Exception the exception */ @Test public void testLast() throws Exception { MockHttpServletRequest request=this.newGet("/caseform"); request.setParameter("caseId","550e4713-e22b-11d4-a716-446655440002"); request.setRemoteUser("admin"); ModelAndView mv=this.form.showForm(this.filters,request,new MockHttpServletResponse()); request=this.newGet("/caseform"); request.setParameter("caseId","last"); request.setRemoteUser("admin"); final AlphaCase aCase=this.caseManager.get("550e4713-e22b-11d4-a716-446655440002"); mv=this.form.showForm(this.filters,request,new MockHttpServletResponse()); Assert.assertEquals("caseform",mv.getViewName()); Assert.assertEquals(aCase,mv.getModel().get("case")); }
Example 5
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file ContributorRoleControllerTest.java
/** * Test basic page call. * @throws Exception the exception */ @SuppressWarnings("unchecked") @Test public void testBasicPage() throws Exception { final MockHttpServletRequest request=this.newGet("/contributorRole"); request.setRemoteUser(this.testUserName); final int numberOfRoles=this.contributorRoleManager.getAll().size(); Assert.assertTrue("No roles to test!",numberOfRoles > 0); final ModelAndView result=this.ctrl.showPage(request); final Map<String,Object> resModel=result.getModel(); Assert.assertTrue(resModel.containsKey("contributorRolesList")); final Object contribListObj=resModel.get("contributorRolesList"); final List<ContributorRole> contribList=(List<ContributorRole>)contribListObj; Assert.assertEquals(numberOfRoles,contribList.size()); }
Example 6
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file PasswordHintControllerTest.java
/** * Test execute. * @throws Exception the exception */ @Test public void testExecute() throws Exception { final MockHttpServletRequest request=this.newGet("/passwordHint.html"); request.addParameter("username","user"); final Wiser wiser=new Wiser(); wiser.setPort(this.getSmtpPort()); wiser.start(); this.c.handleRequest(request); wiser.stop(); Assert.assertTrue(wiser.getMessages().size() == 1); Assert.assertNotNull(request.getSession().getAttribute(BaseFormController.MESSAGES_KEY)); }
Example 7
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/filter/, in source file LocaleFilterTest.java
/** * Test set locale in session when session is null. * @throws Exception the exception */ public void testSetLocaleInSessionWhenSessionIsNull() throws Exception { final MockHttpServletRequest request=new MockHttpServletRequest(); request.addParameter("locale","es"); final MockHttpServletResponse response=new MockHttpServletResponse(); this.filter.doFilter(request,response,new MockFilterChain()); Assert.assertNull(request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY)); Assert.assertNotNull(LocaleContextHolder.getLocale()); }
Example 8
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/filter/, in source file LocaleFilterTest.java
/** * Test set locale in session when session not null. * @throws Exception the exception */ public void testSetLocaleInSessionWhenSessionNotNull() throws Exception { final MockHttpServletRequest request=new MockHttpServletRequest(); request.addParameter("locale","es"); final MockHttpServletResponse response=new MockHttpServletResponse(); request.setSession(new MockHttpSession(null)); this.filter.doFilter(request,response,new MockFilterChain()); final Locale locale=(Locale)request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY); Assert.assertNotNull(locale); Assert.assertNotNull(LocaleContextHolder.getLocale()); Assert.assertEquals(new Locale("es"),locale); }
Example 9
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/filter/, in source file LocaleFilterTest.java
/** * Test set invalid locale. * @throws Exception the exception */ public void testSetInvalidLocale() throws Exception { final MockHttpServletRequest request=new MockHttpServletRequest(); request.addParameter("locale","foo"); final MockHttpServletResponse response=new MockHttpServletResponse(); request.setSession(new MockHttpSession(null)); this.filter.doFilter(request,response,new MockFilterChain()); Assert.assertNotNull(request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY)); }
Example 10
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CardFileUploadControllerTest.java
/** * Test on submit. */ @Test public void testOnSubmit(){ final String caseId="550e4713-e22b-11d4-a716-446655440000"; final String cardId="440e4816-e01b-74d4-a716-449955440092"; final String fileName="doesnotcompute.file"; final String mimeType="text/plain"; final byte[] content="roflcopter".getBytes(); final MockHttpServletRequest request=this.newGet("/cardfileupload"); request.setRemoteUser("admin"); request.addParameter("case",caseId); request.addParameter("card",cardId); final FileUpload fileUpload=this.ctrl.showForm(request); fileUpload.setFile(content); final MockMultipartHttpServletRequest upload=new MockMultipartHttpServletRequest(); upload.setRemoteUser("admin"); final MockMultipartFile file=new MockMultipartFile("file",fileName,mimeType,content); upload.addFile(file); upload.addParameter("case",caseId); upload.addParameter("card",cardId); }
Example 11
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CardFileUploadControllerTest.java
/** * Test on cancel. */ @Test public void testOnCancel(){ final String caseId="550e4713-e22b-11d4-a716-446655440000"; final String cardId="440e4816-e01b-74d4-a716-449955440092"; final String fileName="doesnotcompute.file"; final String mimeType="text/plain"; final byte[] content="roflcopter".getBytes(); final MockHttpServletRequest request=this.newGet("/cardfileupload"); request.setRemoteUser("admin"); request.addParameter("case",caseId); request.addParameter("card",cardId); final FileUpload fileUpload=this.ctrl.showForm(request); fileUpload.setFile(content); final MockMultipartHttpServletRequest upload=new MockMultipartHttpServletRequest(); upload.setRemoteUser("admin"); final MockMultipartFile file=new MockMultipartFile("file",fileName,mimeType,content); upload.addFile(file); upload.addParameter("case",caseId); upload.addParameter("card",cardId); upload.addParameter("cancel","Abbrechen"); final BindingResult errors=new DataBinder(fileUpload).getBindingResult(); String result=""; try { result=this.ctrl.onSubmit(fileUpload,errors,upload); } catch ( final IOException e) { Assert.fail("Should not fail on fail upload"); } Assert.assertFalse(errors.hasErrors()); Assert.assertNull(upload.getSession().getAttribute("successMessages")); Assert.assertEquals("redirect:/caseform?caseId=" + caseId + "&activeCardId="+ cardId,result); }
Example 12
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CardAssignFormControllerTest.java
/** * Test show form. */ @Test public void testShowForm(){ final MockHttpServletRequest request=this.newGet("/cardassignform"); request.setRemoteUser("admin"); request.addParameter("card",""); request.addParameter("case",""); Model m=new ExtendedModelMap(); this.form.showForm(request,m); Assert.assertFalse(m.containsAttribute("users")); request.setParameter("card","440e4816-e01b-74d4-a716-449955440092"); m=new ExtendedModelMap(); this.form.showForm(request,m); Assert.assertFalse(m.containsAttribute("users")); request.setParameter("case","atjaerhe"); m=new ExtendedModelMap(); this.form.showForm(request,m); Assert.assertFalse(m.containsAttribute("users")); request.setParameter("case","550e4713-e22b-11d4-a716-446655440000"); m=new ExtendedModelMap(); this.form.showForm(request,m); final List<UserExtension> l=new LinkedList<UserExtension>(); l.add(this.userExtensionManager.get(-5L)); l.add(this.userExtensionManager.get(-4L)); l.add(this.userExtensionManager.get(-2L)); }
Example 13
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CardAssignFormControllerTest.java
/** * Test on submit. * @throws Exception the exception */ @Test public void testOnSubmit() throws Exception { final String cardId="440e4816-e01b-74d4-a716-449955440092"; final String caseId="550e4713-e22b-11d4-a716-446655440000"; final MockHttpServletRequest request=this.newPost("/cardassignform"); request.setRemoteUser("admin"); request.setParameter("card",cardId); request.setParameter("case",caseId); MockHttpServletResponse response=new MockHttpServletResponse(); request.setParameter("cancel",""); this.form.onSubmit(request,response); response=new MockHttpServletResponse(); request.removeParameter("cancel"); request.setParameter("user","-1"); this.form.onSubmit(request,response); boolean found=false; for ( final ContributorRequest r : this.contrReqManager.getAll()) { if (r.getAcceptingUser().getId().equals(-1L) && r.getAlphaCard().getAlphaCardIdentifier().getCardId().equals(cardId) && r.getRequestingUser().getId().equals(-2L)) { found=true; break; } } Assert.assertTrue(found); }
Example 14
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CardFileUploadControllerTest.java
/** * Test zero file. */ @Test public void testZeroFile(){ final String caseId="550e4713-e22b-11d4-a716-446655440000"; final String cardId="440e4816-e01b-74d4-a716-449955440092"; final String fileName="doesnotcompute.file"; final String mimeType="text/plain"; final byte[] content="".getBytes(); final MockHttpServletRequest request=this.newGet("/cardfileupload"); request.setRemoteUser("admin"); request.addParameter("case",caseId); request.addParameter("card",cardId); final FileUpload fileUpload=this.ctrl.showForm(request); fileUpload.setFile(content); final MockMultipartHttpServletRequest upload=new MockMultipartHttpServletRequest(); upload.setRemoteUser("user"); final MockMultipartFile file=new MockMultipartFile("file",fileName,mimeType,content); upload.addFile(file); upload.addParameter("case",caseId); upload.addParameter("card",cardId); final BindingResult errors=new DataBinder(fileUpload).getBindingResult(); String result=""; try { result=this.ctrl.onSubmit(fileUpload,errors,upload); } catch ( final IOException e) { Assert.fail("Should not fail on fail upload"); } Assert.assertTrue(errors.hasErrors()); final List<ObjectError> errorList=errors.getAllErrors(); Assert.assertEquals(1,errorList.size()); Assert.assertEquals("errors.required",errorList.get(0).getCode()); Assert.assertNull(upload.getSession().getAttribute("successMessages")); Assert.assertEquals("redirect:/cardfileupload?card=" + cardId + "&case="+ caseId,result); }
Example 15
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CaseFormControllerTest.java
/** * Test add. * @throws Exception the exception */ @Test public void testAdd() throws Exception { MockHttpServletRequest request=this.newGet("/caseform"); request.setRemoteUser("admin"); final ModelAndView mv=this.form.showForm(this.filters,request,new MockHttpServletResponse()); request=this.newPost("/caseform"); request.setRemoteUser("admin"); final AlphaCase aCase=(AlphaCase)mv.getModel().get("case"); aCase.setName("test case which does not exist yet"); final BindingResult errors=new DataBinder(aCase).getBindingResult(); final String view=this.form.addCase(aCase,errors,request,new MockHttpServletResponse()); final List<AlphaCase> dbCases=this.caseManager.findByName(aCase.getName()); Assert.assertNotNull(dbCases); Assert.assertTrue(dbCases.size() >= 1); final AlphaCase dbCase=dbCases.get(0); Assert.assertNotNull(dbCase); Assert.assertEquals("redirect:/caseform?caseId=" + dbCase.getCaseId(),view); Assert.assertFalse(errors.hasErrors()); Assert.assertNotNull(request.getSession().getAttribute("successMessages")); final Locale locale=request.getLocale(); final ArrayList<Object> msgs=(ArrayList<Object>)request.getSession().getAttribute("successMessages"); Assert.assertTrue(msgs.contains(this.form.getText("case.added",locale))); }
Example 16
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CaseFormControllerTest.java
/** * Test edit. * @throws Exception the exception */ @Test public void testEdit() throws Exception { MockHttpServletRequest request=this.newGet("/caseform"); request.setParameter("caseId",CaseFormControllerTest.caseId); request.setRemoteUser("admin"); final ModelAndView mv=this.form.showForm(this.filters,request,new MockHttpServletResponse()); Assert.assertEquals("caseform",mv.getViewName()); final AlphaCase aCase=(AlphaCase)mv.getModel().get("case"); AlphaCase dbCase=this.caseManager.get(CaseFormControllerTest.caseId); Assert.assertEquals(dbCase,aCase); Assert.assertEquals(dbCase.getAlphaCards(),mv.getModel().get("cards")); Assert.assertEquals(dbCase.getListOfParticipants(),mv.getModel().get("participants")); request=this.newPost("/caseform"); request.setRemoteUser("admin"); aCase.setName("test case with a new name"); final BindingResult errors=new DataBinder(aCase).getBindingResult(); final String view=this.form.saveCase(aCase,errors,request,new MockHttpServletResponse()); Assert.assertEquals("redirect:/caseform?caseId=" + aCase.getCaseId(),view); Assert.assertFalse(errors.hasErrors()); Assert.assertNotNull(request.getSession().getAttribute("successMessages")); final Locale locale=request.getLocale(); final ArrayList<Object> msgs=(ArrayList<Object>)request.getSession().getAttribute("successMessages"); Assert.assertTrue(msgs.contains(this.form.getText("case.updated",locale))); dbCase=this.caseManager.get(CaseFormControllerTest.caseId); }
Example 17
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CaseFormControllerTest.java
/** * Test delete. * @throws Exception the exception */ @Test public void testDelete() throws Exception { MockHttpServletRequest request=this.newGet("/caseform"); request.setParameter("caseId",CaseFormControllerTest.caseId); request.setRemoteUser("admin"); final ModelAndView mv=this.form.showForm(this.filters,request,new MockHttpServletResponse()); final AlphaCase myCase=(AlphaCase)mv.getModel().get("case"); request=this.newPost("/caseform"); request.setRemoteUser("admin"); request.addParameter("delete",""); final BindingResult errors=new DataBinder(myCase).getBindingResult(); final String view=this.form.deleteCase(myCase,errors,request); Assert.assertEquals(this.form.getCancelView(),view); Assert.assertNotNull(request.getSession().getAttribute("successMessages")); final Locale locale=request.getLocale(); final ArrayList<Object> msgs=(ArrayList<Object>)request.getSession().getAttribute("successMessages"); Assert.assertTrue(msgs.contains(this.form.getText("case.deleted",locale))); Assert.assertFalse(this.caseManager.exists(CaseFormControllerTest.caseId)); }
Example 18
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file CaseMenuControllerTest.java
/** * Test handle request. * @throws Exception the exception */ @SuppressWarnings("unchecked") @Test public void testHandleRequest() throws Exception { final MockHttpServletRequest request=this.newGet("/caseMenu"); User u=new User("ichbineintollertesterdenesnochnichtgibt"); u.setEmail("l@m.d"); u.setFirstName("l"); u.setLastName("m"); u.setPassword("123"); u=this.userManager.save(u); AlphaCase c=new AlphaCase(); c.setName("blablabla"); c.addParticipant(u); c=this.caseManager.save(c); request.setRemoteUser("ichbineintollertesterdenesnochnichtgibt"); ModelAndView result=this.ctrl.handleRequest(request); List<AlphaCase> lCases=(List<AlphaCase>)result.getModel().get("caseList"); Assert.assertEquals(1,lCases.size()); final AlphaCase c2=lCases.get(0); Assert.assertEquals(c,c2); this.caseManager.remove(c.getCaseId()); result=this.ctrl.handleRequest(request); lCases=(List<AlphaCase>)result.getModel().get("caseList"); Assert.assertTrue((lCases == null) || lCases.isEmpty()); this.userManager.remove(u.getId()); }
Example 19
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file ContributorRoleControllerTest.java
/** * "Umlaut"-Test. * @throws Exception the exception */ @SuppressWarnings("unchecked") @Test public void testUmlaute() throws Exception { final String newCRName="???'"; MockHttpServletRequest request=this.newPost("/contributorRole"); request.setRemoteUser(this.testUserName); request.addParameter("save_new","button.save"); request.addParameter("newContributorRole",newCRName); final MockHttpServletResponse response=new MockHttpServletResponse(); this.ctrl.saveNew(request,response); Assert.assertTrue(StringUtils.isBlank(response.getErrorMessage())); request=this.newGet("/contributorRole"); request.setRemoteUser(this.testUserName); final ModelAndView result=this.ctrl.showPage(request); final Map<String,Object> resModel=result.getModel(); final Object contribListObj=resModel.get("contributorRolesList"); final List<ContributorRole> contribList=(List<ContributorRole>)contribListObj; boolean contains=false; for (int c=0; (c < contribList.size()) && (contains == false); c++) { if (contribList.get(c).getName().equals(newCRName)) { contains=true; } } Assert.assertTrue("Umlaut-Test failed",contains); }
Example 20
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file ContributorRoleControllerTest.java
/** * Test for get error redirects. * @throws Exception the exception */ @Test public void testGetErrors() throws Exception { MockHttpServletRequest request=this.newPost("/contributorRole"); request.setRemoteUser(this.testUserName); ModelAndView result=this.ctrl.showPage(request); Map<String,Object> resModel=result.getModel(); Assert.assertTrue(resModel.containsKey("contributorRolesList")); request=this.newPost("/contributorRole"); request.setRemoteUser(this.testUserName); request.addParameter("delete","1234567890"); result=this.ctrl.showPage(request); resModel=result.getModel(); Assert.assertTrue(resModel.containsKey("contributorRolesList")); request=this.newPost("/contributorRole"); request.setRemoteUser(this.testUserName); request.addParameter("delete",this.contributorRoleManager.getContributorRoleByName("Radiologe").getContributorRoleId().toString()); request.addParameter("edit","1234567890"); result=this.ctrl.showPage(request); resModel=result.getModel(); request=this.newPost("/contributorRole"); request.setRemoteUser(this.testUserName); request.addParameter("edit","1234567890"); result=this.ctrl.showPage(request); resModel=result.getModel(); Assert.assertFalse(resModel.containsKey("showEditingForm")); Assert.assertFalse(resModel.containsKey("roleToEditId")); Assert.assertFalse(resModel.containsKey("roleToEdit")); }
Example 21
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/controller/, in source file SignupControllerTest.java
/** * Test signup user. * @throws Exception the exception */ @Test public void testSignupUser() throws Exception { final MockHttpServletRequest request=this.newPost("/signup.html"); final Address address=new Address(); address.setCity("Denver"); address.setProvince("Colorado"); address.setCountry("USA"); address.setPostalCode("80210"); final User user=new User(); user.setAddress(address); user.setUsername("self-registered"); user.setPassword("Password1"); user.setConfirmPassword("Password1"); user.setFirstName("First"); user.setLastName("Last"); user.setEmail("self-registered@raibledesigns.com"); user.setWebsite("http://raibledesigns.com"); user.setPasswordHint("Password is one with you."); final HttpServletResponse response=new MockHttpServletResponse(); final Wiser wiser=new Wiser(); wiser.setPort(this.getSmtpPort()); wiser.start(); final BindingResult errors=new DataBinder(user).getBindingResult(); this.c.onSubmit(user,errors,request,response); Assert.assertFalse("errors returned in model",errors.hasErrors()); wiser.stop(); Assert.assertTrue(wiser.getMessages().size() == 1); Assert.assertNotNull(request.getSession().getAttribute("successMessages")); Assert.assertNotNull(request.getSession().getAttribute(Constants.REGISTERED)); SecurityContextHolder.getContext().setAuthentication(null); }