Ejemplo n.º 1
0
        private void HttpPostPublishVerificationResults(string uri, PactVerificationResult verificationResult)
        {
            ApplyAuthorizationHeadersIfRequired();
            var verificationResultJson = JsonConvert.SerializeObject(verificationResult);

            using (var content = new StringContent(verificationResultJson, Encoding.UTF8, "application/json"))
            {
                var response = _httpClient.PostAsync(uri, content).GetAwaiter().GetResult();
                if (!response.IsSuccessStatusCode)
                {
                    throw new InvalidOperationException(string.Format(
                                                            "Unable to publish results - {0} (status code: {1})", response.ReasonPhrase,
                                                            (int)response.StatusCode));
                }
            }
        }
Ejemplo n.º 2
0
        public void Verify(string description = null, string providerState = null)
        {
            if (String.IsNullOrEmpty(PactFileUri))
            {
                throw new InvalidOperationException(
                          "PactFileUri has not been set, please supply a uri using the PactUri method.");
            }

            MessagePactFile pactFile;

            try
            {
                string pactFileJson;

                if (IsWebUri(PactFileUri))
                {
                    pactFileJson = HttpGetPactFile();
                }
                else
                {
                    pactFileJson = File.ReadAllText(PactFileUri);
                }

                pactFile = JsonConvert.DeserializeObject <MessagePactFile>(pactFileJson);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                          String.Format("Json Pact file could not be retrieved using uri \'{0}\'.", PactFileUri), ex);
            }

            //Filter interactions
            if (description != null)
            {
                pactFile.Messages = pactFile.Messages.Where(x => x.Description.Equals(description));
            }

            if (providerState != null)
            {
                pactFile.Messages = pactFile.Messages.Where(x => x.ProviderState.Equals(providerState));
            }

            if ((description != null || providerState != null) &&
                (pactFile.Messages == null || !pactFile.Messages.Any()))
            {
                throw new ArgumentException(
                          "The specified description and/or providerState filter yielded no interactions.");
            }

            var loggerName = LogProvider.CurrentLogProvider.AddLogger(_pactVerifierConfig.LogDir,
                                                                      ProviderName.ToLowerSnakeCase(), "{0}_verifier.log");

            _pactVerifierConfig.LoggerName = loggerName;

            var verificationResult = new PactVerificationResult
            {
                ProviderApplicationVersion = _pactVerifierConfig.ProviderVersion
            };

            try
            {
                var validator = new MessageProviderValidator(new Reporter(_pactVerifierConfig), _pactVerifierConfig);
                validator.Validate(pactFile, ProviderStates);

                verificationResult.Success = true;
            }
            finally
            {
                if (_pactVerifierConfig.PublishVerificationResults)
                {
                    HttpPostPublishVerificationResults(
                        pactFile.Links.PublishVerificationResults.Href, verificationResult);
                }

                LogProvider.CurrentLogProvider.RemoveLogger(_pactVerifierConfig.LoggerName);
            }
        }