ln.http/ln.http.tests/UnitTest1.cs

54 lines
1.4 KiB
C#

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using ln.http.router;
using ln.type;
using NUnit.Framework;
namespace ln.http.tests
{
public class Tests
{
HTTPServer server;
int testPort;
[SetUp]
public void Setup()
{
if (server != null)
return;
SimpleRouter testRouter = new SimpleRouter();
StaticRouter staticRouter = new StaticRouter(AppContext.BaseDirectory);
testRouter.AddSimpleRoute("/static/*", staticRouter);
server = new HTTPServer(testRouter);
server.AddEndpoint(new Endpoint(IPv6.ANY,0));
server.Start();
testPort = server.Listeners[0].LocalEndpoint.Port;
TestContext.Error.WriteLine("Using Port {0}", testPort);
}
[Test]
public void Test1()
{
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync(String.Format("http://localhost:{0}/static/test.txt", testPort)).Result;
Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode);
byte[] contentBytes = response.Content.ReadAsByteArrayAsync().Result;
byte[] fileBytes = File.ReadAllBytes("test.txt");
CollectionAssert.AreEqual(fileBytes, contentBytes);
//server.Stop();
Assert.Pass();
}
}
}