/// <summary>
 /// Get single request from the calling history. Read the content of the request with specified
 /// <paramref name="deserializer"/> for anonymous type.
 /// </summary>
 /// <typeparam name="T">The anonymous type.</typeparam>
 /// <param name="tracer">The tracing information to verify.</param>
 /// <param name="template">The schema definition for the anonymous type.</param>
 /// <param name="deserializer">The formatter which will be used for deserializing.
 /// The formatter which will be used for deserializing. JSON formatter will be used if not specified.
 /// </param>
 /// <returns>A task reading the content of the request.</returns>
 public static Task <T> SingleOrDefaultRequestContentAsync <T>(
     this IRequestHandlerTracer tracer,
     T template,
     IContentDeserializer deserializer = null)
 {
     return(SingleOrDefaultRequestContentAsync <T>(tracer, deserializer));
 }
 /// <summary>
 /// Verify if the handler was called at least once.
 /// </summary>
 /// <param name="tracer">The tracing information to verify.</param>
 /// <exception cref="VerifyException">
 /// The handler has never been called.
 /// </exception>
 public static void VerifyHasBeenCalled(this IRequestHandlerTracer tracer)
 {
     if (tracer.CallingHistories.Count > 0)
     {
         return;
     }
     throw new VerifyException($"The API \"{tracer.Name}\" is not called, which does not match your expectation.");
 }
 /// <summary>
 /// Get single request from the calling history. Read the content of the request with specified
 /// <paramref name="deserializer"/>.
 /// </summary>
 /// <typeparam name="T">The deserialized type of the request content.</typeparam>
 /// <param name="tracer">The tracing information to verify.</param>
 /// <param name="deserializer">
 /// The formatter which will be used for deserializing. JSON formatter will be used if not specified.
 /// </param>
 /// <returns>
 /// A task reading the content of the request.
 /// </returns>
 public static Task <T> SingleOrDefaultRequestContentAsync <T>(
     this IRequestHandlerTracer tracer,
     IContentDeserializer deserializer = null)
 {
     return(DeserializeContentAsync <T>(
                GetDefaultDeserializerIfNull(deserializer),
                tracer.SingleOrDefaultRequestContent()));
 }
 /// <summary>
 /// Verify if the tracing information of a handler contains a request, which contains the binded paramter
 /// <paramref name="parameter"/>, and the value of the parameter equals to <paramref name="value"/>.
 /// </summary>
 /// <param name="tracer">The tracing information to verify.</param>
 /// <param name="parameter">The name of the parameter.</param>
 /// <param name="value">The value to compare with actual binded parameter value.</param>
 /// <exception cref="VerifyException">
 /// Either the name of the parameter cannot be found in tracing information, or the value does not equal to
 /// <paramref name="value"/>.
 /// </exception>
 public static void VerifyBindedParameter(
     this IRequestHandlerTracer tracer,
     string parameter,
     string value)
 {
     VerifyBindedParameter(
         tracer,
         parameter,
         value == null ? (Func <object, bool>)(v => v == null) : value.Equals);
 }
        /// <summary>
        /// Verify if the handler was never been called.
        /// </summary>
        /// <param name="tracer">The tracing information to verify.</param>
        /// <exception cref="VerifyException">
        /// The handler has been called.
        /// </exception>
        public static void VerifyNotCalled(this IRequestHandlerTracer tracer)
        {
            int actualCalledCount = tracer.CallingHistories.Count;

            if (actualCalledCount == 0)
            {
                return;
            }
            throw new VerifyException($"The API \"{tracer.Name}\" has been called for {actualCalledCount} time(s). But your expectation is not being called.");
        }
        /// <summary>
        /// Verify if the handler was called at specified times.
        /// </summary>
        /// <param name="tracer">The tracing information to verify.</param>
        /// <param name="times">The desired times that the handler was called.</param>
        /// <exception cref="VerifyException">
        /// The handler calling times does not equals to <paramref name="times"/>.
        /// </exception>
        public static void VerifyHasBeenCalled(this IRequestHandlerTracer tracer, int times)
        {
            int actualCalledCount = tracer.CallingHistories.Count;

            if (actualCalledCount == times)
            {
                return;
            }
            throw new VerifyException(
                      $"The API has been called for {actualCalledCount} time(s) rather than {times} time(s) for API named \"{tracer.Name}\".");
        }
Пример #7
0
        public void should_be_able_to_verify_not_called_from_named_handler_tracer()
        {
            var server = new MockHttpServer();

            server.AddHandler(new DelegatedRequestHandler(
                                  _ => true,
                                  (r, p, c) => HttpStatusCode.OK.AsResponse(),
                                  "handlerName"));

            IRequestHandlerTracer handlerTracer = server["handlerName"];

            handlerTracer.VerifyNotCalled();
            Assert.Throws <VerifyException>(() => handlerTracer.VerifyHasBeenCalled());
        }
Пример #8
0
        public void should_be_able_to_get_named_handler_tracer()
        {
            var server = new MockHttpServer();

            server.AddHandler(new DelegatedRequestHandler(
                                  _ => true,
                                  (r, p, c) => HttpStatusCode.OK.AsResponse(),
                                  "handlerName"));

            IRequestHandlerTracer handlerTracer = server["handlerName"];

            Assert.NotNull(handlerTracer);
            Assert.Throws <KeyNotFoundException>(() => server["notExist"]);
        }
        /// <summary>
        /// Verify if the tracing information of a handler contains a request, which contains the binded paramter
        /// <paramref name="parameter"/>, and the value of the parameter satisfies <paramref name="verifyFunc"/>.
        /// </summary>
        /// <param name="tracer">The tracing information to verify.</param>
        /// <param name="parameter">The name of the parameter.</param>
        /// <param name="verifyFunc">The delegate to examine the value of the parameter.</param>
        /// <exception cref="VerifyException">
        /// Either the name of the parameter cannot be found in tracing information, or the value does not meet
        /// the requirement of <paramref name="verifyFunc"/>.
        /// </exception>
        public static void VerifyBindedParameter(
            this IRequestHandlerTracer tracer,
            string parameter,
            Func <object, bool> verifyFunc)
        {
            bool matchParameter = tracer.CallingHistories.Any(
                c => c.Parameters != null &&
                c.Parameters.ContainsKey(parameter) &&
                verifyFunc(c.Parameters[parameter]));

            if (matchParameter)
            {
                return;
            }
            throw new VerifyException(
                      $"Either no parameter has name \"{parameter}\" or the parameter value did not pass the verify process for API named \"{tracer.Name}\"");
        }
Пример #10
0
        public async Task should_get_request_from_calling_history_of_named_handler_tracer()
        {
            var server = new MockHttpServer();

            server.AddHandler(new DelegatedRequestHandler(
                                  _ => true,
                                  (r, p, c) => HttpStatusCode.OK.AsResponse(),
                                  "handlerName"));

            IRequestHandlerTracer handlerTracer = server["handlerName"];

            HttpClient httpClient = CreateClient(server);
            await httpClient.GetAsync("http://uri.that.matches/");

            CallingHistoryContext callingHistoryContext = handlerTracer.CallingHistories.Single();

            Assert.Equal("http://uri.that.matches/", callingHistoryContext.Request.RequestUri.AbsoluteUri);
        }
Пример #11
0
        public async Task should_be_able_to_verify_called_from_named_handler_tracer()
        {
            var server = new MockHttpServer();

            server.AddHandler(new DelegatedRequestHandler(
                                  _ => true,
                                  (r, p, c) => HttpStatusCode.OK.AsResponse(),
                                  "handlerName"));

            IRequestHandlerTracer handlerTracer = server["handlerName"];

            HttpClient httpClient = CreateClient(server);
            await httpClient.GetAsync("http://uri.that.matches");

            handlerTracer.VerifyHasBeenCalled();
            handlerTracer.VerifyHasBeenCalled(1);
            Assert.Throws <VerifyException>(() => handlerTracer.VerifyNotCalled());
        }
Пример #12
0
        public async Task should_verify_api_called_times()
        {
            var httpServer = new MockHttpServer();

            httpServer
            .WithService("http://www.base.com")
            .Api("api1", "GET", HttpStatusCode.OK, "api1")
            .Api("api2", "GET", HttpStatusCode.OK, "api2");

            HttpClient client = CreateClient(httpServer);

            await client.GetAsync("http://www.base.com/api1");

            await client.GetAsync("http://www.base.com/api1");

            IRequestHandlerTracer tracer = httpServer["api1"];

            tracer.VerifyHasBeenCalled(2);
            Assert.Throws <VerifyException>(() => tracer.VerifyHasBeenCalled(3));
        }
Пример #13
0
        public async Task should_verify_binded_parameters_from_named_handler_tracer()
        {
            var server = new MockHttpServer();

            server.AddHandler(new DelegatedRequestHandler(
                                  _ => new MatchingResult(true, new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("p1", "v1"),
                new KeyValuePair <string, object>("p2", "v2")
            }),
                                  (r, p, c) => HttpStatusCode.OK.AsResponse(),
                                  "handlerName"));

            IRequestHandlerTracer handlerTracer = server["handlerName"];

            HttpClient httpClient = CreateClient(server);
            await httpClient.GetAsync("http://uri.that.matches/");

            handlerTracer.VerifyBindedParameter("p1", "v1");
            Assert.Throws <VerifyException>(() => handlerTracer.VerifyBindedParameter("p1", "v2"));
            Assert.Throws <VerifyException>(() => handlerTracer.VerifyBindedParameter("p15", "v2"));
            Assert.Throws <VerifyException>(() => handlerTracer.VerifyBindedParameter("p1", (string)null));
        }
 static HttpContent SingleOrDefaultRequestContent(this IRequestHandlerTracer tracer)
 {
     return(tracer.CallingHistories.SingleOrDefault()?.Request?.Content);
 }