public void GetSampleResponses_FromActionSamples_BasedOnMediaTypeAndNames()
 {
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     var sample = new TextSample("test");
     sampleGenerator.ActionSamples.Add(new HelpPageSampleKey(new MediaTypeHeaderValue("application/xml"), SampleDirection.Response, "Values", "Get", new[] { "id" }), sample);
     ApiDescription apiDescription = ApiDescriptionHelpers.GetApiDescription(null, "Values", "Get", "id");
     IDictionary<MediaTypeHeaderValue, object> samples = sampleGenerator.GetSampleResponses(apiDescription);
     Assert.NotEmpty(samples);
     object result;
     samples.TryGetValue(new MediaTypeHeaderValue("application/xml"), out result);
     Assert.Same(sample, result);
     samples.TryGetValue(new MediaTypeHeaderValue("application/json"), out result);
     Assert.NotSame(sample, result);
 }
 public void GetSampleResponses_FromActionSamples_WhenTheReturnTypeIsHttpResponseMessage()
 {
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     var sample = new TextSample("test");
     sampleGenerator.ActionSamples.Add(new HelpPageSampleKey(new MediaTypeHeaderValue("plain/text"), SampleDirection.Response, "Values", "Post", new[] { "value" }), sample);
     ApiDescription apiDescription = ApiDescriptionHelpers.GetApiDescription(null, "Values", "Post", "value");
     IDictionary<MediaTypeHeaderValue, object> samples = sampleGenerator.GetSampleResponses(apiDescription);
     Assert.NotEmpty(samples);
     object result;
     samples.TryGetValue(new MediaTypeHeaderValue("plain/text"), out result);
     Assert.Same(sample, result);
     samples.TryGetValue(new MediaTypeHeaderValue("application/json"), out result);
     Assert.Null(result);
 }
 public void GetSampleResponses_FromSampleObjects_AndSettingActualResponseTypes()
 {
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     sampleGenerator.ActualHttpMessageTypes.Add(new HelpPageSampleKey(SampleDirection.Response, "values", "post", new[] { "value" }), typeof(string));
     sampleGenerator.SampleObjects.Add(typeof(string), "sample value");
     ApiDescription apiDescription = ApiDescriptionHelpers.GetApiDescription(null, "values", "post", "value");
     IDictionary<MediaTypeHeaderValue, object> samples = sampleGenerator.GetSampleResponses(apiDescription);
     Assert.NotEmpty(samples);
     foreach (var samplePair in samples)
     {
         Assert.Contains("sample value", ((TextSample)samplePair.Value).Text);
     }
 }
 public void GetSampleResponses_FromSampleObjects()
 {
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     sampleGenerator.SampleObjects.Add(typeof(string), "sample value");
     ApiDescription apiDescription = ApiDescriptionHelpers.GetApiDescription(null, "Values", "Get", "id");
     IDictionary<MediaTypeHeaderValue, object> samples = sampleGenerator.GetSampleResponses(apiDescription);
     Assert.NotEmpty(samples);
     foreach (var samplePair in samples)
     {
         Assert.Contains("sample value", ((TextSample)samplePair.Value).Text);
     }
 }
 public void GetSampleResponses_Empty()
 {
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     ApiDescription apiDescription = ApiDescriptionHelpers.GetApiDescription(null, "Values", "Delete", "id");
     IDictionary<MediaTypeHeaderValue, object> samples = sampleGenerator.GetSampleResponses(apiDescription);
     Assert.Empty(samples);
 }
        private static void GenerateSamples(HelpPageApiModel apiModel, HelpPageSampleGenerator sampleGenerator)
        {
            try
            {
                foreach (var item in sampleGenerator.GetSampleRequests(apiModel.ApiDescription))
                {
                    apiModel.SampleRequests.Add(item.Key, item.Value);
                    LogInvalidSampleAsError(apiModel, item.Value);
                }

                foreach (var item in sampleGenerator.GetSampleResponses(apiModel.ApiDescription))
                {
                    apiModel.SampleResponses.Add(item.Key, item.Value);
                    LogInvalidSampleAsError(apiModel, item.Value);
                }
            }
            catch (Exception e)
            {
                apiModel.ErrorMessages.Add(String.Format(CultureInfo.CurrentCulture,
                    "An exception has occurred while generating the sample. Exception message: {0}",
                    HelpPageSampleGenerator.UnwrapException(e).Message));
            }
        }
        private static HelpPageApiModel GenerateApiModel(ApiDescription apiDescription, HelpPageSampleGenerator sampleGenerator)
        {
            HelpPageApiModel apiModel = new HelpPageApiModel();
            apiModel.ApiDescription = apiDescription;

            try
            {
                foreach (var item in sampleGenerator.GetSampleRequests(apiDescription))
                {
                    apiModel.SampleRequests.Add(item.Key, item.Value);
                    LogInvalidSampleAsError(apiModel, item.Value);
                }

                foreach (var item in sampleGenerator.GetSampleResponses(apiDescription))
                {
                    apiModel.SampleResponses.Add(item.Key, item.Value);
                    LogInvalidSampleAsError(apiModel, item.Value);
                }
            }
            catch (Exception e)
            {
                apiModel.ErrorMessages.Add(String.Format(CultureInfo.CurrentCulture, "An exception has occurred while generating the sample. Exception Message: {0}", e.Message));
            }

            return apiModel;
        }