Skip to content

Commit

Permalink
Context的单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 31, 2018
1 parent 922b350 commit 4f308e1
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 18 deletions.
4 changes: 1 addition & 3 deletions WebApiClient.Test/Attributes/TestActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using WebApiClient.Contexts;

namespace WebApiClient.Test
{
class TestActionContext : Contexts.ApiActionContext
class TestActionContext : ApiActionContext
{
/// <summary>
/// 请求Api的上下文
Expand Down
38 changes: 38 additions & 0 deletions WebApiClient.Test/Contexts/ApiActionDescriptorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using WebApiClient.Contexts;
using Xunit;

namespace WebApiClient.Test.Contexts
{
public class ApiActionDescriptorTest
{
[Fact]
public void NewTest()
{
var m = typeof(IUserApi).GetMethod("Get1");
var d = new ApiActionDescriptor(m);

Assert.True(d.Attributes.Count == 3);
Assert.True(d.Filters.Count == 1);
Assert.True(d.Parameters.Count == 2);
Assert.True(d.Name == m.Name);
Assert.True(d.Member == m);
Assert.True(d.Return.ReturnType == m.ReturnType);
}

[Fact]
public void CloneTest()
{
var m = typeof(IUserApi).GetMethod("Get1");
var d = new ApiActionDescriptor(m);
var d2 = d.Clone(new object[] { null, null });

Assert.True(d.Attributes == d2.Attributes);
Assert.True(d.Name == d2.Name);
Assert.True(d.Filters == d2.Filters);
Assert.True(d.Member == d2.Member);
Assert.True(d.Parameters != d2.Parameters);
Assert.True(d.Parameters.Count == d2.Parameters.Count);
Assert.True(d.Return == d2.Return);
}
}
}
50 changes: 50 additions & 0 deletions WebApiClient.Test/Contexts/ApiParameterDescriptorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using WebApiClient.Contexts;
using Xunit;

namespace WebApiClient.Test.Contexts
{
public class ApiParameterDescriptorTest
{
[Fact]
public void NewTest()
{
var p1 = typeof(IUserApi).GetMethod("Get2").GetParameters()[0];
var m1 = new ApiParameterDescriptor(p1);
Assert.True(m1.Attributes.Count == 1);
Assert.True(m1.Index == 0);
Assert.True(m1.Member == p1);
Assert.True(m1.Name == p1.Name);
Assert.True(m1.ParameterType == p1.ParameterType);
Assert.True(m1.ValidationAttributes.Count == 1);
Assert.True(m1.Value == null);


var p2 = typeof(IUserApi).GetMethod("Get2").GetParameters()[1];
var m2 = new ApiParameterDescriptor(p2);
Assert.True(m2.Attributes.Count == 1);
Assert.True(m2.Index == 1);
Assert.True(m2.Member == p2);
Assert.True(m2.Name == p2.Name);
Assert.True(m2.ParameterType == p2.ParameterType);
Assert.True(m2.ValidationAttributes.Count == 0);
Assert.True(m2.Value == null);
}


[Fact]
public void CloneTest()
{
var p1 = typeof(IUserApi).GetMethod("Get2").GetParameters()[0];
var m1 = new ApiParameterDescriptor(p1);
var m2 = m1.Clone(10);

Assert.True(m1.Attributes == m2.Attributes);
Assert.True(m1.Name == m2.Name);
Assert.True(m1.Index == m2.Index);
Assert.True(m1.ParameterType == m2.ParameterType);
Assert.True(m1.Member == m2.Member);
Assert.True(m1.ValidationAttributes == m2.ValidationAttributes);
Assert.True((int)m2.Value == 10);
}
}
}
22 changes: 22 additions & 0 deletions WebApiClient.Test/Contexts/ApiReturnDescriptorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using WebApiClient.Attributes;
using WebApiClient.Contexts;
using Xunit;

namespace WebApiClient.Test.Contexts
{
public class ApiReturnDescriptorTest
{
[Fact]
public void NewTest()
{
var m1 = new ApiReturnDescriptor(typeof(IUserApi).GetMethod("Get1"));
var m2 = new ApiReturnDescriptor(typeof(IUserApi).GetMethod("Get2"));

Assert.True(m1.IsTaskDefinition);
Assert.True(m1.Attribute.GetType() == typeof(AutoReturnAttribute));

Assert.True(m2.IsITaskDefinition);
Assert.True(m2.Attribute.GetType() == typeof(JsonReturnAttribute));
}
}
}
34 changes: 34 additions & 0 deletions WebApiClient.Test/Contexts/DataTypeDescriptorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Linq;
using WebApiClient.Contexts;
using Xunit;

namespace WebApiClient.Test.Contexts
{
public class DataTypeDescriptorTest
{
private DataTypeDescriptor Create(string methodName)
{
var method = typeof(IUserApi).GetMethod(methodName);
var dataType = method.ReturnType.GetGenericArguments().FirstOrDefault();
return new DataTypeDescriptor(dataType);
}

[Fact]
public void NewTest()
{
var m1 = Create("Get1");
var m2 = Create("Get2");
var m3 = Create("Get3");
var m4 = Create("Get4");
var m5 = Create("Get5");
var m6 = Create("Get6");

Assert.True(m1.IsString);
Assert.True(m2.IsHttpResponseMessage);
Assert.True(m3.IsStream);
Assert.True(m4.IsHttpResponseWrapper);
Assert.True(m6.IsByteArray);
Assert.False(m5.IsString || m5.IsStream || m5.IsByteArray || m5.IsHttpResponseMessage || m5.IsHttpResponseWrapper);
}
}
}
39 changes: 39 additions & 0 deletions WebApiClient.Test/Contexts/IUserApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using WebApiClient.Attributes;
using WebApiClient.Parameterables;

namespace WebApiClient.Test.Contexts
{
/// <summary>
/// 用户操作接口
/// </summary>
[TraceFilter]
[HttpHost("http://localhost")]
public interface IUserApi : IHttpApi
{
[HttpGet]
[Timeout(10 * 1000)]
Task<string> Get1([Uri] string url, string something);

[HttpGet]
[JsonReturn]
ITask<HttpResponseMessage> Get2([Required]string id, CancellationToken token);

[HttpGet]
ITask<Stream> Get3([Required]string account, CancellationToken token);

[HttpGet]
ITask<HttpResponseFile> Get4([Uri, Required] string uri);


[HttpGet]
ITask<object> Get5(string nickName);

[HttpGet]
ITask<byte[]> Get6(string nickName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ protected override async Task<object> GetTaskResult(ApiActionContext context)
var response = context.ResponseMessage;
var dataType = context.ApiActionDescriptor.Return.DataType;

if (dataType.Type == typeof(HttpResponseMessage))
if (dataType.IsHttpResponseMessage == true)
{
return response;
}

if (dataType.Type == typeof(string))
if (dataType.IsString == true)
{
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}

if (dataType.Type == typeof(byte[]))
if (dataType.IsByteArray == true)
{
return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}

if (dataType.Type == typeof(Stream))
if (dataType.IsStream == true)
{
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
Expand Down
45 changes: 34 additions & 11 deletions WebApiClient/Contexts/DataTypeDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Reflection;

namespace WebApiClient.Contexts
Expand All @@ -20,11 +22,6 @@ public class DataTypeDescriptor
/// </summary>
public Type Type { get; protected set; }

/// <summary>
/// 获取是否为HttpResponseWrapper子类型
/// </summary>
public bool IsHttpResponseWrapper { get; protected set; }

/// <summary>
/// 获取包装为ITask的创建工厂
/// </summary>
Expand All @@ -35,23 +32,49 @@ public class DataTypeDescriptor
/// </summary>
public ConstructorInfo ITaskConstructor { get; protected set; }


/// <summary>
/// 获取是否为String类型
/// </summary>
public bool IsString { get; protected set; }

/// <summary>
/// 获取是否为Stream类型
/// </summary>
public bool IsStream { get; protected set; }

/// <summary>
/// 获取是否为byte[]类型
/// </summary>
public bool IsByteArray { get; protected set; }

/// <summary>
/// 获取是否为HttpResponseMessage类型
/// </summary>
public bool IsHttpResponseMessage { get; protected set; }

/// <summary>
/// 获取是否为HttpResponseWrapper子类型
/// </summary>
public bool IsHttpResponseWrapper { get; protected set; }

/// <summary>
/// 返回的Task(Of T)的T类型描述
/// </summary>
/// <param name="dataType">数据类型</param>
/// <exception cref="ArgumentNullException"></exception>
public DataTypeDescriptor(Type dataType)
{
if (dataType == null)
{
throw new ArgumentNullException(nameof(dataType));
}
this.Type = dataType ?? throw new ArgumentNullException(nameof(dataType));

var taskType = typeof(ApiTask<>).MakeGenericType(dataType);

this.Type = dataType;
this.ITaskFactory = Lambda.CreateNewFunc<ITask>(taskType);
this.ITaskConstructor = taskType.GetConstructor(emptyTypes);

this.IsString = dataType == typeof(string);
this.IsStream = dataType == typeof(Stream);
this.IsByteArray = dataType == typeof(byte[]);
this.IsHttpResponseMessage = dataType == typeof(HttpResponseMessage);
this.IsHttpResponseWrapper = dataType.IsInheritFrom<HttpResponseWrapper>();
}
}
Expand Down

0 comments on commit 4f308e1

Please sign in to comment.