james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [04/17] james-project git commit: JAMES-2411 Content-Length should be added
Date Tue, 12 Jun 2018 00:03:23 GMT
JAMES-2411 Content-Length should be added


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/bfb5d147
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/bfb5d147
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/bfb5d147

Branch: refs/heads/master
Commit: bfb5d14705c40482a64951b00b32da32051929eb
Parents: 8b1f6fb
Author: benwa <btellier@linagora.com>
Authored: Thu Jun 7 08:41:37 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Jun 12 07:00:47 2018 +0700

----------------------------------------------------------------------
 .../apache/james/webadmin/WebAdminUtils.java    |  7 +-----
 .../webadmin/routes/MailRepositoriesRoutes.java |  8 ++++++
 .../routes/MailRepositoriesRoutesTest.java      | 26 +++++++++++++-------
 3 files changed, 26 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/bfb5d147/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
index 7f294ad..6e5a717 100644
--- a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
+++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
@@ -35,7 +35,7 @@ import com.jayway.restassured.http.ContentType;
 
 public class WebAdminUtils {
 
-    public static WebAdminServer createWebAdminServer(MetricFactory metricFactory, Routes...
routes) throws IOException {
+    public static WebAdminServer createWebAdminServer(MetricFactory metricFactory, Routes...
routes) {
         return new WebAdminServer(WebAdminConfiguration.TEST_CONFIGURATION,
             ImmutableSet.copyOf(routes),
             new NoAuthenticationFilter(),
@@ -53,9 +53,4 @@ public class WebAdminUtils {
             .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
             .setPort(port.getValue());
     }
-
-    public static RequestSpecBuilder buildRequestSpecificationWithPortOnly(Port port) {
-        return new RequestSpecBuilder()
-            .setPort(port.getValue());
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/bfb5d147/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
index 9bd7f1d..c9f07cc 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
@@ -19,6 +19,7 @@
 
 package org.apache.james.webadmin.routes;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
@@ -197,10 +198,17 @@ public class MailRepositoriesRoutes implements Routes {
 
     private Object writeMimeMessage(MimeMessage mimeMessage, HttpServletResponse rawResponse)
throws MessagingException, IOException {
         rawResponse.setContentType(Constants.RFC822_CONTENT_TYPE);
+        rawResponse.setHeader("Content-Length", String.valueOf(computeExactSize(mimeMessage)));
         mimeMessage.writeTo(rawResponse.getOutputStream());
         return rawResponse;
     }
 
+    private long computeExactSize(MimeMessage mimeMessage) throws IOException, MessagingException
{
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        mimeMessage.writeTo(byteArrayOutputStream);
+        return byteArrayOutputStream.size();
+    }
+
     private MimeMessage getMailAsMimeMessage(String url, String mailKey) {
         try {
             return repositoryStoreService.retrieveMessage(url, mailKey)

http://git-wip-us.apache.org/repos/asf/james-project/blob/bfb5d147/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
index 6e7d6fb..e7b57fb 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
@@ -35,6 +35,7 @@ import static org.hamcrest.Matchers.notNullValue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Optional;
 
@@ -66,6 +67,7 @@ import org.junit.Test;
 
 import com.google.common.collect.ImmutableList;
 import com.jayway.restassured.RestAssured;
+import com.jayway.restassured.builder.RequestSpecBuilder;
 import com.jayway.restassured.http.ContentType;
 import com.jayway.restassured.parsing.Parser;
 
@@ -470,10 +472,10 @@ public class MailRepositoriesRoutesTest {
 
     @Test
     public void downloadingAMailShouldReturnTheEml() throws Exception {
-        RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecificationWithPortOnly(webAdminServer.getPort())
+        RestAssured.requestSpecification = new RequestSpecBuilder().setPort(webAdminServer.getPort().getValue())
                 .setBasePath(MailRepositoriesRoutes.MAIL_REPOSITORIES)
                 .build();
-        RestAssured.registerParser(Constants.RFC822_CONTENT_TYPE, Parser.JSON);
+        RestAssured.registerParser(Constants.RFC822_CONTENT_TYPE, Parser.TEXT);
 
         when(mailRepositoryStore.get(URL_MY_REPO)).thenReturn(Optional.of(mailRepository));
 
@@ -484,23 +486,29 @@ public class MailRepositoriesRoutesTest {
             .build();
         mailRepository.store(mail);
 
-        String expectedContent = ClassLoaderUtils.getSystemResourceAsString("mail.eml");
-        given()
+        String expectedContent = ClassLoaderUtils.getSystemResourceAsString("mail.eml", StandardCharsets.UTF_8);
+
+        String actualContent = given()
             .accept(Constants.RFC822_CONTENT_TYPE)
         .when()
             .get(URL_ESCAPED_MY_REPO + "/mails/" + name)
         .then()
             .statusCode(HttpStatus.OK_200)
+            .header("Content-Length", "471")
             .contentType(Constants.RFC822_CONTENT_TYPE)
-            .content(is(expectedContent));
+            .extract()
+            .body()
+            .asString();
+
+        assertThat(actualContent).isEqualToNormalizingNewlines(expectedContent);
     }
 
     @Test
     public void downloadingAMailShouldFailWhenUnknown() throws Exception {
-        RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecificationWithPortOnly(webAdminServer.getPort())
-                .setBasePath(MailRepositoriesRoutes.MAIL_REPOSITORIES)
-                .build();
-        RestAssured.registerParser(Constants.RFC822_CONTENT_TYPE, Parser.JSON);
+        RestAssured.requestSpecification = new RequestSpecBuilder().setPort(webAdminServer.getPort().getValue())
+            .setBasePath(MailRepositoriesRoutes.MAIL_REPOSITORIES)
+            .build();
+        RestAssured.registerParser(Constants.RFC822_CONTENT_TYPE, Parser.TEXT);
 
         when(mailRepositoryStore.get(URL_MY_REPO)).thenReturn(Optional.of(mailRepository));
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message