Skip to content

Commit 23b3331

Browse files
committed
Merge branch 'release_v1.2.0' of https://github.com/peephole8062/redisdev-fakeapi into release_v1.2.0
2 parents 24b91fe + b35c6eb commit 23b3331

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

README.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[![Build Status](https://dev.azure.com/redisdev/FakeApi/_apis/build/status/redisdev.FakeApi?branchName=master)](https://dev.azure.com/redisdev/FakeApi/_build/latest?definitionId=1&branchName=master)
22

3+
[![NuGet](https://img.shields.io/nuget/v/FakeApi.svg)](https://www.nuget.org/packages/FakeApi/)
4+
35
# What is FakeApi?
46

57
FakeApi provides the ability to send HttpWebRequest and get HttpWebResponses without a server.
@@ -19,7 +21,8 @@ FakeApi is for you!
1921
# v1.2.0 Release note
2022

2123
- Improvement of configuration files reading
22-
- Adding the capability to split the api configurations files into several files
24+
- Adding the capability to split the api configurations files into multilpe files. Please read 'Organize your configurations files' section
25+
- Adding a default implementation of IHttpRequester
2326

2427
# How to use FakeApi?
2528

@@ -81,6 +84,30 @@ For each web api you can set several actives responses. One per pair url/method.
8184
Note that you can use template segment in your url configuration (/{idUser}).
8285
You can also override all default values defined at the root path in each apis configuration.
8386

87+
- #### Organize your configurations files
88+
89+
You can configure FakeApi in a single file. But you can now split your Apis configurations files into multiple files. To do that, just provide all directories where FakeApi can find api configurations files by this way :
90+
91+
```json
92+
{
93+
"defaultDelay": 250,
94+
"defaultHttpCode": 200,
95+
"defaultMethod": "GET",
96+
"apisDirectories": [
97+
"Config/Api/User"
98+
]
99+
}
100+
```
101+
102+
In summary, I advise you as in the example below :
103+
- declare a main file (api.cfg.json) with all default values that you need and all api configurations files directories
104+
- create one configuration file per api type (usersApi.cfg.json, ordersApi.cfg.json etc...)
105+
- and create one json response file per request (getUserById.json, postUser1.json etc...)
106+
107+
![Config example](https://github.com/redisdev/FakeApi/blob/release_v1.2.0/config.png?raw=true)
108+
109+
/!\ Be careful, directories containing api configuration files (such as usersApi.cfg) must not contain any other files! Otherwise FakeApi will try to deserialize them and an exception will be thrown.
110+
84111
- #### How to return data from file?
85112

86113
Simply set the "file" property into your api configuration:
@@ -216,8 +243,7 @@ using (var stream = new StreamReader(getUserResponse.GetResponseStream()))
216243
````
217244
- #### IHttpRequester
218245

219-
In production, replace the FakeHttpRequester implemention of FakeApi by your own implementation.
220-
FakeHttpRequester implements IHttpRequester to provide method to send HttpWebRequest synchronous or asynchronous.
246+
In production, replace the FakeHttpRequester implementation by DefaultHttpRequester provided by FakeApi. Or you can also write your own implementation of IHttpRequester interface.
221247

222248
```csharp
223249

@@ -237,6 +263,31 @@ public interface IHttpRequester
237263
Task<HttpWebResponse> GetResponseAsync(HttpWebRequest request);
238264
}
239265

266+
public class DefaultHttpRequester: IHttpRequester
267+
{
268+
public HttpWebResponse GetResponse(WebRequest request)
269+
{
270+
if(request == null)
271+
{
272+
throw new ArgumentNullException(nameof(request));
273+
}
274+
275+
var response = request.GetResponse();
276+
return response as HttpWebResponse;
277+
}
278+
279+
public async Task<HttpWebResponse> GetResponseAsync(WebRequest request)
280+
{
281+
if (request == null)
282+
{
283+
throw new ArgumentNullException(nameof(request));
284+
}
285+
286+
var response = await request.GetResponseAsync();
287+
return response as HttpWebResponse;
288+
}
289+
}
290+
240291
```
241292

242293
#### List of available default HttpWebResponse properties
@@ -289,7 +340,7 @@ public interface IHttpRequester
289340
- string name
290341
- string value
291342

292-
#### Json configuration full example
343+
#### Single file Json configuration full example
293344

294345
```json
295346

0 commit comments

Comments
 (0)