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

Refactored SMS, MMS & Mail generator. #349

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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