|
5 | 5 | using RT.Json; |
6 | 6 | using RT.Util.ExtensionMethods; |
7 | 7 |
|
8 | | -namespace RT.Servers.Tests |
| 8 | +namespace RT.Servers.Tests; |
| 9 | + |
| 10 | +[TestClass] |
| 11 | +public sealed class AjaxHandlerTests |
9 | 12 | { |
10 | | - [TestClass] |
11 | | - public sealed class AjaxHandlerTests |
12 | | - { |
13 | | - sealed class TempObject { public int Value; } |
| 13 | + sealed class TempObject { public int Value; } |
14 | 14 |
|
15 | | - sealed class Ajax |
16 | | - { |
17 | | - [AjaxMethod] |
18 | | - public string HelloWorld() => "Hello, world!"; |
| 15 | + sealed class Ajax |
| 16 | + { |
| 17 | + [AjaxMethod] |
| 18 | + public string HelloWorld() => "Hello, world!"; |
19 | 19 |
|
20 | | - [AjaxMethod] |
21 | | - public string AddOne(int i) => $"{i + 1}"; |
| 20 | + [AjaxMethod] |
| 21 | + public string AddOne(int i) => $"{i + 1}"; |
22 | 22 |
|
23 | | - [AjaxMethod] |
24 | | - public string ReturnUrl([AjaxRequest] HttpRequest req) => req.Url.ToFull(); |
| 23 | + [AjaxMethod] |
| 24 | + public string ReturnUrl([AjaxRequest] HttpRequest req) => req.Url.ToFull(); |
25 | 25 |
|
26 | | - [AjaxMethod] |
27 | | - public int AddOneToTempObject(TempObject tempObj) => tempObj.Value + 1; |
| 26 | + [AjaxMethod] |
| 27 | + public int AddOneToTempObject(TempObject tempObj) => tempObj.Value + 1; |
28 | 28 |
|
29 | | - [AjaxConverter] |
30 | | - public TempObject ConvertTempObject(int tempObj) => new TempObject { Value = tempObj }; |
31 | | - } |
| 29 | + [AjaxConverter] |
| 30 | + public TempObject ConvertTempObject(int tempObj) => new() { Value = tempObj }; |
| 31 | + } |
32 | 32 |
|
33 | | - [TestMethod, Timeout(60 * 1000, CooperativeCancellation = true)] |
34 | | - public void TestErrorHandlerExceptions() |
| 33 | + [TestMethod, Timeout(60 * 1000, CooperativeCancellation = true)] |
| 34 | + public void TestErrorHandlerExceptions() |
| 35 | + { |
| 36 | + var instance = new HttpServer(TestHelpers.Port + 0, new HttpServerOptions { OutputExceptionInformation = true }); |
| 37 | + try |
35 | 38 | { |
36 | | - var instance = new HttpServer(TestHelpers.Port, new HttpServerOptions { OutputExceptionInformation = true }); |
37 | | - try |
38 | | - { |
39 | | - var ajaxHandler = new AjaxHandler<Ajax>(); |
40 | | - var api = new Ajax(); |
41 | | - instance.Handler = req => ajaxHandler.Handle(req, api); |
42 | | - instance.StartListening(); |
| 39 | + var ajaxHandler = new AjaxHandler<Ajax>(); |
| 40 | + var api = new Ajax(); |
| 41 | + instance.Handler = req => ajaxHandler.Handle(req, api); |
| 42 | + instance.StartListening(); |
43 | 43 |
|
44 | | - (HttpStatusCode status, string response) getResponse(string method, string payload) |
45 | | - { |
46 | | - TcpClient cl = new TcpClient(); |
47 | | - cl.Connect("localhost", TestHelpers.Port); |
48 | | - cl.ReceiveTimeout = 1000; // 1 sec |
49 | | - var payloadFull = "data=" + payload.ToUtf8().Select(b => "%" + b.ToString("X2")).JoinString(); |
50 | | - cl.Client.Send($"POST /{method} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: {payloadFull.Utf8Length()}\r\n\r\n{payloadFull}".ToUtf8()); |
51 | | - var response = Encoding.UTF8.GetString(cl.Client.ReceiveAllBytes()); |
52 | | - cl.Close(); |
53 | | - var code = (HttpStatusCode) int.Parse(response.Substring("HTTP/1.1 ".Length, 3)); |
54 | | - var parts = response.Split("\r\n\r\n"); |
55 | | - return (code, parts[1]); |
56 | | - } |
| 44 | + static (HttpStatusCode status, string response) getResponse(string method, string payload) |
| 45 | + { |
| 46 | + TcpClient cl = new(); |
| 47 | + cl.Connect("localhost", TestHelpers.Port + 0); |
| 48 | + cl.ReceiveTimeout = 1000; // 1 sec |
| 49 | + var payloadFull = "data=" + payload.ToUtf8().Select(b => "%" + b.ToString("X2")).JoinString(); |
| 50 | + cl.Client.Send($"POST /{method} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: {payloadFull.Utf8Length()}\r\n\r\n{payloadFull}".ToUtf8()); |
| 51 | + var response = Encoding.UTF8.GetString(cl.Client.ReceiveAllBytes()); |
| 52 | + cl.Close(); |
| 53 | + var code = (HttpStatusCode) int.Parse(response.Substring("HTTP/1.1 ".Length, 3)); |
| 54 | + var parts = response.Split("\r\n\r\n"); |
| 55 | + return (code, parts[1]); |
| 56 | + } |
57 | 57 |
|
58 | | - // Test that we get a 404 response for an invalid method |
59 | | - var resp = getResponse("InvalidMethod", new JsonDict { }.ToString()); |
60 | | - Assert.AreEqual(HttpStatusCode._404_NotFound, resp.status); |
| 58 | + // Test that we get a 404 response for an invalid method |
| 59 | + var resp = getResponse("InvalidMethod", new JsonDict { }.ToString()); |
| 60 | + Assert.AreEqual(HttpStatusCode._404_NotFound, resp.status); |
61 | 61 |
|
62 | | - // Test that we get a 200 response for a valid method |
63 | | - resp = getResponse("HelloWorld", new JsonDict { }.ToString()); |
64 | | - Assert.AreEqual(HttpStatusCode._200_OK, resp.status); |
65 | | - Assert.IsTrue(JsonDict.TryParse(resp.response, out var json)); |
66 | | - Assert.AreEqual("Hello, world!", json["result"].GetString()); |
67 | | - Assert.AreEqual("ok", json["status"].GetString()); |
| 62 | + // Test that we get a 200 response for a valid method |
| 63 | + resp = getResponse("HelloWorld", new JsonDict { }.ToString()); |
| 64 | + Assert.AreEqual(HttpStatusCode._200_OK, resp.status); |
| 65 | + Assert.IsTrue(JsonDict.TryParse(resp.response, out var json)); |
| 66 | + Assert.AreEqual("Hello, world!", json["result"].GetString()); |
| 67 | + Assert.AreEqual("ok", json["status"].GetString()); |
68 | 68 |
|
69 | | - // Test that we get a 400 response for an invalid request (missing parameter) |
70 | | - resp = getResponse("AddOne", new JsonDict { }.ToString()); |
71 | | - Assert.AreEqual(HttpStatusCode._400_BadRequest, resp.status); |
72 | | - Assert.IsTrue(JsonDict.TryParse(resp.response, out json)); |
73 | | - Assert.AreEqual("error", json["status"].GetString()); |
| 69 | + // Test that we get a 400 response for an invalid request (missing parameter) |
| 70 | + resp = getResponse("AddOne", new JsonDict { }.ToString()); |
| 71 | + Assert.AreEqual(HttpStatusCode._400_BadRequest, resp.status); |
| 72 | + Assert.IsTrue(JsonDict.TryParse(resp.response, out json)); |
| 73 | + Assert.AreEqual("error", json["status"].GetString()); |
74 | 74 |
|
75 | | - // Test that we get the correct response for the AddOne method |
76 | | - resp = getResponse("AddOne", new JsonDict { ["i"] = 47 }.ToString()); |
77 | | - Assert.AreEqual(HttpStatusCode._200_OK, resp.status); |
78 | | - Assert.IsTrue(JsonDict.TryParse(resp.response, out json)); |
79 | | - Assert.AreEqual("48", json["result"].GetString()); |
80 | | - Assert.AreEqual("ok", json["status"].GetString()); |
| 75 | + // Test that we get the correct response for the AddOne method |
| 76 | + resp = getResponse("AddOne", new JsonDict { ["i"] = 47 }.ToString()); |
| 77 | + Assert.AreEqual(HttpStatusCode._200_OK, resp.status); |
| 78 | + Assert.IsTrue(JsonDict.TryParse(resp.response, out json)); |
| 79 | + Assert.AreEqual("48", json["result"].GetString()); |
| 80 | + Assert.AreEqual("ok", json["status"].GetString()); |
81 | 81 |
|
82 | | - // Test that we get the correct response for the ReturnUrl method |
83 | | - resp = getResponse("ReturnUrl", new JsonDict { }.ToString()); |
84 | | - Assert.AreEqual(HttpStatusCode._200_OK, resp.status); |
85 | | - Assert.IsTrue(JsonDict.TryParse(resp.response, out json)); |
86 | | - Assert.AreEqual("http://localhost/ReturnUrl", json["result"].GetString()); |
87 | | - Assert.AreEqual("ok", json["status"].GetString()); |
| 82 | + // Test that we get the correct response for the ReturnUrl method |
| 83 | + resp = getResponse("ReturnUrl", new JsonDict { }.ToString()); |
| 84 | + Assert.AreEqual(HttpStatusCode._200_OK, resp.status); |
| 85 | + Assert.IsTrue(JsonDict.TryParse(resp.response, out json)); |
| 86 | + Assert.AreEqual("http://localhost/ReturnUrl", json["result"].GetString()); |
| 87 | + Assert.AreEqual("ok", json["status"].GetString()); |
88 | 88 |
|
89 | | - // Test AjaxConverter |
90 | | - resp = getResponse("AddOneToTempObject", new JsonDict { ["tempObj"] = 47 }.ToString()); |
91 | | - Assert.AreEqual(HttpStatusCode._200_OK, resp.status); |
92 | | - Assert.IsTrue(JsonDict.TryParse(resp.response, out json)); |
93 | | - Assert.AreEqual(48, json["result"].GetInt()); |
94 | | - Assert.AreEqual("ok", json["status"].GetString()); |
95 | | - } |
96 | | - finally |
97 | | - { |
98 | | - instance.StopListening(brutal: true); |
99 | | - } |
| 89 | + // Test AjaxConverter |
| 90 | + resp = getResponse("AddOneToTempObject", new JsonDict { ["tempObj"] = 47 }.ToString()); |
| 91 | + Assert.AreEqual(HttpStatusCode._200_OK, resp.status); |
| 92 | + Assert.IsTrue(JsonDict.TryParse(resp.response, out json)); |
| 93 | + Assert.AreEqual(48, json["result"].GetInt()); |
| 94 | + Assert.AreEqual("ok", json["status"].GetString()); |
| 95 | + } |
| 96 | + finally |
| 97 | + { |
| 98 | + instance.StopListening(brutal: true); |
100 | 99 | } |
101 | 100 | } |
102 | 101 | } |
0 commit comments