public void Test3() { AuthorizeAttribute a = new AuthorizeAttribute(); a.Roles = "2,3,4"; Assert.AreEqual("2,3,4", a.Roles); string requestText = @" POST http://www.fish-web-demo.com/Ajax/test/DataTypeTest/Input_int_Add.aspx HTTP/1.1 "; bool result = false; using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("fish", new string[] { "1", "2" }); result = a.AuthenticateRequest(context.HttpContext); } Assert.AreEqual(true, result); using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("fish", new string[] { "1", "3" }); result = a.AuthenticateRequest(context.HttpContext); } Assert.AreEqual(true, result); }
public void Test_OutputCacheAttribute() { OutputCacheAttribute a = new OutputCacheAttribute(); a.CacheProfile = "_CacheProfile"; a.Duration = 2; a.Location = OutputCacheLocation.Server; a.NoStore = true; a.SqlDependency = "_SqlDependency"; a.VaryByContentEncoding = "_VaryByContentEncoding"; a.VaryByCustom = "_VaryByCustom"; a.VaryByHeader = "_VaryByHeader"; a.VaryByParam = "_VaryByParam"; OutputCacheParameters settings = a.CacheSettings; Assert.AreEqual(2, settings.Duration); string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/Everyone.aspx HTTP/1.1 "; using (WebContext context = WebContext.FromRawText(requestText)) { try { a.SetResponseCache(context.HttpContext); } catch { } // 这里不太容易做断言,只能看一下代码有没有覆盖。 } }
public void Test1() { AuthorizeAttribute a = new AuthorizeAttribute(); a.Users = "fish, abc"; Assert.AreEqual("fish, abc", a.Users); string requestText = @" POST http://www.fish-mvc-demo.com/Ajax/test/DataTypeTest/Input_int_Add.aspx HTTP/1.1 "; bool result = false; using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("fish"); result = a.AuthenticateRequest(context.HttpContext); } Assert.AreEqual(true, result); using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("abc"); result = a.AuthenticateRequest(context.HttpContext); } Assert.AreEqual(true, result); }
public void Test2() { string requestText = @" GET http://www.fish-web-demo.com/Ajax/test/DataTypeTest/Input_string_ToUpper.aspx?input=fish HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36 OPR/31.0.1889.174 "; using (WebContext context = WebContext.FromRawText(requestText)) { context.Response.EnableOutputStream(); byte[] buffer = System.Text.Encoding.Unicode.GetBytes("中文汉字"); // 注意:这个文件名中,有些字符是操作系统不允许的,它们将会变成_ string filename = "中文 汉字,无乱码~`!@#$%^&*()_-+-=[]{}|:;',.<>?¥◆≠∞µαβπ™■.dat"; StreamResult streamResult = new StreamResult(buffer, "text/test", filename); streamResult.Ouput(context.HttpContext); // 获取编码后的文件名标头 string header = context.Response.GetCustomHeader("Content-Disposition"); // TODO: 现在拿到编码后的文件名了,但是不知道该如何断言,暂时先不做判断。 Assert.AreEqual("text/test", context.HttpContext.Response.ContentType); } }
public void Test_InvalidOperationException() { string requestText = @" GET http://www.fish-mvc-demo.com/Ajax/test/DataTypeTest/Input_string_ToUpper.aspx?input=fish HTTP/1.1 "; using (WebContext context = WebContext.FromRawText(requestText)) { HttpCacheResult result = new HttpCacheResult("abc", 100); result.Ouput(context.HttpContext); } }
public void Test2() { // 访问一个必须是【已登录】用户才能访问的地址 string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/LoginUser.aspx HTTP/1.1 "; string responseText = null; using (WebContext context = WebContext.FromRawText(requestText)) { // 没有设置登录用户,下面的调用将会引发异常 responseText = ExecutePage(context); } }
public void Test6() { // 访问一个必须是【权限号=23】的用户才能访问的地址 string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/RightNo23.aspx HTTP/1.1 "; string responseText = null; using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("fish"); // 模拟 fish 用户登录,还是会有异常,因为没有 23 这个权限 responseText = ExecutePage(context); } }
public void Test3() { string requestText = @" POST http://www.fish-web-demo.com/DataTypeTest/xxxxxxxxxxx HTTP/1.1 "; // 上面URL指定了一个ServiceHandlerFactory不能接受的格式,会引发404错误 IHttpHandler handler = null; using (WebContext context = WebContext.FromRawText(requestText)) { var factory = new ServiceHandlerFactory(); handler = factory.GetHandler(context.HttpContext, "POST", null, null); } }
public void Test2() { string requestText = @" POST http://www.fish-mvc-demo.com/Ajax/test/DataTypeTest/xxxxxxxxxxx.aspx HTTP/1.1 "; // 上面URL指定了一个不存在的Action名称,会引发404错误 IHttpHandler handler = null; using (WebContext context = WebContext.FromRawText(requestText)) { var factory = new ServiceHandlerFactory(); handler = factory.GetHandler(context.HttpContext, "POST", null, null); } }
public void Test1() { // 访问一个没有权限检查的地址 string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/Everyone.aspx HTTP/1.1 "; string responseText = null; using (WebContext context = WebContext.FromRawText(requestText)) { responseText = ExecutePage(context); } Assert.AreEqual(responseText, "Hello ClownFish.net"); }
public void Test_Http304Result() { string requestText = @" GET http://www.fish-web-demo.com/Ajax/test/DataTypeTest/Input_string_ToUpper.aspx?input=fish HTTP/1.1 "; using (WebContext context = WebContext.FromRawText(requestText)) { Http304Result result = new Http304Result(); result.Ouput(context.HttpContext); Assert.AreEqual(304, context.HttpContext.Response.StatusCode); } }
public void Test2() { string requestText = @" GET http://www.fish-web-demo.com/Pages/abc.zzz HTTP/1.1 "; IHttpHandler handler = null; using (WebContext context = WebContext.FromRawText(requestText)) { IHttpHandlerFactory factory = new MvcPageHandlerFactory(); handler = factory.GetHandler(context.HttpContext, "GET", "/Pages/abc.zzz", @"D:\ProjectFiles\my-github\ClownFish.net\demo\web\MvcDemoWebSite1\Pages\abc.zzz"); } //Assert.IsNull(handler); }
public void Input_string_ToUpper() { string requestText = @" GET http://www.fish-mvc-demo.com/Ajax/test/SpecialDataType/Input_HttpContext.aspx HTTP/1.1 "; using (WebContext context = WebContext.FromRawText(requestText)) { context.HttpContext.Items["unit-test"] = "ClownFish.Web"; string result = ExecuteService(context); Assert.AreEqual("ClownFish.Web", result); } }
public void Test3() { // 访问一个必须是【已登录】用户才能访问的地址 string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/LoginUser.aspx HTTP/1.1 "; string responseText = null; using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("tester"); // 模拟 tester 用户登录 responseText = ExecutePage(context); } Assert.AreEqual(responseText, "Hello ClownFish.net"); }
public void Test5() { // 访问一个必须是【fish】用户才能访问的地址 string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/Fish.aspx HTTP/1.1 "; string responseText = null; using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("fish"); // 模拟 fish 用户登录 responseText = ExecutePage(context); } Assert.AreEqual(responseText, "Hello Fish Li"); }
public void Test8() { // 访问一个必须是【权限号=23】的用户才能访问的地址 string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/RightNo23.aspx HTTP/1.1 Cookie: rightNo_demo=22 "; // 注意:上面的请求头中已经指定的权限Cookie,但是权限号不对 string responseText = null; using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("fish"); // 模拟 fish 用户登录,并且指定了Cookie(按CheckRightAttribute的要求) responseText = ExecutePage(context); } }
internal static WebContext CreateWebContext() { string requestText = @" POST http://www.bing.com/sfdjosfdj/slfjsfj/sdjfosf.aspx HTTP/1.1 Host: www.bing.com Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36 OPR/34.0.2036.25 Accept-Encoding: gzip, deflate, lzma, sdch Accept-Language: zh-CN,zh;q=0.8 Cookie: SRCHUSR=AUTOREDIR=0&GEOVAR=&DOB=20141216; _EDGE_V=1; SRCHUID=V=2&GUID=7209F97031814D0BAE91BBD675B73E65; SRCHD=D=3659242&AF=NOFORM; MUIDB=2363ECC0008A6E49222DEBD2012B6F37; MUID=2363ECC0008A6E49222DEBD2012B6F37; SRCHHPGUSR=CW=1265&CH=924&DPR=1 "; return(WebContext.FromRawText(requestText)); }
public void Test1() { string requestText = @" POST http://www.fish-web-demo.com/Ajax/test/DataTypeTest/Input_int_Add.aspx HTTP/1.1 "; IHttpHandler handler = null; using (WebContext context = WebContext.FromRawText(requestText)) { var factory = new ServiceHandlerFactory(); handler = factory.GetHandler(context.HttpContext, "POST", null, null); // 下面这个调用没什么意义,只是为了覆盖代码 factory.ReleaseHandler(handler); } Assert.IsNotNull(handler); }
public async Task <string> AsyncExecuteService(string requestText) { using (WebContext context = WebContext.FromRawText(requestText)) { ServiceHandlerFactory factory = new ServiceHandlerFactory(); IHttpHandler handler = factory.GetHandler(context.HttpContext, null, null, null); HttpTaskAsyncHandler taskHandler = handler as HttpTaskAsyncHandler; if (taskHandler == null) { throw new InvalidOperationException(); } await taskHandler.ProcessRequestAsync(context.HttpContext); return(context.Response.GetText()); } }
public void Test2() { string requestText = @" POST http://www.fish-mvc-demo.com/Ajax/test/DataTypeTest/xxxxxxxxxxx.aspx HTTP/1.1 "; // 上面URL指定了一个不存在的Action名称,会引发404错误 IHttpHandler handler = null; Http404DebugModule debugModule = new Http404DebugModule(); using (WebContext context = WebContext.FromRawText(requestText)) { debugModule.app_BeginRequest(context.Application.Instance, null); var factory = new ServiceHandlerFactory(); handler = factory.GetHandler(context.HttpContext, "POST", null, null); } Assert.IsInstanceOfType(handler, typeof(Http404PageHandler)); }
public void Test1() { string requestText = @" POST http://www.fish-web-demo.com/Ajax/test/AdvancedData/TestContextDataAttribute.aspx HTTP/1.1 "; Hashtable table = (WebRuntimeExt.Instance as WebRuntimeExt).CallMessage; table.Clear(); using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("fish"); ExecuteService(context); } Assert.AreEqual("POST", table["Request.HttpMethod"]); Assert.AreEqual("fish", table["User.Identity.Name"]); Assert.AreEqual("/", table["HttpRuntime.AppDomainAppVirtualPath"]); }
public void Test1() { string requestText = @" POST http://www.fish-web-demo.com/Ajax/test/Some/TestCookieAndHeader.aspx HTTP/1.1 Cookie: cookie1=fish header1: ClownFish.net "; // 上面URL指定了一个ServiceHandlerFactory不能接受的格式,会引发404错误 Hashtable table = (WebRuntimeExt.Instance as WebRuntimeExt).CallMessage; table.Clear(); using (WebContext context = WebContext.FromRawText(requestText)) { ExecuteService(context); Assert.AreEqual("hello_fish", context.HttpContext.Response.Cookies["cookie2"].Value); } Assert.AreEqual("hello_ClownFish.net", table["header2"]); }
public void Test4() { // 访问一个必须是【fish】用户才能访问的地址 string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/Fish.aspx HTTP/1.1 "; string responseText = null; try { using (WebContext context = WebContext.FromRawText(requestText)) { // 没有设置登录用户,下面的调用将会引发异常 responseText = ExecutePage(context); } } catch (HttpException) { using (WebContext context = WebContext.FromRawText(requestText)) { context.SetUserName("tester"); // 模拟 tester 用户登录,这里仍然会抛出异常,因为只有 fish 这个用户才能访问 responseText = ExecutePage(context); } } }
public void Test1() { string requestText = @" GET http://www.fish-web-demo.com/Pages/Demo/Authorize/Everyone.aspx HTTP/1.1 "; IHttpHandler handler = null; using (WebContext context = WebContext.FromRawText(requestText)) { IHttpHandlerFactory factory = new MvcPageHandlerFactory(); handler = factory.GetHandler(context.HttpContext, "GET", null, null); // 下面这个调用没什么意义,只是为了覆盖代码 factory.ReleaseHandler(handler); } Assert.IsNotNull(handler); object vkInfo = handler.GetValue("InvokeInfo"); object instance = vkInfo.GetValue("Instance"); Assert.AreEqual(typeof(Controllers.AuthorizeTestController), instance.GetType()); }
public void Test() { string requestText = @" GET http://www.fish-mvc-demo.com/Ajax/test/DataTypeTest/Input_string_ToUpper.aspx?input=fish HTTP/1.1 "; using (WebContext context = WebContext.FromRawText(requestText)) { TM tm = new TM { intVal = 2, StrVal = "abc" }; IActionResult result = new JsonResult(tm); result.Ouput(context.HttpContext); string json = JsonExtensions.ToJson(tm); string responseText = context.Response.GetText(); Assert.AreEqual(json, responseText); Assert.AreEqual("application/json", context.HttpContext.Response.ContentType); } }
public void Test() { // 正常场景测试 string requestText = @" GET http://www.fish-mvc-demo.com/Ajax/test/DataTypeTest/Input_string_ToUpper.aspx?input=fish HTTP/1.1 "; using (WebContext context = WebContext.FromRawText(requestText)) { TextResult text = new TextResult("abc"); DateTime lastModified = new DateTime(2015, 5, 3); HttpCacheResult result = new HttpCacheResult(text, 100, lastModified); result.ETag = "123456789"; ServiceHandlerFactory factory = new ServiceHandlerFactory(); IHttpHandler handler = factory.GetHandler(context.HttpContext, null, null, null); context.HttpContext.Handler = handler; ActionExecutor executor = new ActionExecutor(); executor.SetValue("HttpContext", context.HttpContext); handler.SetValue("ActionExecutor", executor); result.Ouput(context.HttpContext); Assert.AreEqual("abc", context.Response.GetText()); var responseCache = context.HttpContext.Response.Cache; Assert.AreEqual("Public", responseCache.GetValue("_cacheability").ToString()); Assert.AreEqual("max-age=100", responseCache.GetValue("_cacheExtension").ToString()); Assert.AreEqual("123456789", responseCache.GetValue("_etag").ToString()); Assert.AreEqual(true, (bool)responseCache.GetValue("_isLastModifiedSet")); Assert.AreEqual(lastModified.ToUniversalTime(), (DateTime)responseCache.GetValue("_utcLastModified")); } }
public void Test_Jonsp() { string requestText = @" GET http://www.fish-mvc-demo.com/Ajax/test/DataTypeTest/Input_string_ToUpper.aspx?input=fish&callback=testjsonp HTTP/1.1 Referer: http://www.fish-test-demo.com/xxx.html "; using (WebContext context = WebContext.FromRawText(requestText)) { TM tm = new TM { intVal = 2, StrVal = "abc" }; IActionResult result = new JsonResult(tm); result.Ouput(context.HttpContext); string json = "testjsonp({\"intVal\":2,\"StrVal\":\"abc\"});"; string responseText = context.Response.GetText(); Assert.AreEqual(json, responseText); Assert.AreEqual("text/javascript", context.HttpContext.Response.ContentType); } }
public void TestMethod1() { TestPage page = new TestPage(); string requestText = @" POST http://www.fish-mvc-demo.com/pages/abc.aspx?id=2&name=aa HTTP/1.1 Host: www.fish-mvc-demo.com User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0) Accept: */* Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Referer: http://www.fish-mvc-demo.com/a2.aspx?cc=5 Cookie: PageStyle=Style2; cookie1=cccccccccc h1: 111111111 h2: 22222222 a=1&b=2&c=3 "; using (WebContext context = WebContext.FromRawText(requestText)){ context.SetUserName("fish-li"); context.BindPage(page); // 如果需要访问 HttpRequest, HttpResponse 对象,可以参照这个方法来操作(在页面中访问) Encoding contentEncoding = Encoding.UTF8; string formData = "a=1&b=2&c=3"; // 请求体数据,用于后续比较,需要确保与 requestText 的最后一行数据保持一致。 Console.WriteLine("PathInfo: " + HttpContext.Current.Request.PathInfo); Console.WriteLine("PhysicalPath: " + HttpContext.Current.Request.PhysicalPath); Console.WriteLine("UserAgent: " + HttpContext.Current.Request.UserAgent); string currentPath = AppDomain.CurrentDomain.BaseDirectory; // 开始测试 Assert.AreEqual(currentPath, page.GetAppDomainPath()); Assert.AreEqual(currentPath + @"\test.dat", page.GetServerMappingPath("/test.dat")); Assert.AreEqual(currentPath + @"\test.dat", page.GetServerMappingPath("~/test.dat")); Assert.AreEqual(currentPath + @"\pages\test.dat", page.GetServerMappingPath("test.dat")); Assert.AreEqual(currentPath + @"\pages\dd\test.dat", page.GetServerMappingPath("dd/test.dat")); Assert.AreEqual(currentPath + @"\test.dat", page.GetRequestMappingPath("/test.dat")); Assert.AreEqual(currentPath + @"\test.dat", page.GetRequestMappingPath("~/test.dat")); Assert.AreEqual(currentPath + @"\pages\test.dat", page.GetRequestMappingPath("test.dat")); Assert.AreEqual(currentPath + @"\pages\dd\test.dat", page.GetRequestMappingPath("dd/test.dat")); Assert.AreEqual("fish-li", HttpContext.Current.User.Identity.Name); Assert.AreEqual(true, HttpContext.Current.Request.IsAuthenticated); Assert.AreEqual(true, HttpContext.Current.User.Identity.IsAuthenticated); Assert.AreEqual("/a2.aspx?cc=5", HttpContext.Current.Request.UrlReferrer.PathAndQuery); Assert.AreEqual("/pages/abc.aspx", HttpContext.Current.Request.FilePath); Assert.AreEqual("/pages/abc.aspx", HttpContext.Current.Request.Path); Assert.AreEqual("/", HttpContext.Current.Request.ApplicationPath); Assert.AreEqual("~/pages/abc.aspx", HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath); Assert.AreEqual(contentEncoding, HttpContext.Current.Request.ContentEncoding); Assert.AreEqual("application/x-www-form-urlencoded", HttpContext.Current.Request.ContentType); Assert.AreEqual("/pages/abc.aspx?id=2&name=aa", HttpContext.Current.Request.RawUrl); Assert.AreEqual("POST", HttpContext.Current.Request.RequestType); Assert.AreEqual("http://www.fish-mvc-demo.com/pages/abc.aspx", HttpContext.Current.Request.Url.AbsoluteUri); Assert.AreEqual(contentEncoding.GetByteCount(formData), HttpContext.Current.Request.ContentLength); Assert.AreEqual("IE", HttpContext.Current.Request.Browser.Browser); Assert.AreEqual(7, HttpContext.Current.Request.Browser.MajorVersion); Assert.AreEqual("cccccccccc", HttpContext.Current.Request.Cookies["cookie1"].Value); Assert.AreEqual("2", page.ReadQuerySting("id")); Assert.AreEqual("aa", page.ReadQuerySting("name")); Assert.AreEqual("aa", page.ReadParams("name")); Assert.AreEqual("aa", page.ReadItem("name")); Assert.AreEqual("aa", HttpContext.Current.Request.QueryString["name"]); Assert.AreEqual("1", page.ReadForm("a")); Assert.AreEqual("2", page.ReadForm("b")); Assert.AreEqual("3", page.ReadForm("c")); Assert.AreEqual("111111111", page.ReadHeader("h1")); Assert.AreEqual("22222222", page.ReadHeader("h2")); Guid sessionData = Guid.NewGuid(); page.WriteSession("s1", sessionData); Assert.AreEqual(sessionData, (Guid)page.ReadSession("s1")); Assert.AreEqual(sessionData, (Guid)HttpContext.Current.Session["s1"]); int num1 = 123; page.WriteApplication("key1", num1); Assert.AreEqual(num1, (int)page.ReadApplication("key1")); page.SetResponseContentType("application/octet-stream"); Assert.AreEqual("application/octet-stream", HttpContext.Current.Response.ContentType); string writeText = "SQL语法分析和SQL解释实现. SQL语法分析/解释。为设计/实现SQL语法分析器提供参考"; page.WriteToResponse(writeText); Assert.AreEqual(writeText, context.Response.GetText()); context.Request.SetInputStream(writeText); Assert.AreEqual(writeText, page.GetInputStreamString()); string cookieData = Guid.NewGuid().ToString(); page.WriteCookie("test-write-cookie1", cookieData); Assert.AreEqual(cookieData, page.ReadCookie("test-write-cookie1")); string headerData = Guid.NewGuid().ToString(); page.WriteHeader("test-write-header", headerData); ArrayList customHeaders = context.Response.Response.GetValue("_customHeaders") as ArrayList; object header = customHeaders[0]; Assert.AreEqual("test-write-header", (string)header.GetValue("Name")); Assert.AreEqual(headerData, (string)header.GetValue("Value")); } }
public string ExecuteService(string requestText) { using (WebContext context = WebContext.FromRawText(requestText)) { return(ExecuteService(context)); } }