Skip to content

Commit

Permalink
adding Url Decoding to URL parser, updating tests so that they check …
Browse files Browse the repository at this point in the history
…for multi-word inbound messages, updating readme
  • Loading branch information
slorello89 committed Jul 14, 2020
1 parent 7af6361 commit ce00e8e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 12 deletions.
25 changes: 19 additions & 6 deletions Nexmo.Api.Test.Unit/WebhookParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ public void TestParseStream(string contentType)
string contentString = "";
if(contentType == "application/x-www-form-urlencoded; charset=UTF-8")
{
contentString = "foo-bar=foo";
contentString = "foo-bar=foo%20bar";
}
else
{
contentString = "{\"foo-bar\":\"foo\"}";
contentString = "{\"foo-bar\":\"foo bar\"}";
}
byte[] contentToBytes = Encoding.UTF8.GetBytes(contentString);
MemoryStream stream = new MemoryStream(contentToBytes);
try
{
var output = Utility.WebhookParser.ParseWebhook<Foo>(stream, contentType);
Assert.Equal("foo", output.FooBar);
Assert.Equal("foo bar", output.FooBar);
}
catch (Exception)
{
Expand All @@ -51,11 +51,11 @@ public void TestParseHttpRequestMessage(string contentType)
string contentString = "";
if (contentType == "application/x-www-form-urlencoded")
{
contentString = "foo-bar=foo";
contentString = "foo-bar=foo%20bar";
}
else
{
contentString = "{\"foo-bar\":\"foo\"}";
contentString = "{\"foo-bar\":\"foo bar\"}";
}
byte[] contentToBytes = Encoding.UTF8.GetBytes(contentString);
var request = new HttpRequestMessage();
Expand All @@ -64,7 +64,7 @@ public void TestParseHttpRequestMessage(string contentType)
try
{
var output = Utility.WebhookParser.ParseWebhook<Foo>(request);
Assert.Equal("foo", output.FooBar);
Assert.Equal("foo bar", output.FooBar);
}
catch (Exception)
{
Expand All @@ -73,6 +73,19 @@ public void TestParseHttpRequestMessage(string contentType)
}
}

[Fact]
public void TestParseHttpRequestContentWithBadlyEscapedUrl()
{
var contentType = "application/x-www-form-urlencoded";
var contentString = "foo-bar=foo bar";
byte[] contentToBytes = Encoding.UTF8.GetBytes(contentString);
var request = new HttpRequestMessage();
request.Content = new ByteArrayContent(contentToBytes);
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
var output = Utility.WebhookParser.ParseWebhook<Foo>(request);
Assert.Equal("foo bar", output.FooBar);
}

[Fact]
public void TestParseQueryArgsMvcLegacy()
{
Expand Down
3 changes: 2 additions & 1 deletion Nexmo.Api/Utility/WebhookParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Newtonsoft.Json;
using Nexmo.Api.Messaging;
using System.Net;

namespace Nexmo.Api.Utility
{
Expand Down Expand Up @@ -139,7 +140,7 @@ public static T ParseUrlFormString<T>(string contentString)
foreach (var param in split_parameters)
{
var split = param.Split('=');
content_dictonary.Add(split[0], split[1]);
content_dictonary.Add(split[0], WebUtility.UrlDecode(split[1]));
}
var json = JsonConvert.SerializeObject(content_dictonary);
return JsonConvert.DeserializeObject<T>(json);
Expand Down
96 changes: 91 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,28 +221,114 @@ var response = nexmoClient.SmsClient.SendAnSms(new Nexmo.Api.Messaging.SendSmsRe

Use [Nexmo's SMS API][doc_sms] to receive an SMS message. Assumes your Nexmo endpoint is configured.

The best method for receiving an SMS will vary depending on whether you configure your webhooks to be GET or POST. Will Also Vary between ASP.NET MVC and ASP.NET MVC Core.

#### ASP.NET MVC Core

##### GET

```csharp
[HttpGet("webhooks/inbound-sms")]
public async Task<IActionResult> InboundSmsGet()
{
var inbound = Nexmo.Api.Utility.WebhookParser.ParseQuery<InboundSms>(Request.Query);
return NoContent();
}
```

##### POST

```csharp
[HttpPost("webhooks/inbound-sms")]
public IActionResult InboundSms([FromBody]InboundSms sms)
public async Task<IActionResult> InboundSms()
{
Console.WriteLine($"SMS Received with message: {sms.Text}");
var inbound = await Nexmo.Api.Utility.WebhookParser.ParseWebhookAsync<InboundSms>(Request.Body, Request.ContentType);
return NoContent();
}
```

#### ASP.NET MVC

##### GET

```csharp
[HttpGet]
[Route("webhooks/inbound-sms")]
public async Task<HttpResponseMessage> GetInbound()
{
var inboundSms = WebhookParser.ParseQueryNameValuePairs<InboundSms>(Request.GetQueryNameValuePairs());
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
```

##### POST

```csharp
[HttpPost]
[Route("webhooks/inbound-sms")]
public async Task<HttpResponseMessage> PostInbound()
{
var inboundSms = WebhookParser.ParseWebhook<InboundSms>(Request);
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
```

### Receiving a Message Delivery Receipt

Use [Nexmo's SMS API][doc_sms] to receive an SMS delivery receipt. Assumes your Nexmo endpoint is configured.

The best method for receiving an SMS will vary depending on whether you configure your webhooks to be GET or POST. Will Also Vary between ASP.NET MVC and ASP.NET MVC Core.

#### ASP.NET MVC Core

##### GET

```csharp
[HttpGet("webhooks/dlr")]
public async Task<IActionResult> InboundSmsGet()
{
var dlr = Nexmo.Api.Utility.WebhookParser.ParseQuery<DeliveryReceipt>(Request.Query);
return NoContent();
}
```

##### POST

```csharp
[HttpGet("webhooks/delivery-receipt")]
public IActionResult DeliveryReceipt([FromQuery]DeliveryReceipt dlr)
[HttpPost("webhooks/dlr")]
public async Task<IActionResult> InboundSms()
{
Console.WriteLine($"Delivery receipt received for messages {dlr.MessageId}");
var dlr = await Nexmo.Api.Utility.WebhookParser.ParseWebhookAsync<DeliveryReceipt>(Request.Body, Request.ContentType);
return NoContent();
}
```

#### ASP.NET MVC

##### GET

```csharp
[HttpGet]
[Route("webhooks/dlr")]
public async Task<HttpResponseMessage> GetInbound()
{
var dlr = WebhookParser.ParseQueryNameValuePairs<DeliveryReceipt>(Request.GetQueryNameValuePairs());
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
```

##### POST

```csharp
[HttpPost]
[Route("webhooks/dlr")]
public async Task<HttpResponseMessage> PostInbound()
{
var dlr = WebhookParser.ParseWebhook<DeliveryReceipt>(Request);
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
```

### Redacting a message

Use [Nexmo's Redact API][doc_redact] to redact a SMS message.
Expand Down

0 comments on commit ce00e8e

Please sign in to comment.