-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
namespace ServiceBus.AttachmentPlugin.Tests | ||
{ | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.ServiceBus; | ||
using Xunit; | ||
|
||
public class When_using_message_extensions : IClassFixture<AzureStorageEmulatorFixture> | ||
{ | ||
readonly AzureStorageEmulatorFixture fixture; | ||
|
||
public When_using_message_extensions(AzureStorageEmulatorFixture fixture) | ||
{ | ||
this.fixture = fixture; | ||
} | ||
|
||
[Fact] | ||
public async Task Should_send_and_receive_with_configuration() | ||
{ | ||
var payload = "payload"; | ||
var bytes = Encoding.UTF8.GetBytes(payload); | ||
var message = new Message(bytes); | ||
var configuration = new AzureStorageAttachmentConfiguration( | ||
connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id"); | ||
|
||
await message.UploadAzureStorageAttachment(configuration); | ||
|
||
Assert.Null(message.Body); | ||
|
||
var receivedMessage = await message.DownloadAzureStorageAttachment(configuration); | ||
|
||
Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body)); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_send_and_receive_with_default_sas_uri_property() | ||
{ | ||
var payload = "payload"; | ||
var bytes = Encoding.UTF8.GetBytes(payload); | ||
var message = new Message(bytes); | ||
var configuration = new AzureStorageAttachmentConfiguration( | ||
connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id") | ||
.WithBlobSasUri(); | ||
|
||
await message.UploadAzureStorageAttachment(configuration); | ||
|
||
Assert.Null(message.Body); | ||
|
||
var receivedMessage = await message.DownloadAzureStorageAttachment(); | ||
|
||
Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body)); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_send_and_receive_with_custom_sas_uri_property() | ||
{ | ||
var payload = "payload"; | ||
var bytes = Encoding.UTF8.GetBytes(payload); | ||
var message = new Message(bytes); | ||
string customSasUri = "$custom-attachment.sas.uri"; | ||
var configuration = new AzureStorageAttachmentConfiguration( | ||
connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id") | ||
.WithBlobSasUri(customSasUri); | ||
|
||
await message.UploadAzureStorageAttachment(configuration); | ||
|
||
Assert.Null(message.Body); | ||
|
||
var receivedMessage = await message.DownloadAzureStorageAttachment(customSasUri); | ||
|
||
Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace Microsoft.Azure.ServiceBus | ||
{ | ||
using global::ServiceBus.AttachmentPlugin; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary>Extension methods for working with attachments without registering plugin.</summary> | ||
public static class MessageExtensions | ||
{ | ||
/// <summary>Upload attachment to Azure Storage blob without registering plugin.</summary> | ||
/// <param name="message"><see cref="Message"/></param> | ||
/// <param name="configuration"><see cref="AzureStorageAttachmentConfiguration"/> object.</param> | ||
/// <returns><see cref="Message"/> with body uploaded to Azure Storage blob.</returns> | ||
public static async Task<Message> UploadAzureStorageAttachment(this Message message, AzureStorageAttachmentConfiguration configuration) | ||
{ | ||
var plugin = new AzureStorageAttachment(configuration); | ||
return await plugin.BeforeMessageSend(message).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary>Download attachment from Azure Storage blob without registering plugin, using configuration object.</summary> | ||
/// <param name="message"><see cref="Message"/></param> | ||
/// <param name="configuration"><see cref="AzureStorageAttachmentConfiguration"/> object.</param> | ||
/// <returns><see cref="Message"/> with body downloaded from Azure Storage blob.</returns> | ||
public static async Task<Message> DownloadAzureStorageAttachment(this Message message, AzureStorageAttachmentConfiguration configuration) | ||
{ | ||
var plugin = new AzureStorageAttachment(configuration); | ||
return await plugin.AfterMessageReceive(message).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary>Download attachment from Azure Storage blob without registering plugin, using default message property to identify SAS URI.</summary> | ||
/// <param name="message"><see cref="Message"/></param> | ||
/// <returns><see cref="Message"/> with body downloaded from Azure Storage blob.</returns> | ||
public static async Task<Message> DownloadAzureStorageAttachment(this Message message) | ||
{ | ||
var plugin = new ReceiveOnlyAzureStorageAttachment(AzureStorageAttachmentConfigurationExtensions.DefaultMessagePropertyToIdentitySasUri); | ||
return await plugin.AfterMessageReceive(message).ConfigureAwait(false); | ||
} | ||
|
||
|
||
/// <summary>Download attachment from Azure Storage blob without registering plugin, with custom message property to identify SAS URI.</summary> | ||
/// <param name="message"><see cref="Message"/></param> | ||
/// <param name="messagePropertyToIdentifySasUri">Message property which contains the SAS URI used to fetch message body from blob.</param> | ||
/// <returns><see cref="Message"/> with body downloaded from Azure Storage blob.</returns> | ||
public static async Task<Message> DownloadAzureStorageAttachment(this Message message, string messagePropertyToIdentifySasUri) | ||
{ | ||
var plugin = new ReceiveOnlyAzureStorageAttachment(messagePropertyToIdentifySasUri); | ||
return await plugin.AfterMessageReceive(message).ConfigureAwait(false); | ||
} | ||
} | ||
} |