From 6afa358124283bf13a3c43c1763910cf36360ef6 Mon Sep 17 00:00:00 2001 From: Raffael Herrmann Date: Mon, 22 Nov 2021 18:30:48 +0100 Subject: [PATCH] Refactored SMS, MMS & Mail generator. Added more tests and removed empty paramters. --- QRCoder/PayloadGenerator.cs | 90 +++++++++++++-------------- QRCoderTests/PayloadGeneratorTests.cs | 74 ++++++++++++++++------ 2 files changed, 100 insertions(+), 64 deletions(-) diff --git a/QRCoder/PayloadGenerator.cs b/QRCoder/PayloadGenerator.cs index 4a5c796e..cadbb6d4 100644 --- a/QRCoder/PayloadGenerator.cs +++ b/QRCoder/PayloadGenerator.cs @@ -62,32 +62,7 @@ public class Mail : Payload private readonly string mailReceiver, subject, message; private readonly MailEncoding encoding; - /// - /// Creates an empty email payload - /// - /// Receiver's email address - /// Payload encoding type. Choose dependent on your QR Code scanner app. - public Mail(string mailReceiver, MailEncoding encoding = MailEncoding.MAILTO) - { - this.mailReceiver = mailReceiver; - this.subject = this.message = string.Empty; - this.encoding = encoding; - } - - /// - /// Creates an email payload with subject - /// - /// Receiver's email address - /// Subject line of the email - /// Payload encoding type. Choose dependent on your QR Code scanner app. - public Mail(string mailReceiver, string subject, MailEncoding encoding = MailEncoding.MAILTO) - { - this.mailReceiver = mailReceiver; - this.subject = subject; - this.message = string.Empty; - this.encoding = encoding; - } - + /// /// Creates an email payload with subject and message/text /// @@ -95,7 +70,7 @@ public Mail(string mailReceiver, string subject, MailEncoding encoding = MailEnc /// Subject line of the email /// Message content of the email /// Payload encoding type. Choose dependent on your QR Code scanner app. - public Mail(string mailReceiver, string subject, string message, MailEncoding encoding = MailEncoding.MAILTO) + public Mail(string mailReceiver = null, string subject = null, string message = null, MailEncoding encoding = MailEncoding.MAILTO) { this.mailReceiver = mailReceiver; this.subject = subject; @@ -105,20 +80,26 @@ public Mail(string mailReceiver, string subject, string message, MailEncoding en public override string ToString() { + var returnVal = string.Empty; switch (this.encoding) { case MailEncoding.MAILTO: - return - $"mailto:{this.mailReceiver}?subject={System.Uri.EscapeDataString(this.subject)}&body={System.Uri.EscapeDataString(this.message)}"; + var parts = new List(); + if (!string.IsNullOrEmpty(this.subject)) + parts.Add("subject=" + Uri.EscapeDataString(this.subject)); + if (!string.IsNullOrEmpty(this.message)) + parts.Add("body=" + Uri.EscapeDataString(this.message)); + var queryString = parts.Any() ? $"?{string.Join("&", parts.ToArray())}" : ""; + returnVal = $"mailto:{this.mailReceiver}{queryString}"; + break; case MailEncoding.MATMSG: - return - $"MATMSG:TO:{this.mailReceiver};SUB:{EscapeInput(this.subject)};BODY:{EscapeInput(this.message)};;"; + returnVal = $"MATMSG:TO:{this.mailReceiver};SUB:{EscapeInput(this.subject)};BODY:{EscapeInput(this.message)};;"; + break; case MailEncoding.SMTP: - return - $"SMTP:{this.mailReceiver}:{EscapeInput(this.subject, true)}:{EscapeInput(this.message, true)}"; - default: - return this.mailReceiver; + returnVal = $"SMTP:{this.mailReceiver}:{EscapeInput(this.subject, true)}:{EscapeInput(this.message, true)}"; + break; } + return returnVal; } public enum MailEncoding @@ -161,17 +142,26 @@ public SMS(string number, string subject, SMSEncoding encoding = SMSEncoding.SMS public override string ToString() { + var returnVal = string.Empty; switch (this.encoding) - { + { case SMSEncoding.SMS: - return $"sms:{this.number}?body={System.Uri.EscapeDataString(this.subject)}"; + var queryString = string.Empty; + if (!string.IsNullOrEmpty(this.subject)) + queryString = $"?body={Uri.EscapeDataString(this.subject)}"; + returnVal = $"sms:{this.number}{queryString}"; + break; case SMSEncoding.SMS_iOS: - return $"sms:{this.number};body={System.Uri.EscapeDataString(this.subject)}"; + var queryStringiOS = string.Empty; + if (!string.IsNullOrEmpty(this.subject)) + queryStringiOS = $";body={Uri.EscapeDataString(this.subject)}"; + returnVal = $"sms:{this.number}{queryStringiOS}"; + break; case SMSEncoding.SMSTO: - return $"SMSTO:{this.number}:{this.subject}"; - default: - return "sms:"; + returnVal = $"SMSTO:{this.number}:{this.subject}"; + break; } + return returnVal; } public enum SMSEncoding @@ -214,15 +204,23 @@ public MMS(string number, string subject, MMSEncoding encoding = MMSEncoding.MMS public override string ToString() { + var returnVal = string.Empty; switch (this.encoding) - { + { case MMSEncoding.MMSTO: - return $"mmsto:{this.number}?subject={System.Uri.EscapeDataString(this.subject)}"; + var queryStringMmsTo = string.Empty; + if (!string.IsNullOrEmpty(this.subject)) + queryStringMmsTo = $"?subject={Uri.EscapeDataString(this.subject)}"; + returnVal = $"mmsto:{this.number}{queryStringMmsTo}"; + break; case MMSEncoding.MMS: - return $"mms:{this.number}?body={System.Uri.EscapeDataString(this.subject)}"; - default: - return "mms:"; + var queryStringMms = string.Empty; + if (!string.IsNullOrEmpty(this.subject)) + queryStringMms = $"?body={Uri.EscapeDataString(this.subject)}"; + returnVal = $"mms:{this.number}{queryStringMms}"; + break; } + return returnVal; } public enum MMSEncoding diff --git a/QRCoderTests/PayloadGeneratorTests.cs b/QRCoderTests/PayloadGeneratorTests.cs index f6849440..79f5b721 100644 --- a/QRCoderTests/PayloadGeneratorTests.cs +++ b/QRCoderTests/PayloadGeneratorTests.cs @@ -437,6 +437,58 @@ public void mail_should_build_type_mailto() } + [Fact] + [Category("PayloadGenerator/Mail")] + public void mail_should_build_type_mailto_receiver_only() + { + var receiver = "john@doe.com"; + var encoding = PayloadGenerator.Mail.MailEncoding.MAILTO; + + var generator = new PayloadGenerator.Mail(mailReceiver: receiver, encoding: encoding); + + generator.ToString().ShouldBe("mailto:john@doe.com"); + } + + + [Fact] + [Category("PayloadGenerator/Mail")] + public void mail_should_build_type_mailto_subject_only() + { + var receiver = "john@doe.com"; + var subject = "A test mail"; + var encoding = PayloadGenerator.Mail.MailEncoding.MAILTO; + + var generator = new PayloadGenerator.Mail(receiver, subject, encoding: encoding); + + generator.ToString().ShouldBe("mailto:john@doe.com?subject=A%20test%20mail"); + } + + [Fact] + [Category("PayloadGenerator/Mail")] + public void mail_should_build_type_mailto_message_only() + { + var receiver = "john@doe.com"; + var message = "Just see if it works!"; + var encoding = PayloadGenerator.Mail.MailEncoding.MAILTO; + + var generator = new PayloadGenerator.Mail(receiver, message: message, encoding: encoding); + + generator.ToString().ShouldBe("mailto:john@doe.com?body=Just%20see%20if%20it%20works%21"); + } + + [Fact] + [Category("PayloadGenerator/Mail")] + public void mail_should_build_type_mailto_no_receiver() + { + var subject = "A test mail"; + var message = "Just see if it works!"; + var encoding = PayloadGenerator.Mail.MailEncoding.MAILTO; + + var generator = new PayloadGenerator.Mail(subject: subject, message: message, encoding: encoding); + + generator.ToString().ShouldBe("mailto:?subject=A%20test%20mail&body=Just%20see%20if%20it%20works%21"); + } + [Fact] [Category("PayloadGenerator/Mail")] public void mail_should_build_type_MATMSG() @@ -481,7 +533,6 @@ public void mail_should_escape_input_MATMSG() generator.ToString().ShouldBe("MATMSG:TO:john@doe.com;SUB:A test mail;BODY:Just see if \\\\\\:\\;\\, it works!;;"); } - [Fact] [Category("PayloadGenerator/Mail")] public void mail_should_escape_input_SMTP() @@ -496,19 +547,6 @@ public void mail_should_escape_input_SMTP() generator.ToString().ShouldBe("SMTP:john@doe.com:A test mail:Just see\\: if it works!"); } - - [Fact] - [Category("PayloadGenerator/Mail")] - public void mail_should_add_unused_params() - { - var receiver = "john@doe.com"; - - var generator = new PayloadGenerator.Mail(receiver); - - generator.ToString().ShouldBe("mailto:john@doe.com?subject=&body="); - } - - [Fact] [Category("PayloadGenerator/SMS")] public void sms_should_build_type_SMS() @@ -553,13 +591,13 @@ public void sms_should_build_type_SMSTO() [Fact] [Category("PayloadGenerator/SMS")] - public void sms_should_add_unused_params() + public void sms_should_not_add_unused_params() { var number = "01601234567"; var generator = new PayloadGenerator.SMS(number); - generator.ToString().ShouldBe("sms:01601234567?body="); + generator.ToString().ShouldBe("sms:01601234567"); } @@ -593,13 +631,13 @@ public void mms_should_build_type_MMSTO() [Fact] [Category("PayloadGenerator/MMS")] - public void mms_should_add_unused_params() + public void mms_should_not_add_unused_params() { var number = "01601234567"; var generator = new PayloadGenerator.MMS(number); - generator.ToString().ShouldBe("mms:01601234567?body="); + generator.ToString().ShouldBe("mms:01601234567"); }