Skip to content

Commit 7b7cc86

Browse files
authored
Pre 1.12.0 (#142)
* Update nugets and package information * Add JsonExceptionResponse extension method * Missing file... * Refactor unit testing * Add NET47 unit test to Appveyor * Change to SWAN Lite * Revert
1 parent 72bac14 commit 7b7cc86

40 files changed

+588
-702
lines changed

README.md

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -207,22 +207,9 @@ public class PeopleController : WebApiController
207207
}
208208
catch (Exception ex)
209209
{
210-
return HandleError(context, ex, (int)HttpStatusCode.InternalServerError);
210+
return context.JsonExceptionResponse(ex);
211211
}
212212
}
213-
214-
protected bool HandleError(HttpListenerContext context, Exception ex, int statusCode = 500)
215-
{
216-
var errorResponse = new
217-
{
218-
Title = "Unexpected Error",
219-
ErrorCode = ex.GetType().Name,
220-
Description = ex.ExceptionMessage(),
221-
};
222-
223-
context.Response.StatusCode = statusCode;
224-
return context.JsonResponse(errorResponse);
225-
}
226213
}
227214
```
228215

@@ -250,22 +237,9 @@ public class PeopleController : WebApiController
250237
}
251238
catch (Exception ex)
252239
{
253-
return HandleError(context, ex, (int)HttpStatusCode.InternalServerError);
240+
return context.JsonExceptionResponse(ex);
254241
}
255242
}
256-
257-
protected bool HandleError(HttpListenerContext context, Exception ex, int statusCode = 500)
258-
{
259-
var errorResponse = new
260-
{
261-
Title = "Unexpected Error",
262-
ErrorCode = ex.GetType().Name,
263-
Description = ex.ExceptionMessage(),
264-
};
265-
266-
context.Response.StatusCode = statusCode;
267-
return context.JsonResponse(errorResponse);
268-
}
269243
}
270244
```
271245

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ build_script:
3434
- msbuild /p:Configuration=%CONFIGURATION% Unosquare.Labs.EmbedIO.sln
3535
test_script:
3636
- dotnet test test\Unosquare.Labs.EmbedIO.Tests\Unosquare.Labs.EmbedIO.Tests.csproj -c %CONFIGURATION% -f netcoreapp2.0
37+
- dotnet test test\Unosquare.Labs.EmbedIO.Tests\Unosquare.Labs.EmbedIO.Tests.csproj -c %CONFIGURATION% -f net47
3738
- tools\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"%ProgramFiles%\dotnet\dotnet.exe" -targetargs:"test test\Unosquare.Labs.EmbedIO.Tests\Unosquare.Labs.EmbedIO.Tests.csproj -c %CONFIGURATION% -f net46" -output:coverage.xml -filter:"+[Unosquare.Labs.EmbedIO*]* -[Unosquare.Labs.EmbedIO.Test*]*" -register:userdotnet
3839
- ps: |
3940
if(-Not $env:APPVEYOR_PULL_REQUEST_TITLE)

src/Unosquare.Labs.EmbedIO.Samples/PeopleController.cs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
using Modules;
88
using System.Threading.Tasks;
99
using Tubular;
10-
using Swan;
1110
using Tubular.ObjectModel;
1211
#if NET47
1312
using System.Net;
1413
#else
15-
using Unosquare.Net;
14+
using Net;
1615
#endif
1716

17+
/// <inheritdoc />
1818
/// <summary>
1919
/// A very simple controller to handle People CRUD.
2020
/// Notice how it Inherits from WebApiController and the methods have WebApiHandler attributes
@@ -67,10 +67,7 @@ public bool GetPeople(WebServer server, HttpListenerContext context)
6767
}
6868
catch (Exception ex)
6969
{
70-
// here the error handler will respond with a generic 500 HTTP code a JSON-encoded object
71-
// with error info. You will need to handle HTTP status codes correctly depending on the situation.
72-
// For example, for keys that are not found, ou will need to respond with a 404 status code.
73-
return HandleError(context, ex);
70+
return context.JsonExceptionResponse(ex);
7471
}
7572
}
7673

@@ -99,7 +96,7 @@ public async Task<bool> PostPeople(WebServer server, HttpListenerContext context
9996
}
10097
catch (Exception ex)
10198
{
102-
return HandleError(context, ex);
99+
return context.JsonExceptionResponse(ex);
103100
}
104101
}
105102

@@ -120,28 +117,8 @@ public bool Echo(WebServer server, HttpListenerContext context)
120117
}
121118
catch (Exception ex)
122119
{
123-
return HandleError(context, ex);
120+
return context.JsonExceptionResponse(ex);
124121
}
125122
}
126-
127-
/// <summary>
128-
/// Handles the error returning an error status code and json-encoded body.
129-
/// </summary>
130-
/// <param name="context">The context.</param>
131-
/// <param name="ex">The ex.</param>
132-
/// <param name="statusCode">The HTTP status code.</param>
133-
/// <returns></returns>
134-
protected bool HandleError(HttpListenerContext context, Exception ex, int statusCode = 500)
135-
{
136-
var errorResponse = new
137-
{
138-
Title = "Unexpected Error",
139-
ErrorCode = ex.GetType().Name,
140-
Description = ex.ExceptionMessage(),
141-
};
142-
143-
context.Response.StatusCode = statusCode;
144-
return context.JsonResponse(errorResponse);
145-
}
146123
}
147124
}

src/Unosquare.Labs.EmbedIO.Samples/Person.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Unosquare.Labs.EmbedIO.Samples
55
using Swan;
66
using System.ComponentModel.DataAnnotations.Schema;
77

8+
/// <inheritdoc />
89
/// <summary>
910
/// A simple model representing a person
1011
/// </summary>

src/Unosquare.Labs.EmbedIO.Samples/StaticFilesSample.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public static void Setup(WebServer server, bool useGzip)
4343
server.Module<StaticFilesModule>().UseRamCache = false;
4444
server.Module<StaticFilesModule>().DefaultExtension = ".html";
4545
server.Module<StaticFilesModule>().UseGzip = useGzip;
46-
// We don't need to add the line below. The default document is always index.html.
47-
//server.Module<StaticFilesWebModule>().DefaultDocument = "index.html";
4846
}
4947
}
5048
}

src/Unosquare.Labs.EmbedIO.Samples/Unosquare.Labs.EmbedIO.Samples.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="LiteLib" Version="0.15.0" />
22+
<PackageReference Include="LiteLib" Version="0.15.2" />
2323
<PackageReference Include="Tubular.ServerSide" Version="1.4.5" />
2424
</ItemGroup>
2525

src/Unosquare.Labs.EmbedIO.Samples/WebSocketsSample.cs

Lines changed: 25 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static void Setup(WebServer server)
2626
}
2727
}
2828

29+
/// <inheritdoc />
2930
/// <summary>
3031
/// Defines a very simple chat server
3132
/// </summary>
@@ -37,13 +38,8 @@ public WebSocketsChatServer()
3738
{
3839
// placeholder
3940
}
40-
41-
/// <summary>
42-
/// Called when this WebSockets Server receives a full message (EndOfMessage) form a WebSockets client.
43-
/// </summary>
44-
/// <param name="context">The context.</param>
45-
/// <param name="rxBuffer">The rx buffer.</param>
46-
/// <param name="rxResult">The rx result.</param>
41+
42+
/// <inheritdoc />
4743
protected override void OnMessageReceived(WebSocketContext context, byte[] rxBuffer,
4844
WebSocketReceiveResult rxResult)
4945
{
@@ -53,29 +49,18 @@ protected override void OnMessageReceived(WebSocketContext context, byte[] rxBuf
5349
}
5450
}
5551

56-
/// <summary>
57-
/// Gets the name of the server.
58-
/// </summary>
59-
/// <value>
60-
/// The name of the server.
61-
/// </value>
52+
53+
/// <inheritdoc />
6254
public override string ServerName => nameof(WebSocketsChatServer);
6355

64-
#if NET47 /// <summary>
65-
/// Called when this WebSockets Server accepts a new WebSockets client.
66-
/// </summary>
67-
/// <param name="context">The context.</param>
68-
/// <param name="localEndPoint">The local endpoint.</param>
69-
/// /// <param name="remoteEndPoint">The remote endpoint.</param>
56+
#if NET47
57+
/// <inheritdoc />
7058
protected override void OnClientConnected(
7159
WebSocketContext context,
7260
System.Net.IPEndPoint localEndPoint,
7361
System.Net.IPEndPoint remoteEndPoint)
7462
#else
75-
/// <summary>
76-
/// Called when this WebSockets Server accepts a new WebSockets client.
77-
/// </summary>
78-
/// <param name="context">The context.</param>
63+
/// <inheritdoc />
7964
protected override void OnClientConnected(WebSocketContext context)
8065
#endif
8166
{
@@ -86,23 +71,15 @@ protected override void OnClientConnected(WebSocketContext context)
8671
Send(ws, "Someone joined the chat room.");
8772
}
8873
}
89-
90-
/// <summary>
91-
/// Called when this WebSockets Server receives a message frame regardless if the frame represents the EndOfMessage.
92-
/// </summary>
93-
/// <param name="context">The context.</param>
94-
/// <param name="rxBuffer">The rx buffer.</param>
95-
/// <param name="rxResult">The rx result.</param>
74+
75+
/// <inheritdoc />
9676
protected override void OnFrameReceived(WebSocketContext context, byte[] rxBuffer,
9777
WebSocketReceiveResult rxResult)
9878
{
9979
return;
10080
}
101-
102-
/// <summary>
103-
/// Called when the server has removed a WebSockets connected client for any reason.
104-
/// </summary>
105-
/// <param name="context">The context.</param>
81+
82+
/// <inheritdoc />
10683
protected override void OnClientDisconnected(WebSocketContext context)
10784
{
10885
Broadcast("Someone left the chat room.");
@@ -111,7 +88,8 @@ protected override void OnClientDisconnected(WebSocketContext context)
11188
#endif
11289
}
11390
}
114-
91+
92+
/// <inheritdoc />
11593
/// <summary>
11694
/// Define a command-line interface terminal
11795
/// </summary>
@@ -123,13 +101,8 @@ public class WebSocketsTerminalServer : WebSocketsServer
123101

124102
// The SyncRoot is used to send 1 thing at a time and multithreaded Processes dictionary.
125103
private readonly object _syncRoot = new object();
126-
127-
/// <summary>
128-
/// Called when this WebSockets Server receives a full message (EndOfMessage) form a WebSockets client.
129-
/// </summary>
130-
/// <param name="context">The context.</param>
131-
/// <param name="rxBuffer">The rx buffer.</param>
132-
/// <param name="rxResult">The rx result.</param>
104+
105+
/// <inheritdoc />
133106
protected override void OnMessageReceived(WebSocketContext context, byte[] rxBuffer,
134107
WebSocketReceiveResult rxResult)
135108
{
@@ -139,13 +112,8 @@ protected override void OnMessageReceived(WebSocketContext context, byte[] rxBuf
139112
_processes[context].StandardInput.WriteLine(arg);
140113
}
141114
}
142-
143-
/// <summary>
144-
/// Called when this WebSockets Server receives a message frame regardless if the frame represents the EndOfMessage.
145-
/// </summary>
146-
/// <param name="context">The context.</param>
147-
/// <param name="rxBuffer">The rx buffer.</param>
148-
/// <param name="rxResult">The rx result.</param>
115+
116+
/// <inheritdoc />
149117
protected override void OnFrameReceived(WebSocketContext context, byte[] rxBuffer,
150118
WebSocketReceiveResult rxResult)
151119
{
@@ -170,21 +138,14 @@ private WebSocketContext FindContext(Process p)
170138
return null;
171139
}
172140

173-
#if NET47 /// <summary>
174-
/// Called when this WebSockets Server accepts a new WebSockets client.
175-
/// </summary>
176-
/// <param name="context">The context.</param>
177-
/// <param name="localEndPoint">The local endpoint.</param>
178-
/// <param name="remoteEndPoint">The remote endpoint.</param>
141+
#if NET47
142+
/// <inheritdoc />
179143
protected override void OnClientConnected(
180144
WebSocketContext context,
181145
System.Net.IPEndPoint localEndPoint,
182146
System.Net.IPEndPoint remoteEndPoint)
183147
#else
184-
/// <summary>
185-
/// Called when this WebSockets Server accepts a new WebSockets client.
186-
/// </summary>
187-
/// <param name="context">The context.</param>
148+
/// <inheritdoc />
188149
protected override void OnClientConnected(WebSocketContext context)
189150
#endif
190151
{
@@ -251,11 +212,8 @@ protected override void OnClientConnected(WebSocketContext context)
251212
process.BeginOutputReadLine();
252213

253214
}
254-
255-
/// <summary>
256-
/// Called when the server has removed a WebSockets connected client for any reason.
257-
/// </summary>
258-
/// <param name="context">The context.</param>
215+
216+
/// <inheritdoc />
259217
protected override void OnClientDisconnected(WebSocketContext context)
260218
{
261219
lock (_syncRoot)
@@ -264,13 +222,8 @@ protected override void OnClientDisconnected(WebSocketContext context)
264222
_processes[context].Kill();
265223
}
266224
}
267-
268-
/// <summary>
269-
/// Gets the name of the server.
270-
/// </summary>
271-
/// <value>
272-
/// The name of the server.
273-
/// </value>
225+
226+
/// <inheritdoc />
274227
public override string ServerName => nameof(WebSocketsTerminalServer);
275228
}
276229
}

0 commit comments

Comments
 (0)