Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .spelling
Original file line number Diff line number Diff line change
Expand Up @@ -1190,3 +1190,8 @@ tests.zip
v5.0
v6.0.
#endregion

#region test/tools/WebListener/README.md Overrides
- test/tools/WebListener/README.md
ResponseHeaders
#endregion
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they really all used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you are asking. But, I added a file level override because I had been putting all of the overrides for this in the Global dictionary. That was probably a poor decision, so I'm adding the new section and will do a separate PR after this is merged to move the ones from my previous PRs from global to the file override.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Clear.

Closed.

Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
It "Validate Invoke-WebRequest handles missing Content-Type in response header" {

#Validate that exception is not thrown when response headers are missing Content-Type.
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/response-headers?Content-Type='"
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @{'Content-Type' = ''}
$command = "Invoke-WebRequest -Uri '$uri'"
$result = ExecuteWebCommand -command $command
$result.Error | Should BeNullOrEmpty
}
Expand Down Expand Up @@ -1588,7 +1589,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
It "Validate Invoke-RestMethod handles missing Content-Type in response header" {

#Validate that exception is not thrown when response headers are missing Content-Type.
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/response-headers?Content-Type='"
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @{'Content-Type' = ''}
$command = "Invoke-RestMethod -Uri '$uri'"
$result = ExecuteWebCommand -command $command
$result.Error | Should BeNullOrEmpty
}
Expand Down
14 changes: 13 additions & 1 deletion test/tools/Modules/WebListener/WebListener.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ function Get-WebListenerUrl {
'Home',
'Multipart',
'Redirect',
'ResponseHeaders',
'/'
)]
[String]$Test,

[String]$TestValue
[String]$TestValue,

[System.Collections.IDictionary]$Query
)
process {
$runningListener = Get-WebListener
Expand All @@ -151,6 +154,15 @@ function Get-WebListenerUrl {
{
$Uri.Path = $Test
}
$StringBuilder = [System.Text.StringBuilder]::new()
foreach ($key in $Query.Keys)
{
$null = $StringBuilder.Append([System.Net.WebUtility]::UrlEncode($key))
$null = $StringBuilder.Append('=')
$null = $StringBuilder.Append([System.Net.WebUtility]::UrlEncode($Query[$key].ToString()))
$null = $StringBuilder.Append('&')
}
$Uri.Query = $StringBuilder.ToString()

return [Uri]$Uri.ToString()
}
Expand Down
49 changes: 49 additions & 0 deletions test/tools/WebListener/Controllers/ResponseHeadersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using mvc.Models;

namespace mvc.Controllers
{
public class ResponseHeadersController : Controller
{
public string Index()
{
Hashtable headers = new Hashtable();
foreach (var key in Request.Query.Keys)
{
headers.Add(key, String.Join(Constants.HeaderSeparator, Request.Query[key]));

if (String.Equals("Content-Type", key, StringComparison.InvariantCultureIgnoreCase))
{
// Content-Type must be applied right before it is sent to the client or MVC will overwrite.
string contentType = Request.Query[key];
Response.OnStarting(state =>
{
var httpContext = (HttpContext) state;
httpContext.Response.ContentType = contentType;
return Task.FromResult(0);
}, HttpContext);
}
else
{
Response.Headers.TryAdd(key, Request.Query[key]);
}
}
return JsonConvert.SerializeObject(headers);
}

public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
31 changes: 31 additions & 0 deletions test/tools/WebListener/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,34 @@ Location: /Get/
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/Get/">/Get/</a>. If not click the link.
```

## /ResponseHeaders/

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

```powershell
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @{'Content-Type' = 'custom'; 'x-header-01' = 'value01'; 'x-header-02' = 'value02'}
Invoke-RestMethod -Uri $uri
```

Response Headers:

```none
HTTP/1.1 200 OK
Date: Sun, 08 Oct 2017 18:20:38 GMT
Transfer-Encoding: chunked
Server: Kestrel
x-header-02: value02
x-header-01: value01
Content-Type: custom
```

Body:

```json
{
"Content-Type": "custom",
"x-header-02": "value02",
"x-header-01": "value01"
}
```
1 change: 1 addition & 0 deletions test/tools/WebListener/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<li><a href="/Get/">/Get/</a> - Emulates functionality of https://httpbin.org/get by returning GET headers, Arguments, and Request URL</li>
<li><a href="/Multipart/">/Multipart/</a> - Multipart/form-data submission testing</li>
<li><a href="/Redirect/">/Redirect/{count}</a> - 302 redirect <i>count</i> times.</li>
<li><a href="/ResponseHeaders/?key=val">/ResponseHeaders/?key=val</a> - Returns given response headers.</li>
</ul>