protected virtual string ProcessJavaScript(string url) { RRTracer.Trace("Beginning Processing {0}", url); string jsContent = string.Empty; url = url.Replace("&", "&"); RRTracer.Trace("Beginning to Download {0}", url); using (var response = WebClientWrapper.Download <JavaScriptResource>(url)) { if (response == null) { RRTracer.Trace("Response is null for {0}", url); return(null); } var expires = response.Headers["Expires"]; try { if (!string.IsNullOrEmpty(expires) && DateTime.Parse(expires) < DateTime.Now.AddDays(6)) { RRTracer.Trace("{0} expires in less than a week", url); AddUrlToIgnores(url); } } catch (FormatException) { RRTracer.Trace("Format exception thrown parsing expires of {0}", url); } var cacheControl = response.Headers["Cache-Control"]; if (!string.IsNullOrEmpty(cacheControl)) { var cacheControlVals = cacheControl.ToLower().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var val in cacheControlVals) { try { if ((val.Contains("no-") || (val.Contains("max-age") && Int32.Parse(val.Trim().Remove(0, 8)) < (60 * 60 * 24 * 7)))) { RRTracer.Trace("{0} max-age in less than a week", url); AddUrlToIgnores(url); } } catch (FormatException) { RRTracer.Trace("Format exception thrown parsing max-age of {0}", url); } } } using (var responseStream = response.GetResponseStream()) { if (responseStream != null) { using (var streamDecoder = responseDecoder.GetDecodableStream(responseStream, response.Headers["Content-Encoding"])) { using (var streameader = new StreamReader(streamDecoder, Encoding.UTF8)) { jsContent = streameader.ReadToEnd(); RRTracer.Trace("content of {0} read and has {1} bytes and a length of {2}", url, response.ContentLength, jsContent.Length); } } } } } return(jsContent); }
protected virtual string ProcessJavaScript(string url) { string jsContent = string.Empty; url = url.Replace("&", "&"); using (var response = WebClientWrapper.Download <JavaScriptResource>(url)) { if (response == null) { return(null); } var expires = response.Headers["Expires"]; try { if (!string.IsNullOrEmpty(expires) && DateTime.Parse(expires) < DateTime.Now.AddDays(6)) { AddUrlToIgnores(url); } } catch (FormatException) { } var cacheControl = response.Headers["Cache-Control"]; if (!string.IsNullOrEmpty(cacheControl)) { var cacheControlVals = cacheControl.ToLower().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var val in cacheControlVals) { try { if ((val.Contains("no-") || (val.Contains("max-age") && Int32.Parse(val.Trim().Remove(0, 8)) < (60 * 60 * 24 * 7)))) { AddUrlToIgnores(url); } } catch (FormatException) {} } } using (var responseStream = response.GetResponseStream()) { if (responseStream != null) { using (var streamDecoder = responseDecoder.GetDecodableStream(responseStream, response.Headers["Content-Encoding"])) { using (var streameader = new StreamReader(streamDecoder, Encoding.UTF8)) { jsContent = streameader.ReadToEnd(); } } } } } return(jsContent); }
public void WillThrowErrorIfNotJavaScript() { if (ConfigurationManager.AppSettings["Environment"] == "Test") { return; } var wrapper = new WebClientWrapper(); var ex = Assert.Throws <InvalidOperationException>(() => wrapper.Download <JavaScriptResource>("http://localhost:8877/local.html")); Assert.NotNull(ex); }