/// <summary> /// Builds from web response. /// </summary> /// <param name="webResponseDetails"><see cref="WebResponseDetails" /> object</param> /// <exception cref="System.ArgumentNullException">webResponseDetails;Incorrect web response</exception> public void BuildFromWebResponse(IWebResponseDetails webResponseDetails) { if (webResponseDetails == null) { throw new ArgumentNullException("webResponseDetails", "Incorrect web response"); } swiftObjectsCollection = new SwiftObjectsCollection(); // // Body if (string.IsNullOrEmpty(webResponseDetails.Body) == false) { List <SwiftObject> tmpObjects = new List <SwiftObject>(); using (MemoryStream mStream = new MemoryStream(System.Text.Encoding.Default.GetBytes(webResponseDetails.Body))) { DataContractJsonSerializer deSerializer = new DataContractJsonSerializer(tmpObjects.GetType()); try { tmpObjects = deSerializer.ReadObject(mStream) as List <SwiftObject>; swiftObjectsCollection.AddRange(tmpObjects); } catch (FormatException exp_format) { System.Diagnostics.Trace.WriteLine("[ContainerCollectionParser::BuildFromWebResponse] Could not desterilize data. Raw data: " + webResponseDetails.Body + "\n\nException: " + exp_format.ToString()); throw; } } } else { // No objects ? } }
public void Should_not_throw_on_missing_body() { IWebResponseDetails details = MockRepository.GenerateMock <IWebResponseDetails>(null); details.Expect(d => d.Headers).Return(new Dictionary <string, string>()); details.Expect(d => d.Body).Return(null); Assert.DoesNotThrow(() => { containerCollectionParser.BuildFromWebResponse(details); }); }
public void Should_not_throw_on_missing_headers() { IWebResponseDetails details = MockRepository.GenerateMock <IWebResponseDetails>(null); details.Expect(d => d.Headers).Return(new Dictionary <string, string>()); Assert.DoesNotThrow(() => { accountDetailsParser.BuildFromWebResponse(details); }); Assert.NotNull(accountDetailsParser.Data); accountDetails = accountDetailsParser.Data; Assert.Equal(accountDetails.BytesUsed, 0); Assert.Equal(accountDetails.ContainerCount, 0); Assert.Equal(accountDetails.ObjectsCount, 0); }
public void Should_throw_on_incorrect_headers() { Dictionary <string, string> wrongHeaders = new Dictionary <string, string>(); wrongHeaders.Add(AccountDetailsParser.HEADER_BYTES_USED, "wrong_header_value"); IWebResponseDetails details = MockRepository.GenerateMock <IWebResponseDetails>(null); details.Expect(d => d.Headers).Return(wrongHeaders); Assert.Throws(typeof(FormatException), () => { accountDetailsParser.BuildFromWebResponse(details); }); Assert.NotNull(accountDetailsParser.Data); accountDetails = accountDetailsParser.Data; Assert.Equal(accountDetails.BytesUsed, 0); }
/// <summary> /// Builds from web response. /// </summary> /// <param name="webResponseDetails"><see cref="WebResponseDetails" /> object</param> public void BuildFromWebResponse(IWebResponseDetails webResponseDetails) { // Nothing to do }
/// <summary> /// Builds from web response. /// </summary> /// <param name="webResponseDetails"><see cref="WebResponseDetails" /> object</param> /// <example> /// Headers: /// /// HTTP/1.1 200 OK /// Content-Length: 8 /// Accept-Ranges: bytes /// X-Timestamp: 1370188777.21130 /// X-Account-Bytes-Used: 0 /// X-Account-Container-Count: 1 /// Content-Type: text/plain; charset=utf-8 /// X-Account-Object-Count: 0 /// Date: Wed, 05 Jun 2013 11:53:50 GMT /// /// Body: /// cont123 /// </example> public void BuildFromWebResponse(IWebResponseDetails webResponseDetails) { if ((webResponseDetails == null) || (webResponseDetails.Headers == null)) { throw new ArgumentException("Incorrect web-response"); } data = new ContainerCollection(); int iData = 0; string strData = string.Empty; // // BytesUsed if (webResponseDetails.Headers.ContainsKey(HEADER_BYTES_USED)) { strData = webResponseDetails.Headers[HEADER_BYTES_USED]; if (string.IsNullOrEmpty(strData) == false) { if (int.TryParse(strData, out iData)) { data.BytesUsed = iData; } else { throw new FormatException("Header parameter 'BytesCount' could not be converted to integer value. Raw data: " + strData); } } else { data.BytesUsed = 0; } } else { data.BytesUsed = 0; } // // Containers count if (webResponseDetails.Headers.ContainsKey(HEADER_CONTAINERS_COUNT)) { strData = webResponseDetails.Headers[HEADER_CONTAINERS_COUNT]; if (string.IsNullOrEmpty(strData) == false) { if (int.TryParse(strData, out iData)) { data.ContainerCount = iData; } else { throw new FormatException("Header parameter 'Container count' could not be converted to integer value. Raw data: " + strData); } } else { data.ContainerCount = 0; } } else { data.ContainerCount = 0; } // // Body if (string.IsNullOrEmpty(webResponseDetails.Body) == false) { List <Container> tmpContainers = new List <Container>(); using (MemoryStream mStream = new MemoryStream(System.Text.Encoding.Default.GetBytes(webResponseDetails.Body))) { DataContractJsonSerializer deSerializer = new DataContractJsonSerializer(tmpContainers.GetType()); try { tmpContainers = deSerializer.ReadObject(mStream) as List <Container>; data.AddRange(tmpContainers); } catch (FormatException exp_format) { System.Diagnostics.Trace.WriteLine("[ContainerCollectionParser::BuildFromWebResponse] Could not desterilize data. Raw data: " + webResponseDetails.Body + "\n\nException: " + exp_format.ToString()); throw; } } } }
/// <summary> /// Builds from web response. /// </summary> /// <param name="webResponseDetails">The details of SWIFT server response</param> /// <example> /// HTTP/1.1 204 No Content /// Content-Length: 0 /// Accept-Ranges: bytes /// X-Timestamp: 1370188777.21130 /// X-Account-Bytes-Used: 0 /// X-Account-Container-Count: 1 /// Content-Type: text/plain; charset=utf-8 /// X-Account-Object-Count: 0 /// Date: Mon, 03 Jun 2013 08:34:03 GMT /// </example> public void BuildFromWebResponse(IWebResponseDetails webResponseDetails) { if ((webResponseDetails == null) || (webResponseDetails.Headers == null)) { throw new ArgumentException("Incorrect web-response"); } details = new AccountDetails(); int iData = 0; string strData = string.Empty; // // BytesUsed if (webResponseDetails.Headers.ContainsKey(HEADER_BYTES_USED)) { strData = webResponseDetails.Headers[HEADER_BYTES_USED]; if (string.IsNullOrEmpty(strData) == false) { if (int.TryParse(strData, out iData)) { details.BytesUsed = iData; } else { throw new FormatException("Header parameter 'BytesCount' could not be converted to integer value. Raw data: " + strData); } } else { details.BytesUsed = 0; } } else { details.BytesUsed = 0; } // // Containers count if (webResponseDetails.Headers.ContainsKey(HEADER_CONTAINERS_COUNT)) { strData = webResponseDetails.Headers[HEADER_CONTAINERS_COUNT]; if (string.IsNullOrEmpty(strData) == false) { if (int.TryParse(strData, out iData)) { details.ContainerCount = iData; } else { throw new FormatException("Header parameter 'Container count' could not be converted to integer value. Raw data: " + strData); } } else { details.ContainerCount = 0; } } else { details.ContainerCount = 0; } // // Object count if (webResponseDetails.Headers.ContainsKey(HEADER_OBJECT_COUNT)) { strData = webResponseDetails.Headers[HEADER_OBJECT_COUNT]; if (string.IsNullOrEmpty(strData) == false) { if (int.TryParse(strData, out iData)) { details.ObjectsCount = iData; } else { throw new FormatException("Header parameter 'Object count' could not be converted to integer value. Raw data: " + strData); } } else { details.ObjectsCount = 0; } } else { details.ObjectsCount = 0; } }