botbuilder-dotnet/tests/Microsoft.Bot.Streaming.Tests/RequestTests.cs

277 строки
7.3 KiB
C#
Исходник Обычный вид История

// 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;
using System.IO;
2019-06-20 23:19:00 +03:00
using System.Net.Http;
using System.Text;
2019-06-20 23:19:00 +03:00
using System.Threading.Tasks;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Streaming.Payloads;
using Moq;
using Newtonsoft.Json;
using Xunit;
2019-06-20 23:19:00 +03:00
namespace Microsoft.Bot.Streaming.UnitTests
2019-06-20 23:19:00 +03:00
{
public class RequestTests
{
[Fact]
public void ReceiveRequest_ctor_Empty_Streams()
2019-06-20 23:19:00 +03:00
{
var r = new ReceiveRequest();
Assert.NotNull(r.Streams);
Assert.Empty(r.Streams);
2019-06-20 23:19:00 +03:00
}
[Fact]
2019-06-20 23:19:00 +03:00
public void ReceiveRequest_ctor_NullProperties()
{
var r = new ReceiveRequest();
Assert.Null(r.Verb);
Assert.Null(r.Path);
2019-06-20 23:19:00 +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);
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_NullProperties()
{
var r = new StreamingRequest();
Assert.Null(r.Verb);
Assert.Null(r.Path);
2019-06-20 23:19:00 +03:00
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_AddStream_Null_Throws()
{
var r = new StreamingRequest();
2019-06-20 23:19:00 +03:00
Assert.Throws<ArgumentNullException>(() =>
{
2019-06-20 23:19:00 +03:00
r.AddStream(null);
});
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_AddStream_Success()
{
var r = new StreamingRequest();
2019-06-20 23:19:00 +03:00
var s = new StringContent("hi");
r.AddStream(s);
Assert.NotNull(r.Streams);
Assert.Single(r.Streams);
Assert.Equal(s, r.Streams[0].Content);
2019-06-20 23:19:00 +03:00
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_AddStream_ExistingList_Success()
{
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);
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
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_Create_Get_Success()
{
var r = StreamingRequest.CreateGet();
2019-06-20 23:19:00 +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
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_Create_Post_Success()
{
var r = StreamingRequest.CreatePost();
2019-06-20 23:19:00 +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
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_Create_Delete_Success()
{
var r = StreamingRequest.CreateDelete();
2019-06-20 23:19:00 +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
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_Create_Put_Success()
{
var r = StreamingRequest.CreatePut();
2019-06-20 23:19:00 +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
}
[Fact]
2019-06-20 23:19:00 +03:00
public void Request_Create_WithBody_Success()
{
var s = new StringContent("hi");
var r = StreamingRequest.CreateRequest(StreamingRequest.POST, "123", s);
2019-06-20 23:19:00 +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
}
[Fact]
public void Request_Create_WithEmptyMethod()
{
var request = StreamingRequest.CreateRequest(string.Empty);
Assert.Null(request);
}
[Fact]
public async Task RequestExtensions_SetBodyString_Success()
2019-06-20 23:19:00 +03:00
{
var r = new StreamingRequest();
2019-06-20 23:19:00 +03:00
r.SetBody("123");
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
var s = await r.Streams[0].Content.ReadAsStringAsync();
Assert.Equal("123", s);
2019-06-20 23:19:00 +03:00
}
[Fact]
public void RequestExtensions_SetBodyString_Null_Does_Not_Throw()
2019-06-20 23:19:00 +03:00
{
Exception ex = null;
var r = new StreamingRequest();
try
2019-06-20 23:19:00 +03:00
{
r.SetBody((string)null);
}
catch (Exception caughtEx)
{
ex = caughtEx;
}
finally
{
Assert.Null(ex);
}
2019-06-20 23:19:00 +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);
}
}
[Fact]
2019-06-20 23:19:00 +03:00
public async Task RequestExtensions_SetBody_Success()
{
var r = new StreamingRequest();
2019-06-20 23:19:00 +03:00
var a = new Activity() { Text = "hi", Type = "message" };
r.SetBody(a);
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
var s = JsonConvert.DeserializeObject<Activity>(await r.Streams[0].Content.ReadAsStringAsync());
Assert.Equal(a.Text, s.Text);
Assert.Equal(a.Type, s.Type);
2019-06-20 23:19:00 +03:00
}
[Fact]
public void RequestExtensions_SetBody_Null_Does_Not_Throw()
2019-06-20 23:19:00 +03:00
{
var r = new StreamingRequest();
Exception ex = null;
try
2019-06-20 23:19:00 +03:00
{
r.SetBody(null);
}
catch (Exception caughtEx)
{
ex = caughtEx;
}
finally
{
Assert.Null(ex);
}
2019-06-20 23:19:00 +03:00
}
}
}