Skip to content

Commit 0b23741

Browse files
committed
Merge branch 'release_v1.3'
2 parents 8a68727 + 3fbab53 commit 0b23741

File tree

13 files changed

+173
-17
lines changed

13 files changed

+173
-17
lines changed

Example/App/App.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
<None Update="Config\Api\User\Response\searchUsersPage1.json">
4040
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4141
</None>
42+
<None Update="Config\Api\User\Response\searchUsersPage2.json">
43+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
44+
</None>
4245
</ItemGroup>
4346
<ItemGroup>
4447
<ProjectReference Include="..\..\Src\FakeApi\FakeApi.csproj" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"results": [
3+
{
4+
"id": 2048,
5+
"firstname": "mickael",
6+
"lastname": "jordan"
7+
},
8+
{
9+
"id": 4096,
10+
"firstname": "kirk",
11+
"lastname": "hammett"
12+
}
13+
]
14+
}

Example/App/Config/Api/User/usersApi.cfg.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@
5757
{
5858
"url": "https://localhost/api/users?pIndex={0}&pSize={1}",
5959
"responses": [
60-
{
61-
"active": 0,
62-
"file": "Config/Api/User/Response/searchUsersPage0.json"
63-
},
6460
{
6561
"active": 1,
66-
"file": "Config/Api/User/Response/searchUsersPage1.json"
62+
"delay": 500,
63+
"files": [
64+
"Config/Api/User/Response/searchUsersPage0.json",
65+
"Config/Api/User/Response/searchUsersPage1.json",
66+
"Config/Api/User/Response/searchUsersPage2.json"
67+
]
6768
}
6869
]
6970
}

Example/App/UserRequestExample.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ public static class UserRequestExample
1111
public static void Start(IHttpRequester httpRequester)
1212
{
1313
PostUser(httpRequester);
14-
GetUserById(httpRequester);
14+
GetUserById(httpRequester);
15+
//Multiple results files
16+
SearchUser(httpRequester);
17+
SearchUser(httpRequester);
18+
SearchUser(httpRequester);
19+
SearchUser(httpRequester);
1520
SearchUser(httpRequester);
1621
}
1722

FakeApiTest/FakeHttpRequesterTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ namespace FakeApiTest
1010
[TestClass]
1111
public class FakeHttpRequesterTests
1212
{
13+
[TestInitialize]
14+
public void SetUp()
15+
{
16+
ConfigIO._config = null;
17+
}
18+
19+
[TestCleanup]
20+
public void TearDown()
21+
{
22+
ConfigIO._config = null;
23+
}
24+
1325
[TestMethod]
1426
[DataRow(true)]
1527
[DataRow(false)]
@@ -347,5 +359,25 @@ public void CreateResponseStream_ShouldThrowExceptionWhenFileNotExists()
347359
//Assert
348360
Assert.AreEqual("File FakeFile not exists", ex.Message);
349361
}
362+
363+
[TestMethod]
364+
public void CreateResponseStream_ShouldCallGetNextFile()
365+
{
366+
//Arrange
367+
var fakeRequester = new FakeHttpRequester("Files/Config/Api/api.cfg.json");
368+
var request = WebRequest.Create("https://localhost/api/users?pIndex=0&pSize=2");
369+
370+
//Act
371+
var response1 = fakeRequester.GetResponse(request);
372+
var response2 = fakeRequester.GetResponse(request);
373+
374+
//Assert
375+
using (var stream1 = new StreamReader(response1.GetResponseStream()))
376+
using (var stream2 = new StreamReader(response2.GetResponseStream()))
377+
{
378+
Assert.AreEqual(File.ReadAllText("Files/Config/Api/usersApi.cfg.json"), stream1.ReadToEnd());
379+
Assert.AreEqual(File.ReadAllText("Files/Config/Api/ordersApi.cfg.json"), stream2.ReadToEnd());
380+
}
381+
}
350382
}
351383
}

FakeApiTest/Files/Config/Api/usersApi.cfg.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@
3434
"responses": [
3535
{
3636
"active": 1,
37-
"file": "Config/Api/User/Response/searchUsersPage0.json"
38-
},
39-
{
40-
"active": 0,
41-
"file": "Config/Api/User/Response/searchUsersPage1.json"
37+
"files": [
38+
"Files/Config/Api/usersApi.cfg.json",
39+
"Files/Config/Api/ordersApi.cfg.json"
40+
]
4241
}
4342
]
4443
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FakeApi;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
namespace FakeApiTest
8+
{
9+
[TestClass]
10+
public class HttpResponseMockTest
11+
{
12+
[TestMethod]
13+
public void GetNextFile_ShouldReturnNextFileAndReset()
14+
{
15+
//Arrange
16+
var response = new HttpResponseMock();
17+
response.Files = new List<string>
18+
{
19+
"1",
20+
"2",
21+
"3"
22+
};
23+
24+
//Act/Assert
25+
Assert.AreEqual("1", response.GetNextFile());
26+
Assert.AreEqual("2", response.GetNextFile());
27+
Assert.AreEqual("3", response.GetNextFile());
28+
Assert.AreEqual("1", response.GetNextFile());
29+
}
30+
31+
[TestMethod]
32+
public void GetNextFile_ShouldThrowInvalidExceptionWhenFilesIsNull()
33+
{
34+
//Arrange
35+
var response = new HttpResponseMock();
36+
response.Files = null;
37+
38+
//Act
39+
var ex = Assert.ThrowsException<InvalidOperationException>(() =>
40+
{
41+
response.GetNextFile();
42+
});
43+
}
44+
45+
[TestMethod]
46+
public void GetNextFile_ShouldThrowInvalidExceptionWhenFilesIsEmpty()
47+
{
48+
//Arrange
49+
var response = new HttpResponseMock();
50+
response.Files = new List<string>();
51+
52+
//Act
53+
var ex = Assert.ThrowsException<InvalidOperationException>(() =>
54+
{
55+
response.GetNextFile();
56+
});
57+
}
58+
}
59+
}

Src/FakeApi/Config.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public void Validate()
106106
}
107107

108108
if(string.IsNullOrEmpty(DefaultResponse)
109-
&& (!responses.Any() || responses.Any(r => !r.HasFile && !r.HasWebException && !r.HasCustomException && string.IsNullOrEmpty(r.Response))))
109+
&& (!responses.Any()
110+
|| responses.Any(r => !r.HasFile && !r.HasFiles && !r.HasWebException && !r.HasCustomException && string.IsNullOrEmpty(r.Response))))
110111
{
111112
throw new Exception("At least one api has no response configured");
112113
}

Src/FakeApi/ConfigIO.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ namespace FakeApi
88
{
99
internal static class ConfigIO
1010
{
11+
internal static Config _config;
12+
1113
public static Config GetConfig(string configSource)
1214
{
13-
var config = LoadConfig(configSource);
14-
MergeApis(config, configSource);
15-
config.Validate();
16-
return config;
15+
if(_config != null)
16+
{
17+
return _config;
18+
}
19+
20+
_config = LoadConfig(configSource);
21+
MergeApis(_config, configSource);
22+
_config.Validate();
23+
return _config;
1724
}
1825

1926
public static Config LoadConfig(string configSource)

Src/FakeApi/FakeHttpRequester.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ internal static MemoryStream CreateResponseStream(Config config, HttpResponseMoc
115115
streamReader.BaseStream.CopyTo(responseStream);
116116
}
117117
}
118+
else if(apiResponse.HasFiles)
119+
{
120+
var file = apiResponse.GetNextFile();
121+
if (!File.Exists(file))
122+
{
123+
throw new FileLoadException($"File {file} not exists");
124+
}
125+
126+
using (var streamReader = new StreamReader(file))
127+
{
128+
streamReader.BaseStream.CopyTo(responseStream);
129+
}
130+
}
118131
else
119132
{
120133
var expectedBytes = Encoding.UTF8.GetBytes(apiResponse.Response ?? config.DefaultResponse);

0 commit comments

Comments
 (0)