コード例 #1
0
        public async Task ExistsPredicateExample()
        {
            var imposter = _client.CreateHttpImposter(4550, "ExistsPredicateExample");

            // First stub
            var predicateFields = new HttpPredicateFields
            {
                RequestBody = new
                {
                    Message = true
                }
            };

            imposter.AddStub().On(new ExistsPredicate <HttpPredicateFields>(predicateFields))
            .ReturnsBody(HttpStatusCode.OK, "Success");

            // Second stub
            predicateFields = new HttpPredicateFields
            {
                RequestBody = new
                {
                    Message = false
                }
            };

            imposter.AddStub().On(new ExistsPredicate <HttpPredicateFields>(predicateFields))
            .ReturnsBody(HttpStatusCode.BadRequest, "You need to add a message parameter");

            await _client.SubmitAsync(imposter);
        }
コード例 #2
0
        /// <summary>
        /// Adds a predicate to the stub that will match when the request method equals the specified method.
        /// </summary>
        /// <param name="method">The method to match on</param>
        /// <returns>The stub that the predicate was added to</returns>
        public HttpStub OnMethodEquals(Method method)
        {
            var fields = new HttpPredicateFields
            {
                Method = method
            };

            var predicate = new EqualsPredicate <HttpPredicateFields>(fields);

            return(On(predicate));
        }
コード例 #3
0
        /// <summary>
        /// Adds a predicate to the stub that will match when the request path equals the specified path.
        /// </summary>
        /// <param name="path">The path to match on</param>
        /// <returns>The stub that the predicate was added to</returns>
        public HttpStub OnPathEquals(string path)
        {
            var fields = new HttpPredicateFields
            {
                Path = path
            };

            var predicate = new EqualsPredicate <HttpPredicateFields>(fields);

            return(On(predicate));
        }
コード例 #4
0
        /// <summary>
        /// Adds a predicate to the stub that will match when the request path equals the specified path
        /// and the method of the request matches the specified HTTP method.
        /// </summary>
        /// <param name="path">The path to match on</param>
        /// <param name="method">The method to match on</param>
        /// <returns>The stub that the predicate was added to</returns>
        public HttpStub OnPathAndMethodEqual(string path, Method method)
        {
            var fields = new HttpPredicateFields
            {
                Path   = path,
                Method = method
            };

            var predicate = new EqualsPredicate <HttpPredicateFields>(fields);

            return(On(predicate));
        }
コード例 #5
0
        public async Task DeepEqualsPredicateExample()
        {
            var imposter = _client.CreateHttpImposter(4556, "DeepEqualsPredicateExample");

            // First stub
            var predicateFields = new HttpPredicateFields
            {
                QueryParameters = new Dictionary <string, object>()
            };

            var responseFields = new HttpResponseFields
            {
                ResponseObject = "first"
            };

            imposter.AddStub().On(new DeepEqualsPredicate <HttpPredicateFields>(predicateFields))
            .Returns(new IsResponse <HttpResponseFields>(responseFields));

            // Second stub
            predicateFields = new HttpPredicateFields
            {
                QueryParameters = new Dictionary <string, object> {
                    { "first", "1" }
                }
            };

            responseFields = new HttpResponseFields
            {
                ResponseObject = "second"
            };

            imposter.AddStub().On(new DeepEqualsPredicate <HttpPredicateFields>(predicateFields))
            .Returns(new IsResponse <HttpResponseFields>(responseFields));

            // Third stub
            predicateFields = new HttpPredicateFields
            {
                QueryParameters = new Dictionary <string, object> {
                    { "first", "1" }, { "second", "2" }
                }
            };

            responseFields = new HttpResponseFields
            {
                ResponseObject = "third"
            };

            imposter.AddStub().On(new DeepEqualsPredicate <HttpPredicateFields>(predicateFields))
            .Returns(new IsResponse <HttpResponseFields>(responseFields));

            await _client.SubmitAsync(imposter);
        }
コード例 #6
0
        public async Task EqualsPredicateExample()
        {
            var imposter = _client.CreateHttpImposter(4545, "EqualsPredicateExample");

            // First stub
            var bodyPredicateFields = new HttpPredicateFields
            {
                RequestBody = "hello, world"
            };
            var bodyPredicate = new EqualsPredicate <HttpPredicateFields>(bodyPredicateFields, true, "$!", null);

            var complexPredicateFields = new HttpPredicateFields
            {
                Method          = Method.Post,
                Path            = "/test",
                QueryParameters = new Dictionary <string, object> {
                    { "first", "1" }, { "second", "2" }
                },
                Headers = new Dictionary <string, object> {
                    { "Accept", "text/plain" }
                }
            };

            var complexPredicate = new EqualsPredicate <HttpPredicateFields>(complexPredicateFields);

            imposter.AddStub().On(complexPredicate).On(bodyPredicate).ReturnsStatus(HttpStatusCode.BadRequest);

            // Second stub
            var fields = new HttpPredicateFields
            {
                Headers = new Dictionary <string, object> {
                    { "Accept", "application/xml" }
                }
            };

            imposter.AddStub().On(new EqualsPredicate <HttpPredicateFields>(fields)).ReturnsStatus(HttpStatusCode.NotAcceptable);

            // Third stub
            imposter.AddStub().OnMethodEquals(Method.Put).ReturnsStatus((HttpStatusCode)405);

            // Fourth stub
            imposter.AddStub().OnMethodEquals(Method.Put).ReturnsStatus((HttpStatusCode)500);

            await _client.SubmitAsync(imposter);
        }
コード例 #7
0
        public async Task JsonExample()
        {
            var imposter = _client.CreateHttpImposter(4545, "JsonExample");

            var caseSensitiveFields = new HttpPredicateFields {
                RequestBody = new Book {
                    Title = "Harry Potter"
                }
            };
            var caseSensitivePredicate = new EqualsPredicate <HttpPredicateFields>(caseSensitiveFields, true, null, null);

            var exceptFields = new HttpPredicateFields {
                RequestBody = new Book {
                    Title = "POTTER"
                }
            };
            var exceptPredicate = new EqualsPredicate <HttpPredicateFields>(exceptFields, false, "HARRY ", null);

            var matchesFields = new HttpPredicateFields {
                RequestBody = new Book {
                    Title = "^Harry"
                }
            };
            var matchesPredicate = new MatchesPredicate <HttpPredicateFields>(matchesFields);

            // Exists examples not provided since MbDotNet does not yet support checking specific object keys

            imposter.AddStub()
            .On(caseSensitivePredicate)
            .On(exceptPredicate)
            .On(matchesPredicate)
            .ReturnsJson(HttpStatusCode.OK, new BookResponse {
                Code = "SUCCESS", Author = "J.K. Rowling"
            });

            await _client.SubmitAsync(imposter);
        }
        public static void AddRecordedCommand(VirtSqlRpcModel model)
        {
            _recordedCommands.AddOrUpdate(model.StoredProcName, s => new List <VirtSqlRpcModel> {
                model
            }, (s, list) =>
            {
                list.Add(model);
                return(list);
            });

            var mountebankClient = new MountebankClient();

            mountebankClient.DeleteImposter(1234);

            var imposter = mountebankClient.CreateHttpImposter(1234, "test");

            foreach (var virtRecordedCommand in _recordedCommands)
            {
                var stub = imposter.AddStub();

                var predicateFields = new HttpPredicateFields
                {
                    Path = virtRecordedCommand.Key,
                };

                var containsPredicate = new ContainsPredicate <HttpPredicateFields>(predicateFields);

                foreach (var response in virtRecordedCommand.Value)
                {
                    stub
                    .On(containsPredicate)
                    .ReturnsJson(HttpStatusCode.OK, response);
                }
            }

            mountebankClient.Submit(imposter);
        }