Guest User

Untitled

a guest
Jan 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. namespace Nancy.Testing
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8. using Nancy.Json;
  9.  
  10. /// <summary>
  11. /// Extension method for formatting the contents of a <see cref="BrowserResponseBodyWrapper"/>.
  12. /// </summary>
  13. public static class BrowserResponseBodyWrapperExtensions
  14. {
  15. /// <summary>
  16. /// Gets the HTTP response body wrapped in a <see cref="Stream"/>.
  17. /// </summary>
  18. /// <param name="bodyWrapper">An instance of the <see cref="BrowserResponseBodyWrapper"/> that the extension should be invoked on.</param>
  19. /// <returns>A <see cref="Stream"/> representation of the HTTP response body.</returns>
  20. public static Stream AsStream(this BrowserResponseBodyWrapper bodyWrapper)
  21. {
  22. return new MemoryStream(bodyWrapper.ToArray());
  23. }
  24.  
  25. /// <summary>
  26. /// Gets the HTTP response body wrapped in a string.
  27. /// </summary>
  28. /// <param name="bodyWrapper">An instance of the <see cref="BrowserResponseBodyWrapper"/> that the extension should be invoked on.</param>
  29. /// <value>A string containing the HTTP response body.</value>
  30. public static string AsString(this BrowserResponseBodyWrapper bodyWrapper)
  31. {
  32. return Convert.ToString(bodyWrapper);
  33. }
  34.  
  35. /// <summary>
  36. /// Gets the HTTP response body as a <see cref="XmlDocument"/>
  37. /// </summary>
  38. /// <param name="bodyWrapper">An instance of the <see cref="BrowserResponseBodyWrapper"/> that the extension should be invoked on.</param>
  39. /// <value>A <see cref="XmlDocument"/> representation of the HTTP response body.</value>
  40. public static XmlDocument AsXmlDocument(this BrowserResponseBodyWrapper bodyWrapper)
  41. {
  42. var document =
  43. new XmlDocument();
  44. document.LoadXml(bodyWrapper.AsString());
  45.  
  46. return document;
  47. }
  48.  
  49. /// <summary>
  50. /// Gets the deserialized representation of the JSON in the response body.
  51. /// </summary>
  52. /// <typeparam name="TModel">The type that the JSON response body should be deserialized to.</typeparam>
  53. /// <param name="bodyWrapper">An instance of the <see cref="BrowserResponseBodyWrapper"/> that the extension should be invoked on.</param>
  54. /// <value>A <typeparamref name="TModel"/> instance representation of the HTTP response body.</value>
  55. public static TModel DeserializeJson<TModel>(this BrowserResponseBodyWrapper bodyWrapper)
  56. {
  57. var serializer =
  58. new JavaScriptSerializer();
  59.  
  60. return serializer.Deserialize<TModel>(bodyWrapper.AsString());
  61. }
  62.  
  63. /// <summary>
  64. /// Gets the deserialized representation of the XML in the response body.
  65. /// </summary>
  66. /// <typeparam name="TModel">The type that the XML response body should be deserialized to.</typeparam>
  67. /// <param name="bodyWrapper">An instance of the <see cref="BrowserResponseBodyWrapper"/> that the extension should be invoked on.</param>
  68. /// <value>A <typeparamref name="TModel"/> instance representation of the HTTP response body.</value>
  69. public static TModel DeserializeXml<TModel>(this BrowserResponseBodyWrapper bodyWrapper)
  70. {
  71. var serializer =
  72. new XmlSerializer(typeof(TModel));
  73.  
  74. return (TModel)serializer.Deserialize(bodyWrapper.AsStream());
  75. }
  76. }
  77. }
Add Comment
Please, Sign In to add comment