private static FileContent CreateMockContent(FileRevision f) { var data = Encoding.UTF8.GetBytes(String.Format("{0} r{1}", f.File.Name, f.Revision.ToString())); var content = new FileContentData(data, data.Length); return(new FileContent(f.File.Name, content)); }
public void GetCvsRevision_ReturnsExistingFileIfPresent() { var f = new FileRevision(new FileInfo("file.txt"), Revision.Create("1.1"), mergepoint: Revision.Empty, time: DateTime.Now, author: "fred", commitId: "c1"); var contents = new FileContentData(new byte[] { 1, 2, 3, 4 }, 4); var repo1 = MockRepository.GenerateStub <ICvsRepository>(); repo1.Stub(r => r.GetCvsRevision(f)).Return(new FileContent("file.txt", contents)); var cache1 = new CvsRepositoryCache(m_temp.Path, repo1); cache1.GetCvsRevision(f); // create a second cache var repo2 = MockRepository.GenerateMock <ICvsRepository>(); var cache2 = new CvsRepositoryCache(m_temp.Path, repo1); var data = cache2.GetCvsRevision(f); repo2.AssertWasNotCalled(r => r.GetCvsRevision(f)); Assert.AreNotSame(data.Data, contents); Assert.IsTrue(data.Data.Equals(contents)); }
/// <summary> /// Stores the provided rewrite data /// </summary> /// <param name="rewriteCount">Rewrite count of file</param> /// <param name="contentHash">Hash of file</param> /// <param name="contentLength">Optional, length of file</param> internal void AddFileRewriteData(int rewriteCount, ContentHash contentHash, long contentLength = UnknownContentLength) { // Negative rewrite count is nonsensical Contract.Requires(rewriteCount >= 0); // Check if the list is already large enough to insert the data if (rewriteCount < m_fileContentData.Count) { if (m_fileContentData[rewriteCount] == null) { // Put new data into list m_fileContentData[rewriteCount] = new FileContentData(contentHash, contentLength); } else { // We have seen data before for this rewrite count value so only use new data if it is 'better' than data we have already seen // Better here means that it has a known content length. The hash value shouldn't ever be different but the content length isn't always known if (contentLength != UnknownContentLength && m_fileContentData[rewriteCount].ContentLength == UnknownContentLength) { // New data is better than old data so update to new data m_fileContentData[rewriteCount] = new FileContentData(contentHash, contentLength); } } } else { // Grow the list to fit the new data and add it m_fileContentData.AddRange(Enumerable.Repeat((FileContentData)null, rewriteCount - m_fileContentData.Count + 1)); m_fileContentData[rewriteCount] = new FileContentData(contentHash, contentLength); } // We keep track of the largest rewrite count for each file and assume that the data for the file corresponds to the largest rewrite count data RewriteCount = Math.Max(RewriteCount, rewriteCount); }
public void Equals_SameDataAndLength_ReturnsTrue() { var x = new FileContentData(new byte[] { 1, 2, 3, }, 2); var y = new FileContentData(new byte[] { 1, 2, }, 2); Assert.IsTrue(x.Equals(y)); Assert.IsTrue(y.Equals(x)); }
public void Equals_SameDataDifferentLength_ReturnsFalse() { var x = new FileContentData(new byte[] { 1, 2, 3, }, 2); var y = new FileContentData(new byte[] { 1, 2, 3, }, 3); Assert.IsFalse(x.Equals(y)); Assert.IsFalse(y.Equals(x)); }
public void Equals_DifferentData_ReturnsFalse() { var x = new FileContentData(new byte[] { 1, 2, 3, }, 4); var y = new FileContentData(new byte[] { 2, 3, 4, }, 3); Assert.IsFalse(x.Equals(y)); Assert.IsFalse(y.Equals(x)); }
public void EmptyFile() { var data = new FileContentData(new byte[0]); var file = new FileContent("file", data); Assert.AreEqual("file", file.Name); Assert.AreEqual(0, file.Data.Length); Assert.IsFalse(file.IsDead); }
public FileContent(string path, FileContentData data) : this(path, data, false) { }
/// <summary> /// Get bytes for HTTP file Content /// </summary> private static byte[] GetFileContents(FileContentData[] fileDatas, string boundary) { var result = new List<byte>(); foreach (var fileData in fileDatas) { if (File.Exists(fileData.FilePath)) { var file = new FileInfo(fileData.FilePath); // Write Header Boundary string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, fileData.Id, fileData.FilePath, fileData.ContentType); result.AddRange(GetBytes("\r\n--" + boundary + "\r\n")); result.AddRange(GetBytes(header)); // Write File Contents using (var fileStream = new FileStream(fileData.FilePath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[file.Length]; using (var memStream = new MemoryStream()) { int read; while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0) { memStream.Write(buffer, 0, read); } result.AddRange(memStream.ToArray()); } } } } return result.ToArray(); }
private static string SendData( string method, string url, PostContentData[] postDatas = null, FileContentData[] fileDatas = null, HeaderData[] headers = null, string userAgent = null, NetworkCredential credential = null, ProxySettings proxySettings = null, int timeout = TIMEOUT, int maxAttempts = CONNECTION_ATTEMPTS, bool getResponse = true) { var response = SendData(false, method, url, postDatas, fileDatas, headers, userAgent, credential, proxySettings, timeout, maxAttempts, getResponse); if (response != null && response.Body != null) return Encoding.ASCII.GetString(response.Body); else return null; }
private static ResponseInfo SendData( bool returnBytes, string method, string url, PostContentData[] postDatas = null, FileContentData[] fileDatas = null, HeaderData[] headers = null, string userAgent = null, NetworkCredential credential = null, ProxySettings proxySettings = null, int timeout = TIMEOUT, int maxAttempts = CONNECTION_ATTEMPTS, bool getResponse = true ) { ResponseInfo result = null; int attempts = 0; bool success = false; string message = null; // Try to send data for number of connectionAttempts while (attempts < maxAttempts && !success) { attempts += 1; try { // Create HTTP request and define Header info var request = (HttpWebRequest)WebRequest.Create(url); string boundary = String_Functions.RandomString(10); request.Timeout = timeout; request.ReadWriteTimeout = timeout; if (method == "POST") request.ContentType = "multipart/form-data; boundary=" + boundary; else request.ContentType = "application/x-www-form-urlencoded"; // Set the Method request.Method = method; // Set the UserAgent if (userAgent != null) request.UserAgent = userAgent; else request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; // Add Header data to request stream (if present) if (headers != null) { foreach (var header in headers) { request.Headers[header.Id] = header.Text; } } // set NetworkCredentials if (credential != null) { request.Credentials = credential; request.PreAuthenticate = true; } // Get Default System Proxy (Windows Internet Settings -> Proxy Settings) var proxy = WebRequest.GetSystemWebProxy(); // Get Custom Proxy Settings from Argument (overwrite default proxy settings) if (proxySettings != null) { if (proxySettings.Address != null && proxySettings.Port > 0) { var customProxy = new WebProxy(proxySettings.Address, proxySettings.Port); customProxy.BypassProxyOnLocal = false; proxy = customProxy; } } request.Proxy = proxy; var bytes = new List<byte>(); // Add Post Name/Value Pairs if (postDatas != null) { string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (var postData in postDatas) { string formitem = string.Format(formdataTemplate, postData.Name, postData.Value); bytes.AddRange(GetBytes("\r\n--" + boundary + "\r\n")); bytes.AddRange(GetBytes(formitem)); } } // Add File data if (fileDatas != null) { bytes.AddRange(GetFileContents(fileDatas, boundary)); } if (bytes.Count > 0) { // Write Trailer Boundary string trailer = "\r\n--" + boundary + "--\r\n"; bytes.AddRange(GetBytes(trailer)); var byteArray = bytes.ToArray(); // Write Data to Request Stream request.ContentLength = byteArray.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(byteArray, 0, byteArray.Length); } } // Get Response Message from HTTP Request if (getResponse) { result = new ResponseInfo(); using (var response = (HttpWebResponse)request.GetResponse()) { // Get HTTP Response Body using (var responseStream = response.GetResponseStream()) using (var memStream = new MemoryStream()) { byte[] buffer = new byte[10240]; int read; while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) { memStream.Write(buffer, 0, read); } result.Body = memStream.ToArray(); success = true; } var responseHeaders = new List<ReponseHeaderInfo>(); // Get HTTP Response Headers foreach (var key in response.Headers.AllKeys) { var header = new ReponseHeaderInfo(); header.Key = key; header.Value = response.Headers.Get(key); responseHeaders.Add(header); } result.Headers = responseHeaders.ToArray(); } } else success = true; } catch (WebException wex) { message = wex.Message; } catch (Exception ex) { message = ex.Message; } if (!success) System.Threading.Thread.Sleep(500); } if (!success) Logger.Log("Send :: " + attempts.ToString() + " Attempts :: URL = " + url + " :: " + message); return result; }
public void Equals_Null_ReturnsFalse() { var x = new FileContentData(new byte[] { 1, 2, 3, }, 2); Assert.IsFalse(x.Equals(null)); }
public void Equals_SameInstance_ReturnsTrue() { var x = new FileContentData(new byte[] { 1, 2, 3, }, 2); Assert.IsTrue(x.Equals(x)); }
private FileContent(string path, FileContentData data, bool isDead) { this.Name = path; this.Data = data; this.IsDead = isDead; }