forked from NetCoreStack/Proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHelper.cs
More file actions
138 lines (116 loc) · 5.19 KB
/
Copy pathTestHelper.cs
File metadata and controls
138 lines (116 loc) · 5.19 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using Moq;
using NetCoreStack.Mvc.Helpers;
using NetCoreStack.Proxy.Test.Contracts;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace NetCoreStack.Proxy.Tests
{
public static class TestHelper
{
public static HttpContext HttpContext { get; set; }
public static IConfiguration Configuration { get; set; }
private static void CreateHttpContext(IServiceProvider serviceProvider)
{
var contentType = "application/json; charset=utf-8";
var request = new Mock<HttpRequest>();
var response = new Mock<HttpResponse>();
var items = new Dictionary<object, object>();
var cookies = new Dictionary<string, string>();
var responseHeaders = new HeaderDictionary();
var headers = new HeaderDictionary();
headers.Add(ContextBaseExtensions.ClientUserAgentHeader, new StringValues("Chrome/63.0.3239.84 Safari/537.36"));
request.SetupGet(x => x.Cookies).Returns(new RequestCookieCollection(cookies));
request.SetupGet(r => r.Headers).Returns(headers);
request.SetupGet(f => f.ContentType).Returns(contentType);
response.SetupGet(r => r.Headers).Returns(responseHeaders);
var httpContext = new Mock<HttpContext>();
httpContext.Setup(c => c.RequestServices).Returns(serviceProvider);
httpContext.SetupGet(c => c.Request).Returns(request.Object);
httpContext.SetupGet(c => c.Items).Returns(items);
httpContext.Setup(c => c.Response).Returns(response.Object);
HttpContext = httpContext.Object;
}
static TestHelper()
{
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
services.AddSingleton<IHttpContextAccessor>(resolver => new HttpContextAccessor
{
HttpContext = HttpContext
});
var env = new HostingEnvironment
{
ContentRootPath = Directory.GetCurrentDirectory()
};
services.AddSingleton(env);
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
services.AddNetCoreProxy(Configuration, options =>
{
options.DefaultHeaders.Add("X-NetCoreStack-Header", "ProxyHeaderValue");
options.RegisterFilter<CustomProxyContextFilter>();
options.Register<IGuidelineApi>();
options.Register<IFileProxyApi>();
options.Register<IConsulApi>();
options.Register<ISelfApi>();
options.Register<IAttachmentApi>();
options.Register<ISmsApi>();
options.CultureFactory = () =>
{
var thread = System.Threading.Thread.CurrentThread;
return thread.CurrentCulture;
};
});
CreateHttpContext(services.BuildServiceProvider());
}
public static FormFile GetFormFile(string name, string fileName = "")
{
var content = "some text content";
var bytes = Encoding.UTF8.GetBytes(content);
var length = bytes.Length;
var ms = new MemoryStream(bytes);
fileName = string.IsNullOrEmpty(fileName) ? "some_text_file.txt" : fileName;
var formFile = new FormFile(ms, 0, length, name, fileName)
{
Headers = new HeaderDictionary
{
[HeaderNames.ContentType] = "text/plain",
[HeaderNames.ContentDisposition] = $"form-data; name=\"{name}\"; filename=\"{fileName}\""
}
};
return formFile;
}
public static FormFile GetFormFile(string name, string fileName, string contentType = null)
{
var bytes = File.ReadAllBytes(fileName);
var length = bytes.Length;
var ms = new MemoryStream(bytes);
contentType = contentType ?? MimeTypeHelper.Resolve(Path.GetExtension(fileName));
var formFile = new FormFile(ms, 0, length, name, fileName)
{
Headers = new HeaderDictionary
{
[HeaderNames.ContentType] = contentType,
[HeaderNames.ContentDisposition] = $"form-data; name=\"{name}\"; filename=\"{fileName}\""
}
};
return formFile;
}
}
}