无法在单元测试中检查响应标头 [英] Unable to examine reponse headers in unit test

查看:52
本文介绍了无法在单元测试中检查响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Http处理程序进行了单元测试.在其中,我创建一个 HttpResponse 对象,并将其传递给我的Http处理程序的方法之一.

I have a unit test for an Http handler. In it I create a HttpResponse object and pass it to one of my Http handler's methods.

我的一项测试试图验证是否正确设置了响应头:

One of my tests attempts to verify that the response headers have been set correctly:

Assert.AreEqual( "gzip", response.Headers["Content-Encoding"]);

但是, Headers 属性会引发 PlatformNotSupportedException ,并显示消息此操作需要IIS集成管道模式"..

However, the Headers property throws a PlatformNotSupportedException with the message "This operation requires IIS integrated pipeline mode".

奇怪的是,据我了解,该异常与设置响应标头有关-不读取它们.我正在使用TDD,因此(尚未)在任何地方都未设置标头,但仍然会出现异常.

The strange thing is that as I understand it, that exception is related to setting reponse headers - not reading them. I'm using TDD, so I don't set the headers anywhere (yet), but still I'm getting the exception.

为什么我会收到此异常,并且有更好或更好的方法来对响应标头进行单元测试?

Why am I getting this exception and is there a good or better way to unit test response headers?

推荐答案

来自 Response.Headers文档:

备注

仅支持Headers属性与IIS 7.0集成管道模式和至少.NET Framework3.0.当您尝试访问Headers属性时,其中之一不满足两个条件,一个PlatformNotSupportedException是抛出.

The Headers property is only supported with the IIS 7.0 integrated pipeline mode and at least the .NET Framework 3.0. When you try to access the Headers property and either of these two conditions is not met, a PlatformNotSupportedException is thrown.

基本上,除非您在那些条件下运行,否则您甚至无法尝试访问它.

Basically you can't even attempt to access it unless you're running with those conditions.

如果我是您,则将为您的Handler创建一个构造函数,该构造函数接受使用模拟为了正确测试您的标头.

If I were you, I would create a constructor for your Handler that accepts an HttpContextBase object and use a mock in order to test your headers properly.

这篇关于无法在单元测试中检查响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆