Fix JSONObject parsing and assignment from request content
parent
fda7d695d1
commit
458954f7fb
|
@ -11,7 +11,7 @@ namespace ln.http.service
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
StaticRouter staticRouter = new StaticRouter(".");
|
StaticRouter staticRouter = new StaticRouter(".");
|
||||||
HTTPServer httpServer = new HTTPServer(new HttpListener(8888), new LoggingRouter(staticRouter));
|
HTTPServer httpServer = new HTTPServer(new HttpListener(8888), new LoggingRouter(staticRouter.Route).Route);
|
||||||
|
|
||||||
httpServer.Start();
|
httpServer.Start();
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,9 @@ using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using ln.http.router;
|
using ln.http.router;
|
||||||
|
using ln.json;
|
||||||
using ln.type;
|
using ln.type;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
@ -20,11 +22,12 @@ namespace ln.http.tests
|
||||||
return;
|
return;
|
||||||
|
|
||||||
HttpRouter testRouter = new HttpRouter();
|
HttpRouter testRouter = new HttpRouter();
|
||||||
|
testRouter.Map(HttpMethod.ANY, "/controller/*", HttpRoutePriority.NORMAL, new TestApiController().Route);
|
||||||
|
|
||||||
StaticRouter staticRouter = new StaticRouter(AppContext.BaseDirectory);
|
StaticRouter staticRouter = new StaticRouter(AppContext.BaseDirectory);
|
||||||
testRouter.AddSimpleRoute("/static/*", staticRouter);
|
testRouter.Map(HttpMethod.ANY, "/static/*", staticRouter.Route);
|
||||||
|
|
||||||
server = new HTTPServer(testRouter);
|
server = new HTTPServer(testRouter.Route);
|
||||||
server.AddEndpoint(new Endpoint(IPv6.ANY,0));
|
server.AddEndpoint(new Endpoint(IPv6.ANY,0));
|
||||||
|
|
||||||
server.Start();
|
server.Start();
|
||||||
|
@ -50,5 +53,34 @@ namespace ln.http.tests
|
||||||
|
|
||||||
Assert.Pass();
|
Assert.Pass();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestPutJson()
|
||||||
|
{
|
||||||
|
JSONObject jsonPutObject = new JSONObject();
|
||||||
|
jsonPutObject["PutTest"] = JSONTrue.Instance;
|
||||||
|
StringContent jsonStringContent = new StringContent(jsonPutObject.ToString());
|
||||||
|
jsonStringContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
||||||
|
|
||||||
|
HttpClient client = new HttpClient();
|
||||||
|
HttpResponseMessage response = client.PutAsync(String.Format("http://localhost:{0}/controller/put", testPort), jsonStringContent).Result;
|
||||||
|
|
||||||
|
Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode);
|
||||||
|
Assert.Pass();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestApiController : HttpEndpointController
|
||||||
|
{
|
||||||
|
[Map(HttpMethod.PUT, "/put")]
|
||||||
|
public HttpResponse PutTest(
|
||||||
|
[HttpArgumentSource(HttpArgumentSource.CONTENT)]
|
||||||
|
JSONObject jObject
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (jObject.ContainsKey("PutTest") && jObject["PutTest"] is JSONTrue jsonTrue && jsonTrue == JSONTrue.Instance)
|
||||||
|
return HttpResponse.OK();
|
||||||
|
return HttpResponse.BadRequest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -96,7 +96,15 @@ namespace ln.http
|
||||||
contentType.Equals("application/json"))
|
contentType.Equals("application/json"))
|
||||||
{
|
{
|
||||||
using (TextReader reader = httpContext.Request.ContentStream.TextReader())
|
using (TextReader reader = httpContext.Request.ContentStream.TextReader())
|
||||||
parameters[n] = JSONMapper.DefaultMapper.FromJson(JSONParser.Parse(reader), _parameterInfos[n].ParameterType);
|
{
|
||||||
|
JSONValue jsonValue = JSONParser.Parse(reader);
|
||||||
|
|
||||||
|
if (typeof(JSONValue).IsAssignableFrom(_parameterInfos[n].ParameterType))
|
||||||
|
parameters[n] = jsonValue;
|
||||||
|
else
|
||||||
|
parameters[n] = JSONMapper.DefaultMapper.FromJson(jsonValue,
|
||||||
|
_parameterInfos[n].ParameterType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (_parameterInfos[n].HasDefaultValue)
|
else if (_parameterInfos[n].HasDefaultValue)
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
<Copyright>(c) 2020 Harald Wolff-Thobaben</Copyright>
|
<Copyright>(c) 2020 Harald Wolff-Thobaben</Copyright>
|
||||||
<PackageTags>http server</PackageTags>
|
<PackageTags>http server</PackageTags>
|
||||||
<LangVersion>9</LangVersion>
|
<LangVersion>9</LangVersion>
|
||||||
<PackageVersion>0.5.0</PackageVersion>
|
<PackageVersion>0.5.1</PackageVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
<PackageReference Include="ln.json" Version="1.0.8-ci" />
|
<PackageReference Include="ln.json" Version="1.0.8-ci" />
|
||||||
<PackageReference Include="ln.logging" Version="1.0" />
|
<PackageReference Include="ln.logging" Version="1.0" />
|
||||||
<PackageReference Include="ln.threading" Version="0.2.1" />
|
<PackageReference Include="ln.threading" Version="0.2.1" />
|
||||||
<PackageReference Include="ln.type" Version="0.1-*" />
|
<PackageReference Include="ln.type" Version="0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
Loading…
Reference in New Issue