3 using NUnit.Framework.TUnit;
4 using Tizen.NUI.Components;
5 using Tizen.NUI.BaseComponents;
6 using System.Threading.Tasks;
10 namespace Tizen.NUI.Devel.Tests
12 using tlog = Tizen.Log;
15 [Description("internal/WebView/WebHttpRequestInterceptor")]
16 public class InternalWebHttpRequestInterceptorTest
18 private const string tag = "NUITEST";
19 private string invalidUrl = "https://test/";
20 private BaseComponents.WebView webview;
25 tlog.Info(tag, "Init() is called!");
26 webview = new BaseComponents.WebView()
28 Size = new Size(500, 200),
35 tlog.Info(tag, "Destroy() is being called!");
37 tlog.Info(tag, "Destroy() is called!");
42 [Description("WebHttpRequestInterceptor SetResponseBody.")]
43 [Property("SPEC", "Tizen.NUI.WebHttpRequestInterceptor.SetResponseBody M")]
44 [Property("SPEC_URL", "-")]
45 [Property("CRITERIA", "CONSTR")]
46 [Property("COVPARAM", "")]
47 [Property("AUTHOR", "guowei.wang@samsung.com")]
48 public async Task WebHttpRequestInterceptorSetResponseBody()
50 tlog.Debug(tag, $"WebHttpRequestInterceptorConstructor START");
52 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(false);
53 WebContext.HttpRequestInterceptedCallback onRequestIntercepted = (interceptor) =>
55 tlog.Info(tag, $"onRequestIntercepted is called, url {interceptor.Url}.");
56 if (interceptor.Url.Equals(invalidUrl))
58 Assert.IsNotNull(interceptor.InterceptedWebView, "InterceptedWebView should not be null.");
59 Assert.IsNotNull(interceptor.Headers, "Headers should not be null.");
61 byte[] bData = Encoding.UTF8.GetBytes("<html><body><img src='test.jpg'></body></html>");
62 interceptor.SetResponseStatus(200, "OK");
63 interceptor.AddResponseHeader("Content-Type", "text/html; charset=UTF-8");
64 interceptor.AddResponseHeader("Content-Length", bData.Length.ToString());
65 interceptor.SetResponseBody(bData);
67 tlog.Info(tag, $"http request intercepted set response body end, {interceptor.Method}");
69 else if (interceptor.Url.Equals($"{invalidUrl}test.jpg"))
71 string path = $"{Applications.Application.Current.DirectoryInfo.Resource}webview/tizen.png";
72 using (FileStream fs = File.OpenRead(path))
74 byte[] bData = new byte[1024];
75 while (fs.Read(bData, 0, bData.Length) > 0)
77 interceptor.WriteResponseChunk(bData);
79 interceptor.WriteResponseChunk((byte[])null);
80 tlog.Info(tag, $"http request intercepted write chunk end");
85 tlog.Info(tag, $"http request intercepted end, url {interceptor.Url}");
86 tcs.TrySetResult(true);
90 webview.Context.RegisterHttpRequestInterceptedCallback(onRequestIntercepted);
92 webview.LoadUrl(invalidUrl);
93 var result = await tcs.Task;
94 Assert.IsTrue(result, "HttpAuthRequested event should be invoked.");
96 // Make current thread (CPU) sleep...
99 webview.Context.RegisterHttpRequestInterceptedCallback(null);
101 tlog.Debug(tag, $"WebHttpRequestInterceptorConstructor END (OK)");
106 [Description("WebHttpRequestInterceptor constructor.")]
107 [Property("SPEC", "Tizen.NUI.WebHttpRequestInterceptor.Ignore M")]
108 [Property("SPEC_URL", "-")]
109 [Property("CRITERIA", "CONSTR")]
110 [Property("COVPARAM", "")]
111 [Property("AUTHOR", "guowei.wang@samsung.com")]
112 public async Task WebHttpRequestInterceptorIgnore()
114 tlog.Debug(tag, $"WebHttpRequestInterceptorIgnore START");
116 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(false);
117 WebContext.HttpRequestInterceptedCallback onRequestIntercepted = (interceptor) =>
119 interceptor.Ignore();
122 webview.Context.RegisterHttpRequestInterceptedCallback(onRequestIntercepted);
124 EventHandler<WebViewPageLoadErrorEventArgs> onLoadError = (s, e) =>
126 tlog.Info(tag, $"http load error!");
127 tcs.TrySetResult(true);
129 webview.PageLoadError += onLoadError;
131 webview.LoadUrl(invalidUrl);
132 var result = await tcs.Task;
133 Assert.IsTrue(result, "PageLoadError event should be invoked.");
135 // Make current thread (CPU) sleep...
138 webview.Context.RegisterHttpRequestInterceptedCallback(null);
139 webview.PageLoadError -= onLoadError;
141 tlog.Debug(tag, $"WebHttpRequestInterceptorIgnore END (OK)");