zoukankan      html  css  js  c++  java
  • 让statlight支持文件下载

    起因:在写silverlight的功能时,需要从站点下载一些配置文件,单元测试在使用 silverlight tooltik(http://silverlight.codeplex.com/)时ok,使用statlight(http://statlight.codeplex.com/)时会发生错误,原因是statlight模拟了一个webserver,而这个模拟的webserver不支持文件下载。因此下载statlight代码对其进行了改进。

    修改位置:Statlight.Core/WebServer/ResponseFactory.cs

    修改后代码
    using System;
    using System.Globalization;
    using System.IO;
    using System.Threading;
    using StatLight.Core.Configuration;
    using StatLight.Core.Properties;
    using StatLight.Core.Serialization;

    namespace StatLight.Core.WebServer
    {
    public class ResponseFactory
    {
    private readonly Func<byte[]> _xapToTestFactory;
    private readonly byte[] _hostXap;
    private readonly string _serializedConfiguration;

    public ResponseFactory(Func<byte[]> xapToTestFactory, byte[] hostXap, ClientTestRunConfiguration clientTestRunConfiguration)
    {
    _xapToTestFactory
    = xapToTestFactory;
    _hostXap
    = hostXap;
    _serializedConfiguration
    = clientTestRunConfiguration.Serialize();
    }

    private static int _htmlPageInstanceId = 0;

    public ResponseFile Get(string localPath)
    {
    if (IsKnown(localPath, StatLightServiceRestApi.CrossDomain))
    return new ResponseFile { FileData = Resources.CrossDomain.ToByteArray(), ContentType = "text/xml" };

    if (IsKnown(localPath, StatLightServiceRestApi.ClientAccessPolicy))
    return new ResponseFile { FileData = Resources.ClientAccessPolicy.ToByteArray(), ContentType = "text/xml" };

    if (IsKnown(localPath, StatLightServiceRestApi.GetHtmlTestPage))
    {
    return GetTestHtmlPage();
    }

    if (IsKnown(localPath, StatLightServiceRestApi.GetXapToTest))
    return new ResponseFile { FileData = _xapToTestFactory(), ContentType = "application/x-silverlight-app" };

    if (IsKnown(localPath, StatLightServiceRestApi.GetTestPageHostXap))
    return new ResponseFile { FileData = _hostXap, ContentType = "application/x-silverlight-app" };

    if (IsKnown(localPath, StatLightServiceRestApi.GetTestRunConfiguration))
    return new ResponseFile { FileData = _serializedConfiguration.ToByteArray(), ContentType = "text/xml" };

    if (IsLocalFile(localPath))
    return new ResponseFile { FileData = File.ReadAllBytes(localPath), ContentType = "text" };

    throw new NotImplementedException();
    }

    [System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
    public bool IsKnownFile(string localPath)
    {
    if (IsKnown(localPath, StatLightServiceRestApi.CrossDomain))
    return true;

    if (IsKnown(localPath, StatLightServiceRestApi.ClientAccessPolicy))
    return true;

    if (IsKnown(localPath, StatLightServiceRestApi.GetHtmlTestPage))
    return true;

    if (IsKnown(localPath, StatLightServiceRestApi.GetXapToTest))
    return true;

    if (IsKnown(localPath, StatLightServiceRestApi.GetTestPageHostXap))
    return true;

    if (IsKnown(localPath, StatLightServiceRestApi.GetTestRunConfiguration))
    return true;

    return IsLocalFile(localPath);
    }


    [System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
    public static ResponseFile GetTestHtmlPage()
    {
    var page
    = Resources.TestPage.Replace("BB86D193-AD39-494A-AEB7-58F948BA5D93", _htmlPageInstanceId.ToString(CultureInfo.InvariantCulture));

    Interlocked.Increment(
    ref _htmlPageInstanceId);

    return new ResponseFile { FileData = page.ToByteArray(), ContentType = "text/html" };
    }

    private static bool IsKnown(string filea, string fileb)
    {
    return string.Equals(filea, fileb, StringComparison.OrdinalIgnoreCase);
    }

    public static void Reset()
    {
    _htmlPageInstanceId
    = 0;
    }

    /// <summary>
    /// 本地文件是否存在
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static bool IsLocalFile(string path)
    {
    return File.Exists(path);
    }
    }
    }

    注意:

    1.在运行测试时要注意项目的启动目录

    2.以及 silverlight 端只能使用相对路径访问。

    作者:Yahle
    原载:http://www.cnblogs.com/yahle
    版权所有。转载时必须以链接形式注明作者和原始出处。

    欢迎赞助:

  • 相关阅读:
    CSRF攻击原理
    大前端
    尊敬自己,才能拥有改变的力量
    重温尼采语录 序章
    人生的弹性 -- 观《聚宝盆》有感
    求学梦
    爱国情怀
    雾中见我
    找东西
    走在路上的感悟
  • 原文地址:https://www.cnblogs.com/yahle/p/1952209.html
Copyright © 2011-2022 走看看