1

我正在构建一个使用 OAuth2 的 api。我已经成功地对所有单独的部分进行了单元测试,但现在我需要进行一些集成测试。基本上我希望能够检查来自服务器的 http 响应,以确保一切正常。理想情况下,我希望能够在 Visual Studio 中启动 Web 开发服务器来托管网站,然后向它发出一堆请求并检查结果。

这样做的最佳方法是什么,我应该使用什么工具?

4

2 回答 2

2

我建议您在临时服务器上部署您的应用程序(甚至可能作为构建过程的一部分,以便这将是一个按钮单击操作),然后向该服务器触发 HTTP 客户端请求,就像真正的 .NET 客户端一样应该使用您的 API。

于 2011-05-16T18:31:18.697 回答
2

创建持续集成服务器

  1. 安装TeamCity
  2. 安装红宝石
  3. 安装长鳍金枪鱼

让 rake 脚本执行以下操作 1. 从源代码管理中签出文件 2. 在本地构建 3. 将 api 部署到本地 iis 4. 针对 localhost api 运行集成测试

这开始听起来很熟悉。看这里

这是我的 API 集成测试中的一个示例。如果您想了解更多详情,请告诉我。

我正在使用 mspec。

我在 localhost、我们的登台服务器和我们的生产服务器(一组有限的测试)上运行它,以确保所有 http 连接都正常工作。

public class _GET_no_criteria : specs_for_endpoint_test
{
    Establish context = () =>
    {
        Uri = C.Endpoint;
        Querystring = "";
        ExecuteJsonGetRequest();

        SetValidId();
    };

    It should_have_status_code_200_ok =()=>
        IsHttp_200OK();

    It should_have_categories = () =>
    {
        responseText.ShouldNotBeEmpty();
        PutsAll(responseText);
    };
}

从基类

 public static void ExecuteGetRequest(string contentType)
        {
            httpcontext = HttpContext.Current;
            request = (HttpWebRequest)WebRequest.Create(BaseUri + Uri + Querystring);
            request.Method = C.HTTP_GET;          
            request.ContentType = contentType;
            request.Headers[C.AUTHORIZATION] = token;

            // GetResponse reaises an exception on http status code 400
            // We can pull response out of the exception and continue on our way            
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse) ex.Response;
            }

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                responseText = reader.ReadToEnd();
                reader.Close();
            } 
        }

        public static void ExecuteJsonGetRequest()
        {
            ExecuteGetRequest(C.CONTENT_JSON);
        }
于 2011-05-16T18:31:45.277 回答