I have some custom response filters that I’m trying to use to apply header changes on responses. However, it seems that the headers are getting changed/overwritten at a later point. I’ve tried with both GlobalResponseFilters (which has worse results) and now attributes.
Below is full test code that shows the problem. It fails because my Vary headers don’t exist as set. Vary is set to accept only.
[TestFixture()]
public class ResponseFilterTests : BaseServiceTest
{
protected override AppHostMocked CreateAppHost()
{
return (AppHostMocked)new AppHostMocked(typeof(ResponseFilterTests).Assembly)
.Init()
.Start(BaseUri);
}
[Test]
public void ResponseFilterIShouldHaveVaryFilterTest()
{
using (var client = new JsonHttpClient(BaseUri))
{
client.ResponseFilter = message =>
{
message.Headers.Vary.Should().Contain("origin");
message.Headers.Vary.Should().Contain("authorization");
message.Headers.CacheControl.Should().Be("no-cache");
};
var response = client.Get(new ResponseFilterWithVaryRequest());
response.Should().Be("Should have vary headers.");
}
}
}
[NoCacheResponseFilter]
[VaryResponseFilter]
public class ResponseFilterService : IService
{
public object Get(ResponseFilterWithVaryRequest withVaryRequest)
{
return "Should have vary headers.";
}
}
public class ResponseFilterWithVaryRequest: IGet, IReturn<string>, IShouldHaveVaryFilter
{
}
public class NoCacheResponseFilterAttribute : ResponseFilterAttribute
{
public override void Execute(IRequest req, IResponse res, object responseDto)
{
res.AddHeader("Cache-Control", "no-cahce");
}
}
public class VaryResponseFilterAttribute : ResponseFilterAttribute
{
public override void Execute(IRequest req, IResponse res, object responseDto)
{
res.AddHeader(HttpHeaders.Vary, "origin, authorization");
}
}