Skip to content

Commit

Permalink
Refactored SMS, MMS & Mail generator.
Browse files Browse the repository at this point in the history
Added more tests and removed empty paramters.
  • Loading branch information
codebude committed Nov 22, 2021
1 parent 50a9cef commit 6afa358
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 64 deletions.
90 changes: 44 additions & 46 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,15 @@ public class Mail : Payload
private readonly string mailReceiver, subject, message;
private readonly MailEncoding encoding;

/// <summary>
/// Creates an empty email payload
/// </summary>
/// <param name="mailReceiver">Receiver's email address</param>
/// <param name="encoding">Payload encoding type. Choose dependent on your QR Code scanner app.</param>
public Mail(string mailReceiver, MailEncoding encoding = MailEncoding.MAILTO)
{
this.mailReceiver = mailReceiver;
this.subject = this.message = string.Empty;
this.encoding = encoding;
}

/// <summary>
/// Creates an email payload with subject
/// </summary>
/// <param name="mailReceiver">Receiver's email address</param>
/// <param name="subject">Subject line of the email</param>
/// <param name="encoding">Payload encoding type. Choose dependent on your QR Code scanner app.</param>
public Mail(string mailReceiver, string subject, MailEncoding encoding = MailEncoding.MAILTO)
{
this.mailReceiver = mailReceiver;
this.subject = subject;
this.message = string.Empty;
this.encoding = encoding;
}


/// <summary>
/// Creates an email payload with subject and message/text
/// </summary>
/// <param name="mailReceiver">Receiver's email address</param>
/// <param name="subject">Subject line of the email</param>
/// <param name="message">Message content of the email</param>
/// <param name="encoding">Payload encoding type. Choose dependent on your QR Code scanner app.</param>
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;
Expand All @@ -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<string>();
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
74 changes: 56 additions & 18 deletions QRCoderTests/PayloadGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]";
var encoding = PayloadGenerator.Mail.MailEncoding.MAILTO;

var generator = new PayloadGenerator.Mail(mailReceiver: receiver, encoding: encoding);

generator.ToString().ShouldBe("mailto:[email protected]");
}


[Fact]
[Category("PayloadGenerator/Mail")]
public void mail_should_build_type_mailto_subject_only()
{
var receiver = "[email protected]";
var subject = "A test mail";
var encoding = PayloadGenerator.Mail.MailEncoding.MAILTO;

var generator = new PayloadGenerator.Mail(receiver, subject, encoding: encoding);

generator.ToString().ShouldBe("mailto:[email protected]?subject=A%20test%20mail");
}

[Fact]
[Category("PayloadGenerator/Mail")]
public void mail_should_build_type_mailto_message_only()
{
var receiver = "[email protected]";
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:[email protected]?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()
Expand Down Expand Up @@ -481,7 +533,6 @@ public void mail_should_escape_input_MATMSG()
generator.ToString().ShouldBe("MATMSG:TO:[email protected];SUB:A test mail;BODY:Just see if \\\\\\:\\;\\, it works!;;");
}


[Fact]
[Category("PayloadGenerator/Mail")]
public void mail_should_escape_input_SMTP()
Expand All @@ -496,19 +547,6 @@ public void mail_should_escape_input_SMTP()
generator.ToString().ShouldBe("SMTP:[email protected]:A test mail:Just see\\: if it works!");
}


[Fact]
[Category("PayloadGenerator/Mail")]
public void mail_should_add_unused_params()
{
var receiver = "[email protected]";

var generator = new PayloadGenerator.Mail(receiver);

generator.ToString().ShouldBe("mailto:[email protected]?subject=&body=");
}


[Fact]
[Category("PayloadGenerator/SMS")]
public void sms_should_build_type_SMS()
Expand Down Expand Up @@ -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");
}


Expand Down Expand Up @@ -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");
}


Expand Down

0 comments on commit 6afa358

Please sign in to comment.