Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using RestSharp;
  5.  
  6. namespace ProjectName.Tests.System
  7. {
  8. public class CompositeResponseAssert : IResponseAssert
  9. {
  10. private readonly ResponseAssert[] _responses;
  11.  
  12. public CompositeResponseAssert(params IRestResponse[] responses)
  13. {
  14. if (responses == null) throw new ArgumentNullException("responses");
  15. if (responses.Length < 1) throw new ArgumentException("At least 1 is needed for assertions to make sense");
  16.  
  17. _responses = responses.Select(x=>new ResponseAssert(x)).ToArray();
  18. }
  19.  
  20. private IResponseAssert MakeAssertionForResponses(Action<ResponseAssert> assertion)
  21. {
  22. for(int i = 1; i <=_responses.Length;i++)
  23. {
  24. try
  25. {
  26. assertion(_responses[i - 1]);
  27. }
  28. catch(AssertionException assertFail)
  29. {
  30. throw new WrappedAssertionException(
  31. string.Format("Assertion of response no. {0} failed:", i),
  32. assertFail, string.Empty);
  33. }
  34. }
  35. return this;
  36. }
  37.  
  38.  
  39. public IResponseAssert HasContentThatContains(string someContent)
  40. {
  41. return MakeAssertionForResponses(x => x.HasContentThatContains(someContent));
  42. }
  43.  
  44. public IResponseAssert HasFollowingJsonBody(string expected, bool allowUnexpectedProperties = true, bool allowUndefinedArrayMembers = false)
  45. {
  46. return MakeAssertionForResponses(x => x.HasFollowingJsonBody(expected,allowUnexpectedProperties));
  47. }
  48.  
  49. public IResponseAssert Is200Ok()
  50. {
  51. return MakeAssertionForResponses(x => x.Is200Ok());
  52. }
  53.  
  54. public IResponseAssert Is400BadRequest()
  55. {
  56. return MakeAssertionForResponses(x => x.Is400BadRequest());
  57. }
  58.  
  59. public IResponseAssert Is404NotFoundError()
  60. {
  61. return MakeAssertionForResponses(x => x.Is404NotFoundError());
  62. }
  63.  
  64. public IResponseAssert Is405MethodNotAllowedError()
  65. {
  66. return MakeAssertionForResponses(x => x.Is405MethodNotAllowedError());
  67. }
  68.  
  69. public IResponseAssert Is422UnprocessableEntity()
  70. {
  71. return MakeAssertionForResponses(x => x.Is422UnprocessableEntity());
  72. }
  73.  
  74. public IResponseAssert IsNot4xxError()
  75. {
  76. return MakeAssertionForResponses(x => x.IsNot4xxError());
  77. }
  78.  
  79. public IResponseAssert HasNoContentBody()
  80. {
  81. return MakeAssertionForResponses(x => x.HasNoContentBody());
  82. }
  83.  
  84. public IResponseAssert HasRawContent(byte[] binary)
  85. {
  86. return MakeAssertionForResponses(x => x.HasRawContent(binary));
  87. }
  88.  
  89. public IResponseAssert Is201Created()
  90. {
  91. return MakeAssertionForResponses(x => x.Is201Created());
  92. }
  93.  
  94. public IResponseAssert HasExpectedHeader(string headerName, Func<string, bool> expectation)
  95. {
  96. return MakeAssertionForResponses(x => x.HasExpectedHeader(headerName, expectation));
  97. }
  98.  
  99. public IResponseAssert HasContentType(string contentType)
  100. {
  101. return MakeAssertionForResponses(x => x.HasContentType(contentType));
  102. }
  103.  
  104. public class WrappedAssertionException : AssertionException
  105. {
  106. private readonly AssertionException _ae;
  107.  
  108. public WrappedAssertionException(string messageHead, AssertionException ae, string messageTail)
  109. : base(string.Format("{0}\r\n{1}\r\n{2}", messageHead, ae.Message, messageTail))
  110. {
  111. if (ae == null) throw new ArgumentNullException("ae");
  112. _ae = ae;
  113. }
  114.  
  115. public override string StackTrace
  116. {
  117. get
  118. {
  119. return _ae.StackTrace;
  120. }
  121. }
  122.  
  123. public override string Source
  124. {
  125. get
  126. {
  127. return _ae.Source;
  128. }
  129.  
  130. set
  131. {
  132. _ae.Source = value;
  133. }
  134. }
  135. }
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement