You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GlobalResourceHandlerTest.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.vaadin.server;
  2. import java.io.IOException;
  3. import com.vaadin.tests.util.MockUI;
  4. import com.vaadin.ui.LegacyComponent;
  5. import com.vaadin.ui.UI;
  6. import org.junit.Test;
  7. import static org.hamcrest.CoreMatchers.endsWith;
  8. import static org.junit.Assert.assertThat;
  9. import static org.junit.Assert.assertTrue;
  10. import static org.mockito.Matchers.anyInt;
  11. import static org.mockito.Mockito.mock;
  12. import static org.mockito.Mockito.verify;
  13. import static org.mockito.Mockito.when;
  14. public class GlobalResourceHandlerTest {
  15. @Test
  16. public void globalResourceHandlerShouldWorkWithEncodedFilename()
  17. throws IOException {
  18. assertEncodedFilenameIsHandled("simple.txt", "simple.txt");
  19. assertEncodedFilenameIsHandled("with spaces.txt", "with+spaces.txt");
  20. assertEncodedFilenameIsHandled("with # hash.txt", "with+%23+hash.txt");
  21. assertEncodedFilenameIsHandled("with ; semicolon.txt",
  22. "with+%3B+semicolon.txt");
  23. assertEncodedFilenameIsHandled("with , comma.txt",
  24. "with+%2C+comma.txt");
  25. // ResourceReference.encodeFileName does not encode slashes and
  26. // backslashes
  27. // See comment inside2 method for more details
  28. assertEncodedFilenameIsHandled("with \\ backslash.txt",
  29. "with+\\+backslash.txt");
  30. assertEncodedFilenameIsHandled("with / slash.txt", "with+/+slash.txt");
  31. }
  32. private void assertEncodedFilenameIsHandled(String filename,
  33. String expectedFilename) throws IOException {
  34. DownloadStream stream = mock(DownloadStream.class);
  35. ConnectorResource resource = mock(ConnectorResource.class);
  36. when(resource.getFilename()).thenReturn(filename);
  37. when(resource.getStream()).thenReturn(stream);
  38. UI ui = new MockUI() {
  39. @Override
  40. public int getUIId() {
  41. return 0;
  42. }
  43. };
  44. ClientConnector connector = mock(LegacyComponent.class);
  45. when(connector.getUI()).thenReturn(ui);
  46. GlobalResourceHandler handler = new GlobalResourceHandler();
  47. handler.register(resource, connector);
  48. // Verify that file name has been encoded
  49. String uri = handler.getUri(connector, resource);
  50. assertThat(uri, endsWith("/" + expectedFilename));
  51. VaadinSession session = mock(VaadinSession.class);
  52. VaadinRequest request = mock(VaadinRequest.class);
  53. VaadinResponse response = mock(VaadinResponse.class);
  54. // getPathInfo return path decoded but without decoding plus as spaces
  55. when(request.getPathInfo()).thenReturn(
  56. "APP/global/0/legacy/0/" + filename.replace(" ", "+"));
  57. when(session.getUIById(anyInt())).thenReturn(ui);
  58. // Verify that decoded path info is correctly handled
  59. assertTrue("Request not handled",
  60. handler.handleRequest(session, request, response));
  61. verify(stream).writeResponse(request, response);
  62. }
  63. }