[#6438] It's not possible to send binary data with the HttpSend activity (#6470)

* Update HttpRequest to handle binary content

* Add unit test

* Rename variable
This commit is contained in:
Cecilia Avila 2022-11-02 10:28:39 -03:00 коммит произвёл GitHub
Родитель 9c5de0ebd1
Коммит a12e47f601
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 29 добавлений и 39 удалений

Просмотреть файл

@ -282,42 +282,12 @@ namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Actions
traceInfo.request.url = instanceUrl;
HttpResponseMessage response = null;
string contentType = ContentType?.GetValue(dc.State) ?? "application/json";
var contentType = ContentType?.GetValue(dc.State) ?? "application/json";
switch (Method)
{
case HttpMethod.POST:
if (instanceBody == null)
{
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
else
{
using var postContent = new StringContent(instanceBody.ToString(), Encoding.UTF8, contentType);
traceInfo.request.content = instanceBody.ToString();
request.Content = postContent;
traceInfo.request.headers = JObject.FromObject(request.Content.Headers.ToDictionary(t => t.Key, t => (object)t.Value?.FirstOrDefault()));
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
break;
case HttpMethod.PATCH:
if (instanceBody == null)
{
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
else
{
using var patchContent = new StringContent(instanceBody.ToString(), Encoding.UTF8, contentType);
request.Content = patchContent;
traceInfo.request.content = instanceBody.ToString();
traceInfo.request.headers = JObject.FromObject(request.Content.Headers.ToDictionary(t => t.Key, t => (object)t.Value?.FirstOrDefault()));
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
break;
case HttpMethod.PUT:
if (instanceBody == null)
{
@ -325,19 +295,27 @@ namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Actions
}
else
{
using var putContent = new StringContent(instanceBody.ToString(), Encoding.UTF8, contentType);
traceInfo.request.content = instanceBody.ToString();
request.Content = putContent;
traceInfo.request.headers = JObject.FromObject(request.Content.Headers.ToDictionary(t => t.Key, t => (object)t.Value?.FirstOrDefault()));
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
if (instanceBody.GetType() == typeof(byte[]))
{
using var bodyContent = new ByteArrayContent((byte[])instanceBody);
request.Content = bodyContent;
traceInfo.request.content = JsonConvert.SerializeObject(instanceBody);
traceInfo.request.headers = JObject.FromObject(request.Content.Headers.ToDictionary(t => t.Key, t => (object)t.Value?.FirstOrDefault()));
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
else
{
using var bodyContent = new StringContent(instanceBody.ToString(), Encoding.UTF8, contentType);
request.Content = bodyContent;
traceInfo.request.content = instanceBody.ToString();
traceInfo.request.headers = JObject.FromObject(request.Content.Headers.ToDictionary(t => t.Key, t => (object)t.Value?.FirstOrDefault()));
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}
break;
case HttpMethod.DELETE:
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
break;
case HttpMethod.GET:
response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
break;

Просмотреть файл

@ -578,6 +578,10 @@ namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Tests
.WithContent("[\r\n {\r\n \"text\": \"Joe is 52\",\r\n \"age\": 52\r\n },\r\n {\r\n \"text\": \"text\",\r\n \"age\": 11\r\n }\r\n]".Replace("\r\n", Environment.NewLine))
.Respond("plain/text", "array");
handler
.When(HttpMethod.Post, "http://foo.com/upload")
.Respond("plain/text", "data received");
handler
.When(HttpMethod.Put, "http://foo.com/")
.WithContent("Joe is 52")
@ -698,6 +702,13 @@ namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Tests
},
new SendActivity("${turn.lastresult.content}"),
new HttpRequest()
{
Url = "http://foo.com/upload",
Method = HttpRequest.HttpMethod.POST,
Body = "=base64ToBinary(base64('Hello'))"
},
new SendActivity("${turn.lastresult.content}"),
new HttpRequest()
{
Url = "http://foo.com/",
Method = HttpRequest.HttpMethod.PUT,
@ -784,6 +795,7 @@ namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Tests
.AssertReply("string")
.AssertReply("object")
.AssertReply("array")
.AssertReply("data received")
.AssertReply("put:string")
.AssertReply("put:object")
.AssertReply("put:array")