Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ed5fe1e

Browse filesBrowse files
committed
fix float tests and added JsonRpcHandlerBase
1 parent 7577be0 commit ed5fe1e
Copy full SHA for ed5fe1e

5 files changed

+163-166Lines changed: 163 additions & 166 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎AustinHarris.JsonRpc.AspNet/AustinHarris.JsonRpc.AspNet.csproj‎

Copy file name to clipboardExpand all lines: AustinHarris.JsonRpc.AspNet/AustinHarris.JsonRpc.AspNet.csproj
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
</ItemGroup>
4848
<ItemGroup>
4949
<Compile Include="JsonRpcHandler.cs" />
50+
<Compile Include="JsonRpcHandlerBase.cs" />
5051
<Compile Include="Properties\AssemblyInfo.cs" />
5152
</ItemGroup>
5253
<ItemGroup>
Collapse file
+9-128Lines changed: 9 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,16 @@
1-
using System;
2-
using System.IO;
3-
using System.IO.Compression;
4-
using System.Text;
5-
using System.Web;
1+
using AustinHarris.JsonRpc.AspNet;
62

73
namespace AustinHarris.JsonRpc.Handlers.AspNet
84
{
9-
public class JsonRpcHandler : IHttpAsyncHandler
5+
/// <summary>
6+
/// Used default SessionId
7+
/// For routing use JsonRpcHandlerBase
8+
/// </summary>
9+
public class JsonRpcHandler : JsonRpcHandlerBase
1010
{
11-
#region Fields
12-
/// <summary>
13-
/// UTF8 Encoding without BOM.
14-
/// </summary>
15-
static Encoding UTF8Encoding = new UTF8Encoding(false);
16-
#endregion
17-
18-
#region IHttpHandler Members
19-
20-
public bool IsReusable
21-
{
22-
get { return true; }
23-
}
24-
25-
public void ProcessRequest(HttpContext context)
26-
{
27-
// not used
28-
}
29-
30-
#endregion
31-
32-
#region IHttpAsyncHandler Members
33-
34-
/// <summary>
35-
/// Initiates an asynchronous call to the HTTP handler.
36-
/// </summary>
37-
/// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
38-
/// <param name="cb">The <see cref="T:System.AsyncCallback"/> to call when the asynchronous method call is complete. If <paramref name="cb"/> is null, the delegate is not called.</param>
39-
/// <param name="extraData">Any extra data needed to process the request.</param>
40-
/// <returns>
41-
/// An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.
42-
/// </returns>
43-
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
44-
{
45-
var async = new JsonRpcStateAsync(cb, context);
46-
async.JsonRpc = GetJsonRpcString(context.Request);
47-
JsonRpcProcessor.Process(async,context.Request);
48-
return async;
49-
}
50-
51-
private static string GetJsonRpcString(System.Web.HttpRequest request)
11+
protected override string GetSessionId()
5212
{
53-
string json = string.Empty;
54-
if (request.RequestType == "GET")
55-
{
56-
json = request.Params["jsonrpc"] ?? string.Empty;
57-
}
58-
else if (request.RequestType == "POST")
59-
{
60-
if (request.ContentType == "application/x-www-form-urlencoded")
61-
{
62-
json = request.Params["jsonrpc"] ?? string.Empty;
63-
}
64-
else
65-
{
66-
json = new StreamReader(request.InputStream).ReadToEnd();
67-
}
68-
}
69-
return json;
13+
return Handler.DefaultSessionId();
7014
}
71-
72-
/// <summary>
73-
/// Provides an asynchronous process End method when the process ends.
74-
/// </summary>
75-
/// <param name="result">An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.</param>
76-
public void EndProcessRequest(IAsyncResult result)
77-
{
78-
var state = result as JsonRpcStateAsync;
79-
if (state != null)
80-
{
81-
var r = state.Result;
82-
var callback = ((HttpContext)state.AsyncState).Request.Params["callback"];
83-
if (!string.IsNullOrWhiteSpace(callback))
84-
{
85-
r = string.Format("{0}({1})", callback, r);
86-
}
87-
88-
// try to compress the response data.
89-
// fix me: compression filters in IHttpModule always failed for IHttpAsyncHandler
90-
CompressResponseIfPossible(((HttpContext)state.AsyncState).Request, ((HttpContext)state.AsyncState).Response, r, UTF8Encoding);
91-
}
92-
}
93-
94-
#endregion
95-
96-
#region Utility methods
97-
98-
/// <summary>
99-
/// Transfer the result data compressed when the client accepts gzip.
100-
/// </summary>
101-
/// <param name="request">A HttpRequest object that represents the HTTP request.</param>
102-
/// <param name="response">A HttpResponse object that represents the HTTP response to be sent to the client.</param>
103-
/// <param name="result">The string data to be sent to the client.</param>
104-
/// <param name="encoding">The Encoding to be used to encode as the result.</param>
105-
static void CompressResponseIfPossible(HttpRequest request, HttpResponse response, String result, Encoding encoding)
106-
{
107-
string AcceptEncoding = request.Headers["Accept-Encoding"];
108-
if (AcceptEncoding != null && AcceptEncoding.Contains("gzip"))
109-
{
110-
//response.Headers.Remove("Content-Encoding");
111-
response.AddHeader("Content-Encoding", "gzip");
112-
113-
using (var gstream = new GZipStream(response.OutputStream, CompressionMode.Compress))
114-
using (var writer = new StreamWriter(gstream, encoding))
115-
{
116-
writer.Write(result);
117-
writer.Flush();
118-
}
119-
}
120-
else
121-
{
122-
using (StreamWriter writer = new StreamWriter(response.OutputStream, encoding))
123-
{
124-
writer.Write(result);
125-
writer.Flush();
126-
}
127-
}
128-
129-
response.End();
130-
}
131-
132-
133-
#endregion
13415
}
135-
}
16+
}
Collapse file
+137Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System;
2+
using System.IO;
3+
using System.IO.Compression;
4+
using System.Text;
5+
using System.Web;
6+
7+
namespace AustinHarris.JsonRpc.AspNet
8+
{
9+
public abstract class JsonRpcHandlerBase : IHttpAsyncHandler
10+
{
11+
#region Fields
12+
/// <summary>
13+
/// UTF8 Encoding without BOM.
14+
/// </summary>
15+
private static readonly Encoding Utf8Encoding = new UTF8Encoding(false);
16+
17+
protected abstract string GetSessionId();
18+
#endregion
19+
20+
#region IHttpHandler Members
21+
22+
public bool IsReusable
23+
{
24+
get { return true; }
25+
}
26+
27+
public void ProcessRequest(HttpContext context)
28+
{
29+
// not used
30+
}
31+
32+
#endregion
33+
34+
#region IHttpAsyncHandler Members
35+
36+
/// <summary>
37+
/// Initiates an asynchronous call to the HTTP handler.
38+
/// </summary>
39+
/// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
40+
/// <param name="cb">The <see cref="T:System.AsyncCallback"/> to call when the asynchronous method call is complete. If <paramref name="cb"/> is null, the delegate is not called.</param>
41+
/// <param name="extraData">Any extra data needed to process the request.</param>
42+
/// <returns>
43+
/// An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.
44+
/// </returns>
45+
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
46+
{
47+
var async = new JsonRpcStateAsync(cb, context);
48+
async.JsonRpc = GetJsonRpcString(context.Request);
49+
JsonRpcProcessor.Process(GetSessionId(),async, context.Request);
50+
return async;
51+
}
52+
53+
private static string GetJsonRpcString(System.Web.HttpRequest request)
54+
{
55+
string json = string.Empty;
56+
if (request.RequestType == "GET")
57+
{
58+
json = request.Params["jsonrpc"] ?? string.Empty;
59+
}
60+
else if (request.RequestType == "POST")
61+
{
62+
if (request.ContentType == "application/x-www-form-urlencoded")
63+
{
64+
json = request.Params["jsonrpc"] ?? string.Empty;
65+
}
66+
else
67+
{
68+
json = new StreamReader(request.InputStream).ReadToEnd();
69+
}
70+
}
71+
return json;
72+
}
73+
74+
/// <summary>
75+
/// Provides an asynchronous process End method when the process ends.
76+
/// </summary>
77+
/// <param name="result">An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.</param>
78+
public void EndProcessRequest(IAsyncResult result)
79+
{
80+
var state = result as JsonRpcStateAsync;
81+
if (state == null) return;
82+
83+
var stateResult = state.Result;
84+
var callback = ((HttpContext)state.AsyncState).Request.Params["callback"];
85+
if (!string.IsNullOrWhiteSpace(callback))
86+
{
87+
stateResult = string.Format("{0}({1})", callback, stateResult);
88+
}
89+
90+
// try to compress the response data.
91+
// fix me: compression filters in IHttpModule always failed for IHttpAsyncHandler
92+
CompressResponseIfPossible(((HttpContext)state.AsyncState).Request, ((HttpContext)state.AsyncState).Response, stateResult, Utf8Encoding);
93+
}
94+
95+
#endregion
96+
97+
#region Utility methods
98+
99+
/// <summary>
100+
/// Transfer the result data compressed when the client accepts gzip.
101+
/// </summary>
102+
/// <param name="request">A HttpRequest object that represents the HTTP request.</param>
103+
/// <param name="response">A HttpResponse object that represents the HTTP response to be sent to the client.</param>
104+
/// <param name="result">The string data to be sent to the client.</param>
105+
/// <param name="encoding">The Encoding to be used to encode as the result.</param>
106+
static void CompressResponseIfPossible(HttpRequest request, HttpResponse response, String result, Encoding encoding)
107+
{
108+
string acceptEncoding = request.Headers["Accept-Encoding"];
109+
if (acceptEncoding != null && acceptEncoding.Contains("gzip"))
110+
{
111+
//response.Headers.Remove("Content-Encoding");
112+
response.AddHeader("Content-Encoding", "gzip");
113+
114+
using (var gstream = new GZipStream(response.OutputStream, CompressionMode.Compress))
115+
using (var writer = new StreamWriter(gstream, encoding))
116+
{
117+
writer.Write(result);
118+
writer.Flush();
119+
}
120+
}
121+
else
122+
{
123+
using (StreamWriter writer = new StreamWriter(response.OutputStream, encoding))
124+
{
125+
writer.Write(result);
126+
writer.Flush();
127+
}
128+
}
129+
130+
response.End();
131+
}
132+
133+
134+
#endregion
135+
136+
}
137+
}
Collapse file

‎AustinHarris.JsonRpcTestN/Test.cs‎

Copy file name to clipboardExpand all lines: AustinHarris.JsonRpcTestN/Test.cs
+7-30Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,17 @@ public void NullableDateTimeToNullableDateTime()
113113
Assert.AreEqual(expectedDate, acutalDate);
114114
}
115115

116-
[Test()]
117-
public void NullableFloatToNullableFloat()
116+
[TestCase(@"{method:'NullableFloatToNullableFloat',params:[1.2345],id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":1.2345,\"id\":1}")]
117+
[TestCase(@"{method:'NullableFloatToNullableFloat',params:[3.14159],id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":3.14159,\"id\":1}")]
118+
[TestCase(@"{method:'NullableFloatToNullableFloat',params:[null],id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":1}")]
119+
public string NullableFloatToNullableFloat(string request)
118120
{
119-
string request = @"{method:'NullableFloatToNullableFloat',params:[1.2345],id:1}";
120-
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":1.2345,\"id\":1}";
121121
var result = JsonRpcProcessor.Process(request);
122122
result.Wait();
123-
Assert.AreEqual(result.Result, expectedResult);
124-
Assert.AreEqual(expectedResult, result.Result);
125-
}
126-
127-
[Test()]
128-
public void NullableFloatToNullableFloat3()
129-
{
130-
string request = @"{method:'NullableFloatToNullableFloat',params:[3.14159],id:1}";
131-
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":3.14159,\"id\":1}";
132-
var result = JsonRpcProcessor.Process(request);
133-
result.Wait();
134-
Assert.AreEqual(result.Result, expectedResult);
135-
Assert.AreEqual(expectedResult, result.Result);
123+
return result.Result;
136124
}
137-
138-
139-
[Test()]
140-
public void NullableFloatToNullableFloat2()
141-
{
142-
string request = @"{method:'NullableFloatToNullableFloat',params:[null],id:1}";
143-
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":1}";
144-
var result = JsonRpcProcessor.Process(request);
145-
result.Wait();
146-
Assert.AreEqual(result.Result, expectedResult);
147-
Assert.AreEqual(expectedResult, result.Result);
148-
}
149-
125+
126+
150127
[Test()]
151128
public void DecimalToNullableDecimal()
152129
{
Collapse file

‎Json-Rpc/JsonRpcService.cs‎

Copy file name to clipboard
+9-8Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
namespace AustinHarris.JsonRpc
22
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Reflection;
7-
using AustinHarris.JsonRpc;
8-
3+
/// <summary>
4+
/// For routing use SessionId
5+
/// </summary>
96
public abstract class JsonRpcService
107
{
118
protected JsonRpcService()
129
{
13-
ServiceBinder.BindService(Handler.DefaultSessionId(), this);
10+
ServiceBinder.BindService(Handler.DefaultSessionId(), this);
1411
}
1512

13+
/// <summary>
14+
/// Routing by SessionId
15+
/// </summary>
16+
/// <param name="sessionID"></param>
1617
protected JsonRpcService(string sessionID)
1718
{
1819
ServiceBinder.BindService(sessionID, this);
1920
}
2021
}
21-
}
22+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.