-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathTestRequestContext.cs
34 lines (30 loc) · 1.37 KB
/
TestRequestContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using WebApiClientCore.Implementations;
namespace WebApiClientCore.Test
{
public class TestRequestContext : ApiRequestContext
{
/// <summary>
/// 请求Api的上下文
/// </summary>
/// <param name="httpApi">httpApi代理类实例</param>
/// <param name="httpApiConfig">关联的HttpApiConfig</param>
/// <param name="apiActionDescriptor">关联的ApiActionDescriptor</param>
/// <exception cref="ArgumentNullException"></exception>
public TestRequestContext(ApiActionDescriptor apiActionDescriptor, params object[] args)
: base(GetHttpContext(), apiActionDescriptor, args, new DefaultDataCollection())
{
this.HttpContext.ResponseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
}
private static HttpContext GetHttpContext()
{
var services = new ServiceCollection();
var requestServices = services.BuildServiceProvider();
var options = new HttpApiOptions() { HttpHost = new Uri("http://www.webapi.com/") };
var httpClientContext = new HttpClientContext(new HttpClient(), requestServices, options, string.Empty);
return new HttpContext(httpClientContext, new HttpApiRequestMessageImpl());
}
}
}