Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using NUnit.Framework;
- using RestSharp;
- namespace ProjectName.Tests.System
- {
- public class CompositeResponseAssert : IResponseAssert
- {
- private readonly ResponseAssert[] _responses;
- public CompositeResponseAssert(params IRestResponse[] responses)
- {
- if (responses == null) throw new ArgumentNullException("responses");
- if (responses.Length < 1) throw new ArgumentException("At least 1 is needed for assertions to make sense");
- _responses = responses.Select(x=>new ResponseAssert(x)).ToArray();
- }
- private IResponseAssert MakeAssertionForResponses(Action<ResponseAssert> assertion)
- {
- for(int i = 1; i <=_responses.Length;i++)
- {
- try
- {
- assertion(_responses[i - 1]);
- }
- catch(AssertionException assertFail)
- {
- throw new WrappedAssertionException(
- string.Format("Assertion of response no. {0} failed:", i),
- assertFail, string.Empty);
- }
- }
- return this;
- }
- public IResponseAssert HasContentThatContains(string someContent)
- {
- return MakeAssertionForResponses(x => x.HasContentThatContains(someContent));
- }
- public IResponseAssert HasFollowingJsonBody(string expected, bool allowUnexpectedProperties = true, bool allowUndefinedArrayMembers = false)
- {
- return MakeAssertionForResponses(x => x.HasFollowingJsonBody(expected,allowUnexpectedProperties));
- }
- public IResponseAssert Is200Ok()
- {
- return MakeAssertionForResponses(x => x.Is200Ok());
- }
- public IResponseAssert Is400BadRequest()
- {
- return MakeAssertionForResponses(x => x.Is400BadRequest());
- }
- public IResponseAssert Is404NotFoundError()
- {
- return MakeAssertionForResponses(x => x.Is404NotFoundError());
- }
- public IResponseAssert Is405MethodNotAllowedError()
- {
- return MakeAssertionForResponses(x => x.Is405MethodNotAllowedError());
- }
- public IResponseAssert Is422UnprocessableEntity()
- {
- return MakeAssertionForResponses(x => x.Is422UnprocessableEntity());
- }
- public IResponseAssert IsNot4xxError()
- {
- return MakeAssertionForResponses(x => x.IsNot4xxError());
- }
- public IResponseAssert HasNoContentBody()
- {
- return MakeAssertionForResponses(x => x.HasNoContentBody());
- }
- public IResponseAssert HasRawContent(byte[] binary)
- {
- return MakeAssertionForResponses(x => x.HasRawContent(binary));
- }
- public IResponseAssert Is201Created()
- {
- return MakeAssertionForResponses(x => x.Is201Created());
- }
- public IResponseAssert HasExpectedHeader(string headerName, Func<string, bool> expectation)
- {
- return MakeAssertionForResponses(x => x.HasExpectedHeader(headerName, expectation));
- }
- public IResponseAssert HasContentType(string contentType)
- {
- return MakeAssertionForResponses(x => x.HasContentType(contentType));
- }
- public class WrappedAssertionException : AssertionException
- {
- private readonly AssertionException _ae;
- public WrappedAssertionException(string messageHead, AssertionException ae, string messageTail)
- : base(string.Format("{0}\r\n{1}\r\n{2}", messageHead, ae.Message, messageTail))
- {
- if (ae == null) throw new ArgumentNullException("ae");
- _ae = ae;
- }
- public override string StackTrace
- {
- get
- {
- return _ae.StackTrace;
- }
- }
- public override string Source
- {
- get
- {
- return _ae.Source;
- }
- set
- {
- _ae.Source = value;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement