Skip to content

Commit 5d8bb9a

Browse files
authored
Improving end to end test example with PUT, POST and http request collection (#325)
1 parent 35c1eef commit 5d8bb9a

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Net;
6+
using nanoFramework.WebServer;
7+
8+
namespace WebServerE2ETests
9+
{
10+
class PostPutController
11+
{
12+
[Method("POST")]
13+
[Route("post")]
14+
public void Post(WebServerEventArgs e)
15+
{
16+
byte[] buff = new byte[e.Context.Request.InputStream.Length];
17+
e.Context.Request.InputStream.Read(buff, 0, buff.Length);
18+
var txt = e.Context.Request.ContentType.Contains("text") ? System.Text.Encoding.UTF8.GetString(buff, 0, buff.Length) : BitConverter.ToString(buff);
19+
WebServer.OutPutStream(e.Context.Response, $"POST: {txt}");
20+
}
21+
22+
[Method("PUT")]
23+
[Route("put")]
24+
public void Put(WebServerEventArgs e)
25+
{
26+
byte[] buff = new byte[e.Context.Request.InputStream.Length];
27+
e.Context.Request.InputStream.Read(buff, 0, buff.Length);
28+
var txt = e.Context.Request.ContentType.Contains("text") ? System.Text.Encoding.UTF8.GetString(buff, 0, buff.Length) : BitConverter.ToString(buff);
29+
WebServer.OutPutStream(e.Context.Response, $"PUT: {txt}");
30+
}
31+
32+
}
33+
}

tests/WebServerE2ETests/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void Main()
2929
}
3030

3131
Debug.WriteLine($"Connected with wifi credentials. IP Address: {GetCurrentIPAddress()}");
32-
_server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(SimpleRouteController), typeof(AuthController), typeof(MixedController) });
32+
_server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(SimpleRouteController), typeof(AuthController), typeof(MixedController), typeof(PostPutController) });
3333
// To test authentication with various scenarios
3434
_server.ApiKey = "ATopSecretAPIKey1234";
3535
_server.Credential = new NetworkCredential("topuser", "topPassword");

tests/WebServerE2ETests/WebServerE2ETests.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<ItemGroup>
2121
<Compile Include="MixedController.cs" />
2222
<Compile Include="AuthController.cs" />
23+
<Compile Include="PostPutController.cs" />
2324
<Compile Include="Program.cs" />
2425
<Compile Include="Properties\AssemblyInfo.cs" />
2526
<Compile Include="SimpleRouteController.cs" />

tests/WebServerE2ETests/requests.http

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This file is a collection of requests that can be executed with the REST Client extension for Visual Studio Code
2+
# https://marketplace.visualstudio.com/items?itemName=humao.rest-client
3+
# adjust your host here
4+
@host=192.168.1.86:80
5+
6+
###
7+
8+
POST http://{{host}}/post?someparams=1&others=2 HTTP/1.1
9+
Content-Type: text/plain
10+
11+
This is a test with post
12+
13+
###
14+
15+
PUT http://{{host}}/put HTTP/1.1
16+
Content-Type: text/plain
17+
18+
This is another test with put
19+
20+
###
21+
22+
GET http://{{host}}/get?someparams=1&others=2 HTTP/1.1
23+
24+
###
25+
26+
# This request will fail with 401 Unauthorized
27+
GET http://{{host}}/authbasic HTTP/1.1
28+
29+
###
30+
31+
# this one will succeed
32+
GET http://topuser:topPassword@{{host}}/authbasic HTTP/1.1
33+
34+
###
35+
36+
# This request will fail with 401 Unauthorized
37+
GET http://{{host}}/authapi HTTP/1.1
38+
39+
###
40+
41+
# this one will succeed
42+
GET http://{{host}}/authapi HTTP/1.1
43+
ApiKey: superKey1234
44+
45+
###
46+
47+
# This request will fail with 401 Unauthorized
48+
GET http://{{host}}/authdefaultapi HTTP/1.1
49+
50+
###
51+
52+
# this one will succeed
53+
GET http://{{host}}/authdefaultapi HTTP/1.1
54+
ApiKey: ATopSecretAPIKey1234
55+
56+
57+
###
58+
59+
# this one will succeed with the public route
60+
GET http://{{host}}/authapikeybasicandpublic HTTP/1.1
61+
62+
###
63+
64+
# this one will succeed with user 3
65+
GET http://user3:password@{{host}}/authapikeybasicandpublic HTTP/1.1

0 commit comments

Comments
 (0)