Skip to content

Commit d7bc688

Browse files
committed
Proxy tests, put - post proper body content, offset parameters
1 parent d9770f0 commit d7bc688

File tree

10 files changed

+222
-23
lines changed

10 files changed

+222
-23
lines changed
Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
1-
namespace NetCoreStack.Proxy.Extensions
1+
using System.Collections.Generic;
2+
3+
namespace NetCoreStack.Proxy.Extensions
24
{
35
internal static class CollectionExtensions
46
{
7+
internal static IEnumerable<ProxyModelMetadata> ToFlat(this ProxyModelMetadata modelMetadata)
8+
{
9+
List<ProxyModelMetadata> list = new List<ProxyModelMetadata>();
10+
var stack = new Stack<IEnumerator<ProxyModelMetadata>>();
11+
var enumerator = modelMetadata.Properties.GetEnumerator();
12+
while (true)
13+
{
14+
if (enumerator.MoveNext())
15+
{
16+
ProxyModelMetadata element = enumerator.Current;
17+
yield return element;
518

19+
stack.Push(enumerator);
20+
enumerator = element.Properties.GetEnumerator();
21+
}
22+
else if (stack.Count > 0)
23+
{
24+
enumerator.Dispose();
25+
enumerator = stack.Pop();
26+
}
27+
else
28+
{
29+
yield break;
30+
}
31+
}
32+
}
633
}
7-
}
34+
}

src/NetCoreStack.Proxy/Internal/DefaultProxyTypeManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ private IList<ProxyDescriptor> GetProxyDescriptors()
7575
"Remove FromBody attribute to proper model binding.");
7676
}
7777
}
78+
else
79+
{
80+
var properties = modelMetadata.ToFlat().ToList();
81+
if (properties.Any(p => p.IsFormFile))
82+
{
83+
isMultipartFormData = true;
84+
}
85+
}
7886

7987
proxyMethodDescriptor.Parameters.Add(modelMetadata);
8088
}

test/NetCoreStack.Proxy.Mvc.Hosting/Controllers/GuidelineController.cs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.Extensions.Logging;
34
using NetCoreStack.Contracts;
45
using NetCoreStack.Proxy.Test.Contracts;
56
using Newtonsoft.Json;
67
using System;
78
using System.Collections.Generic;
9+
using System.IO;
810
using System.Threading.Tasks;
911

1012
namespace NetCoreStack.Proxy.Mvc.Hosting.Controllers
1113
{
1214
[Route("api/[controller]")]
1315
public class GuidelineController : Controller, IGuidelineApi
1416
{
17+
private readonly IHostingEnvironment _hostingEnvironment;
1518
private readonly ILoggerFactory _loggerFactory;
1619
private readonly ILogger _logger;
1720

18-
public GuidelineController(ILoggerFactory loggerFactory)
21+
public GuidelineController(IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory)
1922
{
23+
_hostingEnvironment = hostingEnvironment;
2024
_loggerFactory = loggerFactory;
2125
_logger = _loggerFactory.CreateLogger<GuidelineController>();
2226
}
@@ -73,6 +77,24 @@ public async Task TaskActionPost([FromBody]ComplexTypeModel model)
7377
_logger.LogWarning(JsonConvert.SerializeObject(model));
7478
}
7579

80+
[HttpPost(nameof(TaskSingleFileModel))]
81+
public async Task TaskSingleFileModel(SingleFileModel model)
82+
{
83+
await Task.CompletedTask;
84+
85+
var name = model.File.Name;
86+
var fileName = model.File.FileName;
87+
var length = model.File.Length;
88+
89+
using (var ms = new MemoryStream())
90+
{
91+
model.File.CopyTo(ms);
92+
System.IO.File.WriteAllBytes(Path.Combine(_hostingEnvironment.ContentRootPath, fileName), ms.ToArray());
93+
}
94+
95+
_logger.LogWarning(JsonConvert.SerializeObject(new { name, fileName, length }));
96+
}
97+
7698
[HttpPut("kv")]
7799
public async Task<bool> CreateOrUpdateKey(string key, Bar body)
78100
{
@@ -87,11 +109,38 @@ public Task<bool> CreateOrUpdateKey(string key)
87109
throw new NotImplementedException();
88110
}
89111

112+
[HttpPut(nameof(TaskKeyAndSingleFileModel))]
113+
public Task TaskKeyAndSingleFileModel(string key, SingleFileModel model)
114+
{
115+
throw new NotImplementedException();
116+
}
117+
118+
[HttpPut(nameof(TaskKeySingleFileAndBarModel))]
119+
public Task TaskKeySingleFileAndBarModel(string key, SingleFileModel model, Bar bar)
120+
{
121+
throw new NotImplementedException();
122+
}
123+
90124
[HttpDelete(nameof(TaskActionDelete))]
91125
public async Task TaskActionDelete(long id)
92126
{
93127
await Task.CompletedTask;
94128
_logger.LogWarning(JsonConvert.SerializeObject(new { id }));
95129
}
130+
131+
public Task TaskKeyAndSingleFileAndPropsModel(string key, SingleFileAndPropsModel model)
132+
{
133+
throw new NotImplementedException();
134+
}
135+
136+
public Task TaskEnumerableFileModel(EnumerableFileModel model)
137+
{
138+
throw new NotImplementedException();
139+
}
140+
141+
public Task TaskKeyAndEnumerableFileModel(string key, EnumerableFileModel model)
142+
{
143+
throw new NotImplementedException();
144+
}
96145
}
97146
}

test/NetCoreStack.Proxy.Mvc.Hosting/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:55062/",
6+
"applicationUrl": "http://localhost:5003/",
77
"sslPort": 0
88
}
99
},
@@ -23,7 +23,7 @@
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
2525
},
26-
"applicationUrl": "http://localhost:55063/"
26+
"applicationUrl": "http://localhost:5003/"
2727
}
2828
}
2929
}

test/NetCoreStack.Proxy.Test.Contracts/IGuidelineApi.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ public interface IGuidelineApi : IApiContract
2424
[HttpPostMarker]
2525
Task TaskActionPost(ComplexTypeModel model);
2626

27+
[HttpPostMarker]
28+
Task TaskSingleFileModel(SingleFileModel model);
29+
30+
[HttpPostMarker]
31+
Task TaskEnumerableFileModel(EnumerableFileModel model);
32+
33+
[HttpPutMarker(Template = "TaskKeyAndSingleFileModel/{key}")]
34+
Task TaskKeyAndSingleFileModel(string key, SingleFileModel model);
35+
36+
[HttpPutMarker(Template = "TaskKeyAndMultipleFileModel/{key}")]
37+
Task TaskKeyAndEnumerableFileModel(string key, EnumerableFileModel model);
38+
39+
[HttpPutMarker(Template = "TaskKeyAndSingleFileAndPropsModel/{key}")]
40+
Task TaskKeyAndSingleFileAndPropsModel(string key, SingleFileAndPropsModel model);
41+
42+
[HttpPutMarker(Template = "TaskKeySingleFileAndBarModel/{key}")]
43+
Task TaskKeySingleFileAndBarModel(string key, SingleFileModel model, Bar bar);
44+
2745
/// <summary>
2846
/// Template and parameter usage, key parameter will be part of the request Url and extracting it as api/guideline/kv/<key>
2947
/// </summary>

test/NetCoreStack.Proxy.Test.Contracts/TypesModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ public class SingleFileModel
110110
public IFormFile File { get; set; }
111111
}
112112

113+
public class SingleFileAndPropsModel
114+
{
115+
public string String { get; set; }
116+
public int Int { get; set; }
117+
public IFormFile File { get; set; }
118+
}
119+
113120
public class EnumerableFileModel
114121
{
115122
public IEnumerable<IFormFile> Files { get; set; }

test/NetCoreStack.Proxy.Tests/ModelTypeTests.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,8 @@ public void ProxyModelMetadataInfoForAnonymousObject()
358358
[Fact]
359359
public void ProxyModelMetadataInfoForSingleFile()
360360
{
361-
var content = "some text content";
362-
var bytes = Encoding.UTF8.GetBytes(content);
363-
var length = bytes.Length;
364-
var ms = new MemoryStream();
365-
var formFile = new FormFile(ms, 0, length, "file", "some_text_file.txt");
361+
var formFile = TestHelper.GetFormFile("file");
362+
var length = formFile.Length;
366363
var model = new SingleFileModel
367364
{
368365
File = formFile
@@ -381,11 +378,9 @@ public void ProxyModelMetadataInfoForSingleFile()
381378
[Fact]
382379
public void ProxyModelMetadataInfoForEnumerableFile()
383380
{
384-
var content = "some text content";
385-
var bytes = Encoding.UTF8.GetBytes(content);
386-
var length = bytes.Length;
387-
var ms = new MemoryStream();
388-
var formFile = new FormFile(ms, 0, length, "file", "some_text_file.txt");
381+
var formFile = TestHelper.GetFormFile("file");
382+
var length = formFile.Length;
383+
389384
var model = new EnumerableFileModel
390385
{
391386
Files = new[] { formFile }
@@ -403,11 +398,7 @@ public void ProxyModelMetadataInfoForEnumerableFile()
403398
[Fact]
404399
public void ProxyModelMetadataInfoForFileModel()
405400
{
406-
var content = "some text content";
407-
var bytes = Encoding.UTF8.GetBytes(content);
408-
var length = bytes.Length;
409-
var ms = new MemoryStream();
410-
var formFile = new FormFile(ms, 0, length, "file", "some_text_file.txt");
401+
var formFile = TestHelper.GetFormFile("file");
411402
var model = new FileModel
412403
{
413404
InnerFileModel = new InnerFileModel

test/NetCoreStack.Proxy.Tests/ProxyCreationTests.cs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
using Microsoft.Extensions.Configuration;
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Http.Internal;
3+
using Microsoft.Extensions.Configuration;
24
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Net.Http.Headers;
36
using NetCoreStack.Contracts;
47
using NetCoreStack.Proxy.Test.Contracts;
58
using System;
69
using System.Collections.Generic;
10+
using System.IO;
711
using System.Linq;
12+
using System.Text;
813
using System.Threading.Tasks;
914
using Xunit;
1015

@@ -119,6 +124,78 @@ public async Task GetCollectionStreamTest()
119124
Assert.True(collection.Data.Count() == 5);
120125
}
121126

127+
[Fact]
128+
public async Task TaskSingleFileModel()
129+
{
130+
var guidelineApi = Resolver.GetService<IGuidelineApi>();
131+
132+
var model = new SingleFileModel
133+
{
134+
File = TestHelper.GetFormFile("file")
135+
};
136+
137+
await guidelineApi.TaskSingleFileModel(model);
138+
Assert.True(true);
139+
}
140+
141+
[Fact]
142+
public async Task TaskKeyAndSingleFileModel()
143+
{
144+
var guidelineApi = Resolver.GetService<IGuidelineApi>();
145+
146+
var model = new SingleFileModel
147+
{
148+
File = TestHelper.GetFormFile("file")
149+
};
150+
151+
await guidelineApi.TaskKeyAndSingleFileModel(_someKey, model);
152+
Assert.True(true);
153+
}
154+
155+
[Fact]
156+
public async Task TaskKeyAndSingleFileAndPropsModel()
157+
{
158+
var guidelineApi = Resolver.GetService<IGuidelineApi>();
159+
160+
var model = new SingleFileAndPropsModel
161+
{
162+
Int = 6,
163+
String = "Some string value!",
164+
File = TestHelper.GetFormFile("file")
165+
};
166+
167+
await guidelineApi.TaskKeyAndSingleFileAndPropsModel(_someKey, model);
168+
Assert.True(true);
169+
}
170+
171+
[Fact]
172+
public async Task TaskEnumerableFileModel()
173+
{
174+
var guidelineApi = Resolver.GetService<IGuidelineApi>();
175+
176+
var model = new EnumerableFileModel
177+
{
178+
Files = new[] { TestHelper.GetFormFile("files", "file1.txt"), TestHelper.GetFormFile("files", "file2.txt") }
179+
};
180+
181+
await guidelineApi.TaskEnumerableFileModel(model);
182+
Assert.True(true);
183+
}
184+
185+
[Fact]
186+
public async Task TaskKeyAndEnumerableFileModel()
187+
{
188+
var guidelineApi = Resolver.GetService<IGuidelineApi>();
189+
190+
var model = new EnumerableFileModel
191+
{
192+
Files = new[] { TestHelper.GetFormFile("files", "file1.txt"), TestHelper.GetFormFile("files", "file2.txt") }
193+
};
194+
195+
await guidelineApi.TaskKeyAndEnumerableFileModel(_someKey, model);
196+
Assert.True(true);
197+
}
198+
122199
[Fact]
123200
public async Task CreateOrUpdateNoBodyKeyTest()
124201
{

test/NetCoreStack.Proxy.Tests/TestHelper.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
using Microsoft.Extensions.Logging;
77
using Microsoft.Extensions.Logging.Abstractions;
88
using Microsoft.Extensions.Primitives;
9+
using Microsoft.Net.Http.Headers;
910
using Moq;
1011
using NetCoreStack.Proxy.Test.Contracts;
1112
using System;
1213
using System.Collections.Generic;
1314
using System.IO;
15+
using System.Text;
1416

1517
namespace NetCoreStack.Proxy.Tests
1618
{
@@ -80,5 +82,25 @@ static TestHelper()
8082

8183
CreateHttpContext(services.BuildServiceProvider());
8284
}
85+
86+
public static FormFile GetFormFile(string name, string fileName = "")
87+
{
88+
var content = "some text content";
89+
var bytes = Encoding.UTF8.GetBytes(content);
90+
var length = bytes.Length;
91+
var ms = new MemoryStream(bytes);
92+
93+
fileName = string.IsNullOrEmpty(fileName) ? "some_text_file.txt" : fileName;
94+
var formFile = new FormFile(ms, 0, length, name, fileName)
95+
{
96+
Headers = new HeaderDictionary
97+
{
98+
[HeaderNames.ContentType] = "text/plain",
99+
[HeaderNames.ContentDisposition] = $"form-data; name=\"{name}\"; filename=\"{fileName}\""
100+
}
101+
};
102+
103+
return formFile;
104+
}
83105
}
84-
}
106+
}
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)