diff --git a/tests/WebServerE2ETests/PostPutController.cs b/tests/WebServerE2ETests/PostPutController.cs new file mode 100644 index 0000000..f404f8e --- /dev/null +++ b/tests/WebServerE2ETests/PostPutController.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Net; +using nanoFramework.WebServer; + +namespace WebServerE2ETests +{ + class PostPutController + { + [Method("POST")] + [Route("post")] + public void Post(WebServerEventArgs e) + { + byte[] buff = new byte[e.Context.Request.InputStream.Length]; + e.Context.Request.InputStream.Read(buff, 0, buff.Length); + var txt = e.Context.Request.ContentType.Contains("text") ? System.Text.Encoding.UTF8.GetString(buff, 0, buff.Length) : BitConverter.ToString(buff); + WebServer.OutPutStream(e.Context.Response, $"POST: {txt}"); + } + + [Method("PUT")] + [Route("put")] + public void Put(WebServerEventArgs e) + { + byte[] buff = new byte[e.Context.Request.InputStream.Length]; + e.Context.Request.InputStream.Read(buff, 0, buff.Length); + var txt = e.Context.Request.ContentType.Contains("text") ? System.Text.Encoding.UTF8.GetString(buff, 0, buff.Length) : BitConverter.ToString(buff); + WebServer.OutPutStream(e.Context.Response, $"PUT: {txt}"); + } + + } +} diff --git a/tests/WebServerE2ETests/Program.cs b/tests/WebServerE2ETests/Program.cs index 23dc8d7..ef38cb6 100644 --- a/tests/WebServerE2ETests/Program.cs +++ b/tests/WebServerE2ETests/Program.cs @@ -29,7 +29,7 @@ public static void Main() } Debug.WriteLine($"Connected with wifi credentials. IP Address: {GetCurrentIPAddress()}"); - _server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(SimpleRouteController), typeof(AuthController), typeof(MixedController) }); + _server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(SimpleRouteController), typeof(AuthController), typeof(MixedController), typeof(PostPutController) }); // To test authentication with various scenarios _server.ApiKey = "ATopSecretAPIKey1234"; _server.Credential = new NetworkCredential("topuser", "topPassword"); diff --git a/tests/WebServerE2ETests/WebServerE2ETests.nfproj b/tests/WebServerE2ETests/WebServerE2ETests.nfproj index bb38224..096435a 100644 --- a/tests/WebServerE2ETests/WebServerE2ETests.nfproj +++ b/tests/WebServerE2ETests/WebServerE2ETests.nfproj @@ -20,6 +20,7 @@ + diff --git a/tests/WebServerE2ETests/requests.http b/tests/WebServerE2ETests/requests.http new file mode 100644 index 0000000..b950c9f --- /dev/null +++ b/tests/WebServerE2ETests/requests.http @@ -0,0 +1,65 @@ +# This file is a collection of requests that can be executed with the REST Client extension for Visual Studio Code +# https://marketplace.visualstudio.com/items?itemName=humao.rest-client +# adjust your host here +@host=192.168.1.86:80 + +### + +POST http://{{host}}/post?someparams=1&others=2 HTTP/1.1 +Content-Type: text/plain + +This is a test with post + +### + +PUT http://{{host}}/put HTTP/1.1 +Content-Type: text/plain + +This is another test with put + +### + +GET http://{{host}}/get?someparams=1&others=2 HTTP/1.1 + +### + +# This request will fail with 401 Unauthorized +GET http://{{host}}/authbasic HTTP/1.1 + +### + +# this one will succeed +GET http://topuser:topPassword@{{host}}/authbasic HTTP/1.1 + +### + +# This request will fail with 401 Unauthorized +GET http://{{host}}/authapi HTTP/1.1 + +### + +# this one will succeed +GET http://{{host}}/authapi HTTP/1.1 +ApiKey: superKey1234 + +### + +# This request will fail with 401 Unauthorized +GET http://{{host}}/authdefaultapi HTTP/1.1 + +### + +# this one will succeed +GET http://{{host}}/authdefaultapi HTTP/1.1 +ApiKey: ATopSecretAPIKey1234 + + +### + +# this one will succeed with the public route +GET http://{{host}}/authapikeybasicandpublic HTTP/1.1 + +### + +# this one will succeed with user 3 +GET http://user3:password@{{host}}/authapikeybasicandpublic HTTP/1.1