Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the output of the SMTPSampler #2395

Closed
asfimport opened this issue Sep 2, 2010 · 7 comments
Closed

Improve the output of the SMTPSampler #2395

asfimport opened this issue Sep 2, 2010 · 7 comments

Comments

@asfimport
Copy link
Collaborator

Luciana Moreira (Bug 49862):
The patch attached improves the output generated by the SMTP Sampler.

During tests I found it very handy to have a more detailed information about the message sent to the server.

So far I only tested it with messages that contain a message body and do not contain an attachment. The output looks like:

------------ Message Headers ------------
Message-Id: <689649240.25.1283425759858.JavaMail.moreira@pandora>
MIME-Version: 1.0
Sensitivity: company-confidential
From: tester@myserver.com
Reply-to: tester@myserver.com
To: jmeter-tester@anotherserver.com

------------ Message Content ------------
Content-Type: multipart/mixed;
boundary="----=_Part_24_1513386444.1283425759857"
Content-Transfer-Encoding: 7bit
ContentType = text/plain; charset=us-ascii

The message text comes in here
blah blah blah
blah blah blah
blah blah blah
and ends here

Request Headers:

I don't know why the "Request Headers:" keeps showing but it is independent from my code.

OS: Linux

@asfimport
Copy link
Collaborator Author

Luciana Moreira (migrated from Bugzilla):
Created attachment clipboard.txt: Patch to for a nicer presentation of the request on SMTPSampler

clipboard.txt
### Eclipse Workspace Patch 1.0
#P jmeterSVN
Index: src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java
===================================================================
--- src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java	(revision 991618)
+++ src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java	(working copy)
@@ -23,11 +23,15 @@
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.Enumeration;
 import java.util.List;
 
 import javax.mail.AuthenticationFailedException;
+import javax.mail.BodyPart;
+import javax.mail.Header;
 import javax.mail.Message;
 import javax.mail.MessagingException;
+import javax.mail.Multipart;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 
@@ -206,11 +210,8 @@
         try {
             instance.execute(message);
 
-            // Set up the sample result details
-            res.setSamplerData(
-                    "To: " + receiverTo
-                    + "\nCC: " + receiverCC
-                    + "\nBCC: " + receiverBcc);
+            // Set up the sample result details            
+            res.setSamplerData(getSamplerData(message));
             res.setDataType(SampleResult.TEXT);
             res.setResponseCodeOK();
             /*
@@ -282,7 +283,104 @@
         return res;
     }
 
-    /**
+    private String getSamplerData(Message message) throws MessagingException, IOException {
+    	StringBuffer sb = new StringBuffer();
+        
+        sb.append("------------ Message Headers ------------\n");
+        Enumeration headers = message.getAllHeaders();
+        writeHeaders(headers, sb);
+        String from = InternetAddress.toString(message.getFrom());
+        if (from != null) {
+        	sb.append("From: ");
+        	sb.append(from);
+        	sb.append("\n");
+        }
+
+        String replyTo = InternetAddress.toString(message.getReplyTo());
+        if (replyTo != null) {
+        	sb.append("Reply-to: ");
+        	sb.append(replyTo);
+        	sb.append("\n");
+        }
+        String to = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
+        if (to != null) {
+        	sb.append("To: ");
+        	sb.append(to);
+        	sb.append("\n");
+        }
+
+        String subject = message.getSubject();
+        if (subject != null) {
+        	sb.append("Subject: ");
+        	sb.append(subject);
+        	sb.append("\n");
+        }
+        sb.append("\n------------ Message Content ------------\n");
+        Object content = message.getContent();
+        if (content instanceof Multipart) {
+        	Multipart multipart = (Multipart) content;
+        	sb.append("Content-Type: ");
+        	sb.append(multipart.getContentType());
+        	sb.append("\n");
+        	for (int i = 0; i < multipart.getCount(); i++) {        		
+                BodyPart bodyPart = multipart.getBodyPart(i);
+                writeBodyPart(sb, bodyPart);
+            }
+		} else if(content instanceof BodyPart){
+			BodyPart bodyPart = (BodyPart) content;
+			writeBodyPart(sb, bodyPart);
+		}        
+		return sb.toString();
+	}
+
+	private void writeHeaders(Enumeration<Header> headers, StringBuffer sb)
+			throws MessagingException {
+        while (headers.hasMoreElements()) {
+        	Header header = headers.nextElement();
+        	if("From".equals(header.getName())
+        		|| "Reply-to".equals(header.getName())
+        		|| "To".equals(header.getName())
+        		|| "Subject".equals(header.getName())
+        		|| "Content-Type".equals(header.getName())){        		
+        		continue;
+        	}
+        	sb.append(header.getName());
+        	sb.append(": ");
+        	sb.append(header.getValue());
+        	sb.append("\n");
+        }
+	}
+
+	private void writeBodyPart(StringBuffer sb, BodyPart bodyPart)
+			throws MessagingException, IOException {
+		writeHeaders(bodyPart.getAllHeaders(), sb);
+		sb.append("ContentType = ");
+		sb.append(bodyPart.getContentType());
+		sb.append("\n");
+		String description = bodyPart.getDescription();
+		if(description != null){
+			sb.append("Description = ");
+			sb.append(description);
+			sb.append("\n");
+		}
+		
+		String disposition = bodyPart.getDisposition();
+		
+		if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
+			sb.append("\n Attachment - ");
+			sb.append("Disposition = ");
+			sb.append(disposition);		    
+		    sb.append("; FileName = ");
+		    sb.append(bodyPart.getFileName());    
+		    sb.append("; FileSize = ");
+		    sb.append(bodyPart.getSize());
+		} else {
+        	sb.append("\n");
+		    sb.append(bodyPart.getContent());
+		}
+	}
+
+	/**
      * Get the list of addresses or null.
      * Null is treated differently from an empty list.
      * @param propValue addresses separated by ";"

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Thanks, good patch.

However, I wonder why the From:/Reply-To: etc. headers are treated specially - would it not be better to use the actual headers?

Also, there were quite a few tabs in the patch - please could you use spaces instead in future? [Eclipse has a setting for this.]

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Applied to SVN:

URL: http://svn.apache.org/viewvc?rev=992129&view=rev
Log:
#2395 - Improve SMTPSampler Request output.

Modified:
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java
jakarta/jmeter/trunk/xdocs/changes.xml

I made a few changes:

  • split output into samplerData and RequestHeaders
  • moved setup so even failed samples show the request data
  • eliminated duplication of header output

@asfimport
Copy link
Collaborator Author

Luciana Moreira (migrated from Bugzilla):
Hello Sebb,

I will configure Eclipse to avoid the tab problem :-)

I treated the From:/Reply-TO: etc differently only to be able to display them together after all other tags. I only did this because otherwise it is a bit annoying to try to find this information in the middle of all the other tags.

But I can live with them being displayed out of order.

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
OK, I see.

Could still sort the fields if the ordering proves to be a problem.

@asfimport
Copy link
Collaborator Author

Luciana Moreira (migrated from Bugzilla):
After looking into the improvements you made in the code. I must say I like it.

The only thing that I believe should be changed is moving the Request Headers to the top. I saw that you separated the sample result into the body and the headers, and I think it is right!

I just find it a bit odd to have the headers at the bottom...

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
The headers are at the bottom because that's the way they are shown for HTTP(S) requests.

Could potentially change to having them at the top, but then one would need to add markers to show where the body starts and ends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant