public void GetSampleRequests_FromActionSamples_BasedOnMediaTypeAndType()
 {
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     var sample = new TextSample("test");
     sampleGenerator.ActionSamples.Add(new HelpPageSampleKey(new MediaTypeHeaderValue("application/json"), typeof(Tuple<int, string>)), sample);
     ApiDescription apiDescription = ApiDescriptionHelpers.GetApiDescription(null, "Values", "Patch", "valuePair");
     IDictionary<MediaTypeHeaderValue, object> samples = sampleGenerator.GetSampleRequests(apiDescription);
     Assert.NotEmpty(samples);
     object result;
     samples.TryGetValue(new MediaTypeHeaderValue("application/json"), out result);
     Assert.Same(sample, result);
     samples.TryGetValue(new MediaTypeHeaderValue("application/xml"), out result);
     Assert.NotSame(sample, result);
 }
        public virtual object WriteSampleObjectUsingFormatter(MediaTypeFormatter formatter, object value, Type type, MediaTypeHeaderValue mediaType)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException("formatter");
            }
            if (mediaType == null)
            {
                throw new ArgumentNullException("mediaType");
            }

            object sample = String.Empty;
            MemoryStream ms = null;
            HttpContent content = null;
            try
            {
                if (formatter.CanWriteType(type))
                {
                    ms = new MemoryStream();
                    content = new ObjectContent(type, value, formatter, mediaType);
                    formatter.WriteToStreamAsync(type, value, ms, content, null).Wait();
                    ms.Position = 0;
                    StreamReader reader = new StreamReader(ms);
                    string serializedSampleString = reader.ReadToEnd();
                    if (mediaType.MediaType.ToUpperInvariant().Contains("XML"))
                    {
                        serializedSampleString = TryFormatXml(serializedSampleString);
                    }
                    else if (mediaType.MediaType.ToUpperInvariant().Contains("JSON"))
                    {
                        serializedSampleString = TryFormatJson(serializedSampleString);
                    }

                    sample = new TextSample(serializedSampleString);
                }
                else
                {
                    sample = new InvalidSample(String.Format(
                        CultureInfo.CurrentCulture,
                        "Failed to generate the sample for media type '{0}'. Cannot use formatter '{1}' to write type '{2}'.",
                        mediaType,
                        formatter.GetType().Name,
                        type.Name));
                }
            }
            catch (Exception e)
            {
                sample = new InvalidSample(String.Format(
                    CultureInfo.CurrentCulture,
                    "An exception has occurred while using the formatter '{0}' to generate sample for media type '{1}'. Exception message: {2}",
                    formatter.GetType().Name,
                    mediaType.MediaType,
                    UnwrapException(e).Message));
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                }
                if (content != null)
                {
                    content.Dispose();
                }
            }

            return sample;
        }
 public void GetSampleRequests_FromActionSamples_BasedOnMediaTypeAndNames()
 {
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     var sample = new TextSample("test");
     sampleGenerator.ActionSamples.Add(new HelpPageSampleKey(new MediaTypeHeaderValue("application/xml"), SampleDirection.Request, "Values", "Put", new[] { "valuePairCollection" }), sample);
     ApiDescription apiDescription = ApiDescriptionHelpers.GetApiDescription(null, "Values", "Put", "valuePairCollection");
     IDictionary<MediaTypeHeaderValue, object> samples = sampleGenerator.GetSampleRequests(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 SetSampleResponse_WithParameters()
 {
     HttpConfiguration config = new HttpConfiguration();
     TextSample sample = new TextSample("test");
     config.SetSampleResponse(sample, new MediaTypeHeaderValue("application/json"), "values", "post", "id", "name");
     object sampleGeneratorObj;
     config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
     HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
     Assert.NotEmpty(sampleGenerator.ActionSamples);
     var actionSample = sampleGenerator.ActionSamples.First();
     Assert.Equal("values", actionSample.Key.ControllerName);
     Assert.Equal("post", actionSample.Key.ActionName);
     Assert.Equal(new MediaTypeHeaderValue("application/json"), actionSample.Key.MediaType);
     Assert.Equal(SampleDirection.Response, actionSample.Key.SampleDirection);
     Assert.Equal(2, actionSample.Key.ParameterNames.Count);
     Assert.True(actionSample.Key.ParameterNames.SetEquals(new[] { "id", "name" }));
     Assert.Same(sample, actionSample.Value);
 }
 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 SetSampleRequest()
 {
     HttpConfiguration config = new HttpConfiguration();
     TextSample sample = new TextSample("test");
     config.SetSampleRequest(sample, new MediaTypeHeaderValue("application/xml"), "values", "get");
     object sampleGeneratorObj;
     config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
     HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
     Assert.NotEmpty(sampleGenerator.ActionSamples);
     var actionSample = sampleGenerator.ActionSamples.First();
     Assert.Equal("values", actionSample.Key.ControllerName);
     Assert.Equal("get", actionSample.Key.ActionName);
     Assert.Equal(new MediaTypeHeaderValue("application/xml"), actionSample.Key.MediaType);
     Assert.Equal(SampleDirection.Request, actionSample.Key.SampleDirection);
     Assert.NotEmpty(actionSample.Key.ParameterNames);
     Assert.Equal("*", actionSample.Key.ParameterNames.First());
     Assert.Same(sample, actionSample.Value);
 }
 public void GetHashCode_ReturnsTextHashCode()
 {
     TextSample sample = new TextSample("some text");
     Assert.Equal("some text".GetHashCode(), sample.GetHashCode());
 }
 public void ToString_ReturnsText()
 {
     TextSample sample = new TextSample("some text");
     Assert.Equal("some text", sample.ToString());
 }
 public void Equals_ReturnsTrue()
 {
     TextSample sample = new TextSample("some text");
     Assert.Equal(new TextSample("some text"), sample);
 }
 public void Constructor()
 {
     TextSample sample = new TextSample("some text");
     Assert.Equal("some text", sample.Text);
 }