2019-06-25 01:49:44 +03:00
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-06-20 23:19:00 +03:00
|
|
|
|
using System.Collections.Generic;
|
2021-09-30 20:38:06 +03:00
|
|
|
|
using System.IO;
|
2019-06-20 23:19:00 +03:00
|
|
|
|
using System.Net.Http;
|
2021-09-30 20:38:06 +03:00
|
|
|
|
using System.Text;
|
2019-06-20 23:19:00 +03:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Bot.Schema;
|
2019-09-27 23:16:43 +03:00
|
|
|
|
using Microsoft.Bot.Streaming.Payloads;
|
2021-09-30 20:38:06 +03:00
|
|
|
|
using Moq;
|
2019-11-09 01:20:32 +03:00
|
|
|
|
using Newtonsoft.Json;
|
2020-09-11 01:42:16 +03:00
|
|
|
|
using Xunit;
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2019-09-27 23:16:43 +03:00
|
|
|
|
namespace Microsoft.Bot.Streaming.UnitTests
|
2019-06-20 23:19:00 +03:00
|
|
|
|
{
|
|
|
|
|
public class RequestTests
|
|
|
|
|
{
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-07-02 21:55:12 +03:00
|
|
|
|
public void ReceiveRequest_ctor_Empty_Streams()
|
2019-06-20 23:19:00 +03:00
|
|
|
|
{
|
|
|
|
|
var r = new ReceiveRequest();
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.NotNull(r.Streams);
|
|
|
|
|
Assert.Empty(r.Streams);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void ReceiveRequest_ctor_NullProperties()
|
|
|
|
|
{
|
|
|
|
|
var r = new ReceiveRequest();
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Null(r.Verb);
|
|
|
|
|
Assert.Null(r.Path);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 20:38:06 +03:00
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
|
|
public void ReceiveExtensions_ReadBodyAsJson_Streams()
|
|
|
|
|
{
|
|
|
|
|
var activity = new Activity { Type = ActivityTypes.Message };
|
|
|
|
|
var stringInput = JsonConvert.SerializeObject(activity);
|
|
|
|
|
var stream = new MemoryStream(Encoding.UTF8.GetBytes(stringInput));
|
|
|
|
|
var mockContentStream = new Mock<IContentStream>();
|
|
|
|
|
mockContentStream.Setup(e => e.Stream).Returns(stream);
|
|
|
|
|
|
|
|
|
|
var request = new ReceiveRequest
|
|
|
|
|
{
|
|
|
|
|
Streams = new List<IContentStream> { mockContentStream.Object }
|
|
|
|
|
};
|
|
|
|
|
var result = request.ReadBodyAsJson<Activity>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Equal(activity.Type, result.Type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
|
|
public void ReceiveExtensions_ReadBodyAsJson_Streams_Zero()
|
|
|
|
|
{
|
|
|
|
|
var request = new ReceiveRequest();
|
|
|
|
|
var result = request.ReadBodyAsJson<dynamic>();
|
|
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
|
|
public void ReceiveExtensions_ReadBodyAsString_Streams_Zero()
|
|
|
|
|
{
|
|
|
|
|
var request = new ReceiveRequest();
|
|
|
|
|
var result = request.ReadBodyAsString();
|
|
|
|
|
|
|
|
|
|
Assert.Empty(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_NullProperties()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = new StreamingRequest();
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Null(r.Verb);
|
|
|
|
|
Assert.Null(r.Path);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_AddStream_Null_Throws()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = new StreamingRequest();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Throws<ArgumentNullException>(() =>
|
2019-06-21 23:47:23 +03:00
|
|
|
|
{
|
2019-06-20 23:19:00 +03:00
|
|
|
|
r.AddStream(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_AddStream_Success()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = new StreamingRequest();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
var s = new StringContent("hi");
|
|
|
|
|
|
|
|
|
|
r.AddStream(s);
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.NotNull(r.Streams);
|
|
|
|
|
Assert.Single(r.Streams);
|
|
|
|
|
Assert.Equal(s, r.Streams[0].Content);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_AddStream_ExistingList_Success()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = new StreamingRequest();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
var s = new StringContent("hi");
|
|
|
|
|
var s2 = new StringContent("hello");
|
|
|
|
|
|
2019-07-04 05:27:59 +03:00
|
|
|
|
r.Streams = new List<ResponseMessageStream> { new ResponseMessageStream() { Content = s2 } };
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
|
|
|
|
r.AddStream(s);
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.NotNull(r.Streams);
|
|
|
|
|
Assert.Equal(2, r.Streams.Count);
|
|
|
|
|
Assert.Equal(s2, r.Streams[0].Content);
|
|
|
|
|
Assert.Equal(s, r.Streams[1].Content);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_Create_Get_Success()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = StreamingRequest.CreateGet();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Equal(StreamingRequest.GET, r.Verb);
|
|
|
|
|
Assert.Null(r.Path);
|
2021-10-15 07:59:07 +03:00
|
|
|
|
Assert.Empty(r.Streams);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_Create_Post_Success()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = StreamingRequest.CreatePost();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Equal(StreamingRequest.POST, r.Verb);
|
|
|
|
|
Assert.Null(r.Path);
|
2021-10-15 07:59:07 +03:00
|
|
|
|
Assert.Empty(r.Streams);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
2019-06-21 23:47:23 +03:00
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_Create_Delete_Success()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = StreamingRequest.CreateDelete();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Equal(StreamingRequest.DELETE, r.Verb);
|
|
|
|
|
Assert.Null(r.Path);
|
2021-10-15 07:59:07 +03:00
|
|
|
|
Assert.Empty(r.Streams);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
2019-06-21 23:47:23 +03:00
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_Create_Put_Success()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = StreamingRequest.CreatePut();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Equal(StreamingRequest.PUT, r.Verb);
|
|
|
|
|
Assert.Null(r.Path);
|
2021-10-15 07:59:07 +03:00
|
|
|
|
Assert.Empty(r.Streams);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public void Request_Create_WithBody_Success()
|
|
|
|
|
{
|
|
|
|
|
var s = new StringContent("hi");
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = StreamingRequest.CreateRequest(StreamingRequest.POST, "123", s);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Equal(StreamingRequest.POST, r.Verb);
|
|
|
|
|
Assert.Equal("123", r.Path);
|
|
|
|
|
Assert.NotNull(r.Streams);
|
|
|
|
|
Assert.Single(r.Streams);
|
|
|
|
|
Assert.Equal(s, r.Streams[0].Content);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 20:38:06 +03:00
|
|
|
|
[Fact]
|
|
|
|
|
public void Request_Create_WithEmptyMethod()
|
|
|
|
|
{
|
|
|
|
|
var request = StreamingRequest.CreateRequest(string.Empty);
|
|
|
|
|
|
|
|
|
|
Assert.Null(request);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-21 23:47:23 +03:00
|
|
|
|
public async Task RequestExtensions_SetBodyString_Success()
|
2019-06-20 23:19:00 +03:00
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = new StreamingRequest();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
r.SetBody("123");
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.NotNull(r.Streams);
|
|
|
|
|
Assert.Single(r.Streams);
|
|
|
|
|
Assert.Equal(typeof(StringContent), r.Streams[0].Content.GetType());
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2024-08-12 20:16:17 +03:00
|
|
|
|
var s = await r.Streams[0].Content.ReadAsStringAsync();
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Equal("123", s);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-26 22:45:20 +03:00
|
|
|
|
public void RequestExtensions_SetBodyString_Null_Does_Not_Throw()
|
2019-06-20 23:19:00 +03:00
|
|
|
|
{
|
2019-06-26 22:45:20 +03:00
|
|
|
|
Exception ex = null;
|
|
|
|
|
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = new StreamingRequest();
|
2019-06-26 22:45:20 +03:00
|
|
|
|
try
|
2019-06-20 23:19:00 +03:00
|
|
|
|
{
|
|
|
|
|
r.SetBody((string)null);
|
2019-06-26 22:45:20 +03:00
|
|
|
|
}
|
|
|
|
|
catch (Exception caughtEx)
|
|
|
|
|
{
|
|
|
|
|
ex = caughtEx;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Null(ex);
|
2019-06-26 22:45:20 +03:00
|
|
|
|
}
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 20:38:06 +03:00
|
|
|
|
[Fact]
|
|
|
|
|
public void RequestExtensions_SetBodyObject_Null_Does_Not_Throw()
|
|
|
|
|
{
|
|
|
|
|
Exception ex = null;
|
|
|
|
|
|
|
|
|
|
var r = new StreamingRequest();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
r.SetBody((object)null);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception caughtEx)
|
|
|
|
|
{
|
|
|
|
|
ex = caughtEx;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Assert.Null(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-20 23:19:00 +03:00
|
|
|
|
public async Task RequestExtensions_SetBody_Success()
|
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = new StreamingRequest();
|
2019-06-20 23:19:00 +03:00
|
|
|
|
var a = new Activity() { Text = "hi", Type = "message" };
|
|
|
|
|
r.SetBody(a);
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.NotNull(r.Streams);
|
|
|
|
|
Assert.Single(r.Streams);
|
|
|
|
|
Assert.Equal(typeof(StringContent), r.Streams[0].Content.GetType());
|
2019-06-20 23:19:00 +03:00
|
|
|
|
|
2024-08-12 20:16:17 +03:00
|
|
|
|
var s = JsonConvert.DeserializeObject<Activity>(await r.Streams[0].Content.ReadAsStringAsync());
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Equal(a.Text, s.Text);
|
|
|
|
|
Assert.Equal(a.Type, s.Type);
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 01:42:16 +03:00
|
|
|
|
[Fact]
|
2019-06-26 22:45:20 +03:00
|
|
|
|
public void RequestExtensions_SetBody_Null_Does_Not_Throw()
|
2019-06-20 23:19:00 +03:00
|
|
|
|
{
|
2019-07-01 23:18:10 +03:00
|
|
|
|
var r = new StreamingRequest();
|
2019-06-26 22:45:20 +03:00
|
|
|
|
Exception ex = null;
|
|
|
|
|
|
|
|
|
|
try
|
2019-06-20 23:19:00 +03:00
|
|
|
|
{
|
|
|
|
|
r.SetBody(null);
|
2019-06-26 22:45:20 +03:00
|
|
|
|
}
|
|
|
|
|
catch (Exception caughtEx)
|
|
|
|
|
{
|
|
|
|
|
ex = caughtEx;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2020-09-11 01:42:16 +03:00
|
|
|
|
Assert.Null(ex);
|
2019-06-26 22:45:20 +03:00
|
|
|
|
}
|
2019-06-20 23:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|