Source:
<code>public void testAnonymousDownload() throws Exception { String repositoryName = "test"; String baseUrl = "http://localhost:8080/nuxeo/"; Framework.getProperties().setProperty("nuxeo.url", baseUrl); <START> CoreSession session = CoreInstance.openCoreSession(repositoryName); <END> DocumentModel doc = session.createDocumentModel("/", "MyDoc", "File"); String blobValue = "Hello World"; Blob blob = Blobs.createBlob(blobValue); String blobFilename = "blob.txt"; blob.setFilename(blobFilename); BlobHolder bh = doc.getAdapter(BlobHolder.class); bh.setBlob(blob); doc = session.createDocument(doc); session.save(); String path = "nxfile/" + repositoryName + "/" + doc.getId() + "/blobholder:0/" + blobFilename; ByteArrayOutputStream out = new ByteArrayOutputStream(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn("GET"); HttpServletResponse response = mock(HttpServletResponse.class); ServletOutputStream sos = new ServletOutputStream() { @Override public void write(int b) throws IOException { out.write(b); } }; @SuppressWarnings("resource") PrintWriter printWriter = new PrintWriter(sos); when(response.getOutputStream()).thenReturn(sos); when(response.getWriter()).thenReturn(printWriter); NuxeoPrincipal anonymous = new UserPrincipal("johndoe", null, true, false); LoginStack loginStack = ClientLoginModule.getThreadLocalLogin(); loginStack.push(anonymous, null, null); try { downloadService.handleDownload(new TestHttpServletRequestWrapper(request, anonymous), response, baseUrl, path); fail("The user has to authenticate before downloading the blob"); } catch (IOException e) { assertEquals("Authentication is needed for downloading the blob", e.getCause().getMessage()); NuxeoPrincipal principal = new UserPrincipal("johnnotdoe", Collections.singletonList("members"), false, false); loginStack.push(principal, null, null); try { downloadService.handleDownload(new TestHttpServletRequestWrapper(request, principal), response, baseUrl, path); assertEquals(blobValue, out.toString()); } catch (IOException ioe) { fail("The user should be able to download the blob"); } finally { loginStack.pop(); } } finally { loginStack.pop(); session.close(); } }</code><technical_language>injected CoreSession</technical_language>

Target:
public void testAnonymousDownload() throws Exception { String repositoryName = "test"; String baseUrl = "http://localhost:8080/nuxeo/"; Framework.getProperties().setProperty("nuxeo.url", baseUrl); DocumentModel doc = session.createDocumentModel("/", "MyDoc", "File"); String blobValue = "Hello World"; Blob blob = Blobs.createBlob(blobValue); String blobFilename = "blob.txt"; blob.setFilename(blobFilename); BlobHolder bh = doc.getAdapter(BlobHolder.class); bh.setBlob(blob); doc = session.createDocument(doc); session.save(); String path = "nxfile/" + repositoryName + "/" + doc.getId() + "/blobholder:0/" + blobFilename; ByteArrayOutputStream out = new ByteArrayOutputStream(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn("GET"); HttpServletResponse response = mock(HttpServletResponse.class); ServletOutputStream sos = new ServletOutputStream() { @Override public void write(int b) throws IOException { out.write(b); } }; @SuppressWarnings("resource") PrintWriter printWriter = new PrintWriter(sos); when(response.getOutputStream()).thenReturn(sos); when(response.getWriter()).thenReturn(printWriter); NuxeoPrincipal anonymous = new UserPrincipal("johndoe", null, true, false); LoginStack loginStack = ClientLoginModule.getThreadLocalLogin(); loginStack.push(anonymous, null, null); try { downloadService.handleDownload(new TestHttpServletRequestWrapper(request, anonymous), response, baseUrl, path); fail("The user has to authenticate before downloading the blob"); } catch (IOException e) { assertEquals("Authentication is needed for downloading the blob", e.getCause().getMessage()); NuxeoPrincipal principal = new UserPrincipal("johnnotdoe", Collections.singletonList("members"), false, false); loginStack.push(principal, null, null); try { downloadService.handleDownload(new TestHttpServletRequestWrapper(request, principal), response, baseUrl, path); assertEquals(blobValue, out.toString()); } catch (IOException ioe) { fail("The user should be able to download the blob"); } finally { loginStack.pop(); } } finally { loginStack.pop(); } }