-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebHeaderCollection.IsRestricted (#148)
- Loading branch information
Showing
8 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.IO; | ||
using System.Reflection; | ||
using log4net; | ||
using log4net.Config; | ||
using log4net.Repository; | ||
using Newtonsoft.Json; | ||
using WireMock.Logging; | ||
using WireMock.RequestBuilders; | ||
using WireMock.ResponseBuilders; | ||
using WireMock.Server; | ||
using WireMock.Settings; | ||
|
||
namespace WireMock.Net.Console.NETCoreApp | ||
{ | ||
static class Program | ||
{ | ||
private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); | ||
private static readonly ILog Log = LogManager.GetLogger(typeof(Program)); | ||
|
||
static void Main(params string[] args) | ||
{ | ||
XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config")); | ||
|
||
string url = "http://localhost:9999/"; | ||
|
||
var server = FluentMockServer.Start(new FluentMockServerSettings | ||
{ | ||
Urls = new[] { url }, | ||
StartAdminInterface = true, | ||
Logger = new WireMockConsoleLogger() | ||
}); | ||
System.Console.WriteLine("FluentMockServer listening at {0}", string.Join(",", server.Urls)); | ||
|
||
server.SetBasicAuthentication("a", "b"); | ||
|
||
server.AllowPartialMapping(); | ||
|
||
server | ||
.Given(Request.Create() | ||
.UsingGet() | ||
.WithHeader("Keep-Alive-Test", "stef") | ||
) | ||
.RespondWith(Response.Create() | ||
.WithHeader("Keep-Alive", "timeout=1, max=1") | ||
.WithBody("Keep-Alive OK") | ||
); | ||
|
||
System.Console.WriteLine("Press any key to stop the server"); | ||
System.Console.ReadKey(); | ||
server.Stop(); | ||
|
||
System.Console.WriteLine("Displaying all requests"); | ||
var allRequests = server.LogEntries; | ||
System.Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented)); | ||
|
||
System.Console.WriteLine("Press any key to quit"); | ||
System.Console.ReadKey(); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/WireMock.Net.Console.HeadersTest/WireMock.Net.Console.HeadersTest.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" /> | ||
<PackageReference Include="log4net" Version="2.0.8" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="log4net.config"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="nlog.config"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<configSections> | ||
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" /> | ||
</configSections> | ||
<appSettings> | ||
<add key="log4net.Internal.Debug" value="true"/> | ||
</appSettings> | ||
<log4net> | ||
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> | ||
<layout type="log4net.Layout.PatternLayout"> | ||
<conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" /> | ||
</layout> | ||
</appender> | ||
<root> | ||
<level value="DEBUG" /> | ||
<appender-ref ref="ConsoleAppender" /> | ||
</root> | ||
</log4net> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
autoReload="true" | ||
internalLogLevel="info" | ||
internalLogFile="c:\temp\wiremock-internal-nlog.log"> | ||
|
||
<targets> | ||
<!-- write logs to file --> | ||
<target xsi:type="File" name="all" fileName="c:\temp\wiremock-${shortdate}.log" | ||
layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}" /> | ||
</targets> | ||
|
||
<!-- rules to map from logger name to target --> | ||
<rules> | ||
<logger name="*" minlevel="Debug" writeTo="all" /> | ||
</rules> | ||
</nlog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
|
@@ -49,8 +50,13 @@ public class OwinResponseMapper | |
else | ||
{ | ||
#if !NETSTANDARD | ||
response.Headers.AppendValues(pair.Key, pair.Value.ToArray()); | ||
// For non-NETSTANDARD, check if this response header can be added (#148) | ||
if (!WebHeaderCollection.IsRestricted(pair.Key, true)) | ||
{ | ||
response.Headers.AppendValues(pair.Key, pair.Value.ToArray()); | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
StefH
Author
Collaborator
|
||
#else | ||
// NETSTANDARD can add any header (or so it seems) | ||
response.Headers.Append(pair.Key, pair.Value.ToArray()); | ||
#endif | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'm not sure this will be a long term fix. Unless I'm missing something, the restricted headers are now omitted on the response. What if that header is important for your virtualization scenario?
Also, from what I understand from https://stackoverflow.com/a/4752359 there should be an option to set the header via a specific property, which the exception suggests. I could only find examples for setting these headers via properties on the request (casting it to
HttpWebRequest
) though, not on the response.