Esempio n. 1
0
        public void InvalidUrl()
        {
            /* Arrange */
            var testYamlSequence = new YamlScript
            {
                sequence_items = new List <SequenceItem> {
                    new SequenceItem
                    {
                        command = "invalid_url",
                        send    = new UrlRequest
                        {
                            header = new KeyValueList {
                                new KeyValuePair <string, string>("Accept", "application/json")
                            },
                            http_method = "GET",
                            url         = "http://doesnt-even-exist-7djemd/totally-invalid-url"
                        }
                    }
                }
            };
            var testOptions = new Options {
                YamlDirect = testYamlSequence
            };

            /* Act */
            var provider = new HttpSequencer.HttpSequencer();
            var result   = provider.RunSequenceAsync(testOptions).Result;

            /* Assert */
            Assert.Equal(1, result);
        }
        private static HttpClient MakeClientWithHeaders(Options o, YamlScript yaml, SequenceItem entry = null)
        {
            var client = new HttpClient();

            const int defaultClientTimeoutSeconds = 90;

            client.Timeout = TimeSpan.FromSeconds(yaml.client_timeout_seconds ?? defaultClientTimeoutSeconds);

            var scribanModel = new { yaml.run_id, command_args = o, model = new { } };

            client.DefaultRequestHeaders.Accept.Clear();
            if (entry?.send?.header != null)
            {
                /* UN TESTED  - for POST*/

                //Add customer headers
                foreach (var addHeader in entry.send.header)
                {
                    if (addHeader.Key.Equals("accept", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var vals = ScribanUtil.ScribanParse(addHeader.Value, scribanModel);
                        foreach (var val in vals.Split(',').Select(s => s.Trim()))
                        {
                            var queryParts = val.Split(';');
                            if (queryParts.Length > 1)
                            {
                                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(queryParts[0]));
                                foreach (var queryPart in queryParts.Skip(1))
                                {
                                    var keyVal = queryPart.Split('=');
                                    client.DefaultRequestHeaders.Add(keyVal[0], keyVal[1]);
                                }
                            }
                            else
                            {
                                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(val));
                            }
                        }
                    }
                    else
                    {
                        client.DefaultRequestHeaders.Add(addHeader.Key, ScribanUtil.ScribanParse(addHeader.Value, scribanModel));
                    }
                }
            }

            var contenType = entry.send.content_type ?? "text/plain";

            if (client.DefaultRequestHeaders.Accept.Count == 0)
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contenType));
            }

            return(client);
        }
Esempio n. 3
0
        public void InvalidUrlForSecondSequenceItem()
        {
            int testPort = GetAvailablePort();

            using (var ConsumeTestYamlPact = new ConsumeHttpSequencerPact("FirstConsumer", testPort))
            {
                ConsumeTestYamlPact.MockProviderService.ClearInteractions();


                /* ๐“๐“ป๐“ป๐“ช๐“ท๐“ฐ๐“ฎ */
                const string expectedMoreDetailString = nameof(expectedMoreDetailString);

                ConsumeTestYamlPact.MockProviderService
                .Given("There is an active endpoint that provides a list of ids")
                .UponReceiving("A GET request to retrieve the list")
                .With(new ProviderServiceRequest
                {
                    Method  = HttpVerb.Get,
                    Path    = "/first",
                    Headers = new Dictionary <string, object> {
                        { "Accept", "application/json" }
                    },
                })
                .WillRespondWith(new ProviderServiceResponse
                {
                    Status  = 200,
                    Headers = new Dictionary <string, object> {
                        { "Content-Type", "application/json; charset=utf-8" }
                    },
                    Body = new Dictionary <string, object> {
                        { "Id", "00000001" }
                    }
                });

                var testYamlSequence = new YamlScript
                {
                    sequence_items = new List <SequenceItem> {
                        /* First */
                        new SequenceItem
                        {
                            command = "one-of-two-url-ok",
                            send    = new UrlRequest
                            {
                                header = new KeyValueList {
                                    new KeyValuePair <string, string>("Accept", "application/json")
                                },
                                http_method = "GET",
                                url         = $"http://localhost:{testPort}/first"
                            }
                        },
                        /* Second */
                        new SequenceItem
                        {
                            command = "two-of-two-url-doesnt-exist",
                            send    = new UrlRequest
                            {
                                header = new KeyValueList {
                                    new KeyValuePair <string, string>("Accept", "application/json")
                                },
                                http_method = "GET",
                                url         = "http://doesnt-even-exist-7djemd/totally-invalid-url/{{model.Id}}"
                            }
                        }
                    }
                };

                var testOptions = new Options {
                    YamlDirect = testYamlSequence
                };

                /* ๐“๐“ฌ๐“ฝ */
                var provider = new HttpSequencer.HttpSequencer();
                var result   = provider.RunSequenceAsync(testOptions).Result;

                /* ๐“๐“ผ๐“ผ๐“ฎ๐“ป๐“ฝ */
                Assert.Equal(1, result);
                ConsumeTestYamlPact.MockProviderService.VerifyInteractions();
            }
        }
Esempio n. 4
0
        public void OneSequence()
        {
            /*     _
             *    / \     _ __   _ __    __ _   _ __     __ _    ___
             *   / _ \   | '__| | '__|  / _` | | '_ \   / _` |  / _ \
             *  / ___ \  | |    | |    | (_| | | | | | | (_| | |  __/
             * /_/   \_\ |_|    |_|     \__,_| |_| |_|  \__, |  \___|
             |___/           */

            ConsumeTestYamlPact.MockProviderService
            .Given("There is an active endpoint")
            .UponReceiving("A GET request to the endpoint")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/",
                Headers = new Dictionary <string, object> {
                    { "Accept", "text/plain" }
                },
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object> {
                    { "Content-Type", "text/plain" }
                },
                Body = ""
            });

            /*
             * One sequence, the simplest GET with no params
             */
            var testYamlSequence = new YamlScript
            {
                sequence_items = new List <SequenceItem> {
                    new SequenceItem
                    {
                        command = "first-and-only",
                        send    = new UrlRequest
                        {
                            http_method = "GET",
                            url         = $"http://localhost:{Port}"
                        }
                    }
                }
            };

            var testOptions = new Options {
                YamlDirect = testYamlSequence
            };


            /*     _             _
             *    / \      ___  | |_
             *   / _ \    / __| | __|
             *  / ___ \  | (__  | |_
             * /_/   \_\  \___|  \__|
             */

            var consumer = new HttpSequencer.HttpSequencer();

            var result = consumer.RunSequenceAsync(testOptions).Result;



            /*     _                                _
             *    / \     ___   ___    ___   _ __  | |_
             *   / _ \   / __| / __|  / _ \ | '__| | __|
             *  / ___ \  \__ \ \__ \ |  __/ | |    | |_
             * /_/   \_\ |___/ |___/  \___| |_|     \__|
             */

            Assert.Equal(0, result);

            ConsumeTestYamlPact.MockProviderService.VerifyInteractions();
        }
Esempio n. 5
0
        public void ThreeSequences_CheckFails()
        {
            /* ๐“๐“ป๐“ป๐“ช๐“ท๐“ฐ๐“ฎ */

            const string expectedMoreDetailString = nameof(expectedMoreDetailString);

            ConsumeTestYamlPact.MockProviderService
            .Given("There is an active endpoint that provides a list of ids")
            .UponReceiving("A GET request to retrieve the list")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/first",
                Headers = new Dictionary <string, object> {
                    { "Accept", "application/json" }
                },
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object> {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new List <object> {
                    new { Id = "00000001" }
                }
            });

            ConsumeTestYamlPact.MockProviderService
            .Given("Given there is more detail for item id 00000001")
            .UponReceiving("A GET request for more detail for item id 00000001")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/second/00000001",
                Headers = new Dictionary <string, object> {
                    { "Accept", "application/json" }
                },
                Body = { }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object> {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new { detail = expectedMoreDetailString }
            });

            var testYamlSequence = new YamlScript
            {
                sequence_items = new List <SequenceItem> {
                    /* First */
                    new SequenceItem
                    {
                        command = "one-of-three-check-at-end-fails",
                        send    = new UrlRequest
                        {
                            header = new KeyValueList {
                                new KeyValuePair <string, string>("Accept", "application/json")
                            },
                            http_method = "GET",
                            url         = $"http://localhost:{Port}/first"
                        }
                    },
                    /* Second */
                    new SequenceItem
                    {
                        command        = "two-of-three-check-at-end-fails",
                        is_model_array = true,
                        send           = new UrlRequest
                        {
                            header = new KeyValueList {
                                new KeyValuePair <string, string>("Accept", "application/json")
                            },
                            http_method = "GET",
                            url         = $"http://localhost:{Port}/second/" + "{{model.Id}}"
                        }
                    },
                    /* Check */
                    new SequenceItem
                    {
                        command = "three-of-three-check-at-end-fails",
                        check   = new SequenceCheck
                        {
                            pass_template         = "{{if model.detail=='it will never be this'}}true{{else}}false{{end}}",
                            fail_message_template = "Model detail failed with value: {{model.detail}}"
                        }
                    }
                }
            };

            var testOptions = new Options {
                YamlDirect = testYamlSequence
            };


            /* ๐“๐“ฌ๐“ฝ */

            var provider = new HttpSequencer.HttpSequencer();
            var result   = provider.RunSequenceAsync(testOptions).Result;


            /* ๐“๐“ผ๐“ผ๐“ฎ๐“ป๐“ฝ */

            Assert.Equal(1, result);

            ConsumeTestYamlPact.MockProviderService.VerifyInteractions();
        }
Esempio n. 6
0
        public void TwoSequences()
        {
            /* ๐“๐“ป๐“ป๐“ช๐“ท๐“ฐ๐“ฎ */

            const string expectedMoreDetailString = nameof(expectedMoreDetailString);

            ConsumeTestYamlPact.MockProviderService
            .Given("There is an active endpoint that provides a list of ids")
            .UponReceiving("A GET request to retrieve the list")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/first",
                Headers = new Dictionary <string, object> {
                    { "Accept", "application/json" }
                },
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object> {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new List <object> {
                    new { Id = "00000001" }
                }
            });

            ConsumeTestYamlPact.MockProviderService
            .Given("Given there is more detail for item id 00000001")
            .UponReceiving("A GET request for more detail for item id 00000001")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/second/00000001",
                Headers = new Dictionary <string, object> {
                    { "Accept", "application/json" }
                },
                Body = { }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object> {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new { detail = expectedMoreDetailString }
            });

            var testYamlSequence = new YamlScript
            {
                sequence_items = new List <SequenceItem> {
                    /* First
                     * Get a list of ids, which will be a list of one id.
                     * For each of these (that is, for the list of one) use that id in the next request */
                    new SequenceItem
                    {
                        command = "one-of-two",
                        send    = new UrlRequest
                        {
                            header = new KeyValueList {
                                new KeyValuePair <string, string>("Accept", "application/json")
                            },
                            http_method = "GET",
                            url         = $"http://localhost:{Port}/first"
                        }
                    },
                    /* Second */
                    new SequenceItem
                    {
                        command        = "two-of-two",
                        is_model_array = true,
                        send           = new UrlRequest
                        {
                            header = new KeyValueList {
                                new KeyValuePair <string, string>("Accept", "application/json")
                            },
                            http_method = "GET",
                            url         = $"http://localhost:{Port}/second/" + "{{model.Id}}"
                        }
                    }
                }
            };

            var testOptions = new Options {
                YamlDirect = testYamlSequence
            };


            /* ๐“๐“ฌ๐“ฝ */

            var consumer = new HttpSequencer.HttpSequencer();
            var result   = consumer.RunSequenceAsync(testOptions).Result;


            /* ๐“๐“ผ๐“ผ๐“ฎ๐“ป๐“ฝ */

            Assert.Equal(0, result);

            ConsumeTestYamlPact.MockProviderService.VerifyInteractions();
        }