GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Replace HttpListener Response Tests with WebListener (#5540) · PowerShell/PowerShell@95a8a64

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • test
    • powershell/Modules/Microsoft.PowerShell.Utility
      • WebCmdlets.Tests.ps1
    • tools
      • Modules/WebListener
        • WebListener.psm1
      • WebListener
        • Constants.cs
        • Controllers
          • ResponseController.cs
        • README.md
        • Views/Home
          • Index.cshtml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1‎

Copy file name to clipboardExpand all lines: test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
+182-346Lines changed: 182 additions & 346 deletions
Large diffs are not rendered by default.
Collapse file

‎test/tools/Modules/WebListener/WebListener.psm1‎

Copy file name to clipboardExpand all lines: test/tools/Modules/WebListener/WebListener.psm1
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ function Get-WebListenerUrl {
136136
'Home',
137137
'Multipart',
138138
'Redirect',
139+
'Response',
139140
'ResponseHeaders',
140141
'/'
141142
)]
Collapse file

‎test/tools/WebListener/Constants.cs‎

Copy file name to clipboardExpand all lines: test/tools/WebListener/Constants.cs
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ namespace mvc.Controllers
55
internal static class Constants
66
{
77
public const string HeaderSeparator = ", ";
8+
public const string ApplicationJson = "application/json";
9+
}
10+
11+
internal static class StatusCodes
12+
{
13+
public const Int32 ApplicationError = 500;
814
}
915
}
Collapse file

‎test/tools/WebListener/Controllers/ResponseController.cs‎

Copy file name to clipboard
+103Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Http.Extensions;
11+
using Microsoft.Extensions.Primitives;
12+
using mvc.Models;
13+
using Newtonsoft.Json;
14+
using Newtonsoft.Json.Linq;
15+
16+
namespace mvc.Controllers
17+
{
18+
public class ResponseController : Controller
19+
{
20+
public String Index()
21+
{
22+
string output = String.Empty;
23+
string contentType = Constants.ApplicationJson;
24+
25+
StringValues contentTypes;
26+
if (Request.Query.TryGetValue("contenttype", out contentTypes))
27+
{
28+
contentType = contentTypes.FirstOrDefault();
29+
}
30+
31+
StringValues statusCodes;
32+
Int32 statusCode;
33+
if (Request.Query.TryGetValue("statuscode", out statusCodes) &&
34+
Int32.TryParse(statusCodes.FirstOrDefault(), out statusCode))
35+
{
36+
Response.StatusCode = statusCode;
37+
}
38+
39+
StringValues body;
40+
if (Request.Query.TryGetValue("body", out body))
41+
{
42+
output = body.FirstOrDefault();
43+
}
44+
45+
StringValues headers;
46+
if (Request.Query.TryGetValue("headers", out headers))
47+
{
48+
try
49+
{
50+
Response.Headers.Clear();
51+
JObject jobject = JObject.Parse(headers.FirstOrDefault());
52+
foreach (JProperty property in (JToken)jobject)
53+
{
54+
// Only set Content-Type through contenttype field.
55+
if (String.Equals(property.Name, "Content-Type", StringComparison.InvariantCultureIgnoreCase))
56+
{
57+
continue;
58+
}
59+
foreach (string entry in GetSingleOrArray<String>(property.Value))
60+
{
61+
Response.Headers.Append(property.Name,entry);
62+
}
63+
}
64+
}
65+
catch (Exception ex)
66+
{
67+
output = JsonConvert.SerializeObject(ex);
68+
Response.StatusCode = StatusCodes.ApplicationError;
69+
contentType = Constants.ApplicationJson;
70+
}
71+
}
72+
73+
// Content-Type must be applied right before it is sent to the client or MVC will overwrite.
74+
Response.OnStarting(state =>
75+
{
76+
var httpContext = (HttpContext) state;
77+
httpContext.Response.ContentType = contentType;
78+
return Task.FromResult(0);
79+
}, HttpContext);
80+
81+
Response.ContentLength = Encoding.UTF8.GetBytes(output).Length;
82+
83+
return output;
84+
}
85+
86+
public IActionResult Error()
87+
{
88+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
89+
}
90+
91+
private List<T> GetSingleOrArray<T>(JToken token)
92+
{
93+
if (token.HasValues)
94+
{
95+
return token.ToObject<List<T>>();
96+
}
97+
else
98+
{
99+
return new List<T> { token.ToObject<T>() };
100+
}
101+
}
102+
}
103+
}
Collapse file

‎test/tools/WebListener/README.md‎

Copy file name to clipboardExpand all lines: test/tools/WebListener/README.md
+35Lines changed: 35 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,41 @@ Location: /Get/
342342
<p>You should be redirected automatically to target URL: <a href="/Get/">/Get/</a>. If not click the link.
343343
```
344344

345+
## /Response/
346+
347+
Will return a response crafted from the query string. The following four fields are supported:
348+
349+
* `body` - a string containing the response body
350+
* `statuscode` - the HTTP Status Code to return
351+
* `contenttype` - The `Content-Type` response header
352+
* `headers` - a JSON string containing response headers. `Content-Type` will be ignored in `headers`. Use `contenttype` instead.
353+
354+
```powershell
355+
$Query = @{
356+
statsucode = 200
357+
contenttype = 'application/json'
358+
body = '{"key1": "value1"}'
359+
headers = @{
360+
"X-Header" = "Response header value"
361+
} | ConvertTo-Json
362+
}
363+
$Uri = Get-WebListenerUrl -Test 'Response' -Query $Query
364+
Invoke-RestMethod -Uri $uri
365+
```
366+
367+
Response headers:
368+
369+
```none
370+
Content-Type: application/json
371+
X-Header: Response header value
372+
```
373+
374+
Response Body:
375+
376+
```json
377+
{"key1": "value1"}
378+
```
379+
345380
## /ResponseHeaders/
346381

347382
Will return the response headers passed in query string. The response body will be the supplied headers as a JSON object.
Collapse file

‎test/tools/WebListener/Views/Home/Index.cshtml‎

Copy file name to clipboardExpand all lines: test/tools/WebListener/Views/Home/Index.cshtml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
<li><a href="/Get/">/Get/</a> - Emulates functionality of https://httpbin.org/get by returning GET headers, Arguments, and Request URL</li>
1313
<li><a href="/Multipart/">/Multipart/</a> - Multipart/form-data submission testing</li>
1414
<li><a href="/Redirect/">/Redirect/{count}</a> - 302 redirect <i>count</i> times.</li>
15+
<li><a href="/Response/?statuscode=200&contenttype=application%2Fjson&body=%22Body%20text%22&headers=%7B%22x-header%22%3A%20%22Response%20Header%20Value%22%7D">/Response/?statuscode=&lt;StatusCode&gt;&amp;body=&lt;ResponseBody&gt;&amp;contenttype=&lt;ResponseContentType&gt;&amp;headers=&lt;JsonHeadersObject&gt;</a> - Returns the given response.</li>
1516
<li><a href="/ResponseHeaders/?key=val">/ResponseHeaders/?key=val</a> - Returns given response headers.</li>
1617
</ul>

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.