REST client API: Polishing (SPRNET-1345)
This commit is contained in:
@@ -102,7 +102,6 @@ namespace Spring.Http.Client
|
||||
/// </summary>
|
||||
public Action<Stream> Body
|
||||
{
|
||||
get { return this.body; }
|
||||
set { this.body = value; }
|
||||
}
|
||||
|
||||
|
||||
@@ -20,55 +20,150 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Spring.Util;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="IHttpMessageConverter"/> that can write files.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A mapping between file extension and mime types is used to determine the Content-Type of written files.
|
||||
/// If no Content-Type is available, 'application/octet-stream' is used.
|
||||
/// </remarks>
|
||||
/// <author>Bruno Baia</author>
|
||||
public class FileInfoHttpMessageConverter : AbstractHttpMessageConverter
|
||||
public class FileInfoHttpMessageConverter : IHttpMessageConverter
|
||||
{
|
||||
// Pre-defined mapping between file extension and mime types
|
||||
private static IDictionary<string, string> defaultMimeMapping;
|
||||
|
||||
private IList<MediaType> _supportedMediaTypes;
|
||||
private IDictionary<string, string> _mimeMapping;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="ByteArrayHttpMessageConverter"/>
|
||||
/// with 'text/plain; charset=ISO-8859-1', and '*/*' media types.
|
||||
/// Gets or sets the mapping between file extension and mime types.
|
||||
/// </summary>
|
||||
public FileInfoHttpMessageConverter() :
|
||||
base(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL)
|
||||
public IDictionary<string, string> MimeMapping
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._mimeMapping == null)
|
||||
{
|
||||
this._mimeMapping = new Dictionary<string, string>(defaultMimeMapping);
|
||||
}
|
||||
return _mimeMapping;
|
||||
}
|
||||
set { _mimeMapping = value; }
|
||||
}
|
||||
|
||||
static FileInfoHttpMessageConverter()
|
||||
{
|
||||
defaultMimeMapping = new Dictionary<string, string>(9, StringComparer.OrdinalIgnoreCase);
|
||||
defaultMimeMapping.Add(".bmp", "image/bmp");
|
||||
defaultMimeMapping.Add(".gif", "image/gif");
|
||||
defaultMimeMapping.Add(".jpg", "image/jpeg");
|
||||
defaultMimeMapping.Add(".jpeg", "image/jpeg");
|
||||
defaultMimeMapping.Add(".pdf", "application/pdf");
|
||||
defaultMimeMapping.Add(".png", "image/png");
|
||||
defaultMimeMapping.Add(".tif", "image/tiff");
|
||||
defaultMimeMapping.Add(".txt", "text/plain");
|
||||
defaultMimeMapping.Add(".zip", "application/x-zip-compressed");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the given class is supported by this converter.
|
||||
/// Creates a new instance of the <see cref="FileInfoHttpMessageConverter"/>
|
||||
/// with 'application/octet-stream', and '*/*' media types.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to test for support.</param>
|
||||
/// <returns><see langword="true"/> if supported; otherwise <see langword="false"/></returns>
|
||||
protected override bool Supports(Type type)
|
||||
public FileInfoHttpMessageConverter()
|
||||
{
|
||||
this._supportedMediaTypes = new List<MediaType>();
|
||||
this._supportedMediaTypes.Add(MediaType.APPLICATION_OCTET_STREAM);
|
||||
this._supportedMediaTypes.Add(MediaType.ALL);
|
||||
}
|
||||
|
||||
#region IHttpMessageConverter Membres
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the given class can be read by this converter.
|
||||
/// </summary>
|
||||
/// <param name="type">The class to test for readability</param>
|
||||
/// <param name="mediaType">
|
||||
/// The media type to read, can be null if not specified. Typically the value of a 'Content-Type' header.
|
||||
/// </param>
|
||||
/// <returns><see langword="true"/> if readable; otherwise <see langword="false"/></returns>
|
||||
public bool CanRead(Type type, MediaType mediaType)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the given class can be written by this converter.
|
||||
/// </summary>
|
||||
/// <param name="type">The class to test for writability</param>
|
||||
/// <param name="mediaType">
|
||||
/// The media type to write, can be null if not specified. Typically the value of an 'Accept' header.
|
||||
/// </param>
|
||||
/// <returns><see langword="true"/> if writable; otherwise <see langword="false"/></returns>
|
||||
public bool CanWrite(Type type, MediaType mediaType)
|
||||
{
|
||||
return type.Equals(typeof(FileInfo));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>.
|
||||
/// Gets the list of <see cref="MediaType"/> objects supported by this converter.
|
||||
/// </summary>
|
||||
public IList<MediaType> SupportedMediaTypes
|
||||
{
|
||||
get { return this._supportedMediaTypes; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read an object of the given type form the given HTTP message, and returns it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object to return.</typeparam>
|
||||
/// <typeparam name="T">
|
||||
/// The type of object to return. This type must have previously been passed to the
|
||||
/// <see cref="M:CanRead"/> method of this interface, which must have returned <see langword="true"/>.
|
||||
/// </typeparam>
|
||||
/// <param name="message">The HTTP message to read from.</param>
|
||||
/// <returns>The converted object.</returns>
|
||||
/// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception>
|
||||
protected override T ReadInternal<T>(IHttpInputMessage message)
|
||||
public T Read<T>(IHttpInputMessage message) where T : class
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
|
||||
/// Write an given object to the given HTTP message.
|
||||
/// </summary>
|
||||
/// <param name="content">The object to write to the HTTP message.</param>
|
||||
/// <param name="content">
|
||||
/// The object to write to the HTTP message. The type of this object must have previously been
|
||||
/// passed to the <see cref="M:CanWrite"/> method of this interface, which must have returned <see langword="true"/>.
|
||||
/// </param>
|
||||
/// <param name="contentType">
|
||||
/// The content type to use when writing. May be null to indicate that the default content type of the converter must be used.
|
||||
/// If not null, this media type must have previously been passed to the <see cref="M:CanWrite"/> method of this interface,
|
||||
/// which must have returned <see langword="true"/>.
|
||||
/// </param>
|
||||
/// <param name="message">The HTTP message to write to.</param>
|
||||
/// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
|
||||
protected override void WriteInternal(object content, IHttpOutputMessage message)
|
||||
public void Write(object content, MediaType contentType, IHttpOutputMessage message)
|
||||
{
|
||||
// Get the content type
|
||||
HttpHeaders headers = message.Headers;
|
||||
if (headers.ContentType == null)
|
||||
{
|
||||
if (contentType == null || contentType.IsWildcardType || contentType.IsWildcardSubtype)
|
||||
{
|
||||
contentType = GetContentType(content as FileInfo);
|
||||
}
|
||||
if (contentType != null)
|
||||
{
|
||||
headers.ContentType = contentType;
|
||||
}
|
||||
}
|
||||
|
||||
// Write to the message stream
|
||||
message.Body = delegate(Stream stream)
|
||||
{
|
||||
@@ -78,5 +173,23 @@ namespace Spring.Http.Converters
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private MediaType GetContentType(FileInfo file)
|
||||
{
|
||||
IDictionary<string, string> mimeMapping =
|
||||
(this._mimeMapping == null) ? defaultMimeMapping : this._mimeMapping;
|
||||
|
||||
string mimeType;
|
||||
if (mimeMapping.TryGetValue(file.Extension, out mimeType))
|
||||
{
|
||||
return MediaType.Parse(mimeType);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,6 @@ namespace Spring.Http
|
||||
/// <summary>
|
||||
/// Sets the delegate that writes the body message as a stream.
|
||||
/// </summary>
|
||||
Action<Stream> Body { get; set; }
|
||||
Action<Stream> Body { set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1843,11 +1843,11 @@ namespace Spring.Http.Rest
|
||||
{
|
||||
if (this._errorHandler.HasError(response))
|
||||
{
|
||||
this.HandleResponseError(uri, method, response);
|
||||
HandleResponseError(uri, method, response, this._errorHandler);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.LogResponseStatus(uri, method, response);
|
||||
LogResponseStatus(uri, method, response);
|
||||
}
|
||||
|
||||
if (responseExtractor != null)
|
||||
@@ -1883,7 +1883,7 @@ namespace Spring.Http.Rest
|
||||
{
|
||||
IClientHttpRequest request = this._requestFactory.CreateRequest(uri, method);
|
||||
|
||||
ExecuteState<T> state = new ExecuteState<T>(uri, method, responseExtractor, methodCompleted);
|
||||
ExecuteState<T> state = new ExecuteState<T>(uri, method, responseExtractor, this._errorHandler, methodCompleted);
|
||||
|
||||
if (requestCallback != null)
|
||||
{
|
||||
@@ -1900,7 +1900,7 @@ namespace Spring.Http.Rest
|
||||
}
|
||||
}
|
||||
|
||||
private void ResponseReceivedCallback<T>(ExecuteCompletedEventArgs responseReceived) where T : class
|
||||
private static void ResponseReceivedCallback<T>(ExecuteCompletedEventArgs responseReceived) where T : class
|
||||
{
|
||||
ExecuteState<T> state = (ExecuteState<T>)responseReceived.UserState;
|
||||
if (responseReceived.Error == null)
|
||||
@@ -1917,13 +1917,13 @@ namespace Spring.Http.Rest
|
||||
Exception exception = null;
|
||||
try
|
||||
{
|
||||
if (this._errorHandler.HasError(response))
|
||||
if (state.ResponseErrorHandler.HasError(response))
|
||||
{
|
||||
this.HandleResponseError(state.Uri, state.Method, response);
|
||||
HandleResponseError(state.Uri, state.Method, response, state.ResponseErrorHandler);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.LogResponseStatus(state.Uri, state.Method, response);
|
||||
LogResponseStatus(state.Uri, state.Method, response);
|
||||
}
|
||||
|
||||
if (state.ResponseExtractor != null)
|
||||
@@ -1948,20 +1948,23 @@ namespace Spring.Http.Rest
|
||||
}
|
||||
}
|
||||
|
||||
private class ExecuteState<T> where T : class
|
||||
private sealed class ExecuteState<T> where T : class
|
||||
{
|
||||
public Uri Uri;
|
||||
public HttpMethod Method;
|
||||
public IResponseExtractor<T> ResponseExtractor;
|
||||
public IResponseErrorHandler ResponseErrorHandler;
|
||||
public Action<MethodCompletedEventArgs<T>> MethodCompleted;
|
||||
|
||||
public ExecuteState(Uri uri, HttpMethod method,
|
||||
IResponseExtractor<T> responseExtractor,
|
||||
IResponseErrorHandler responseErrorHandler,
|
||||
Action<MethodCompletedEventArgs<T>> methodCompleted)
|
||||
{
|
||||
this.Uri = uri;
|
||||
this.Method = method;
|
||||
this.ResponseExtractor = responseExtractor;
|
||||
this.ResponseErrorHandler = responseErrorHandler;
|
||||
this.MethodCompleted = methodCompleted;
|
||||
}
|
||||
}
|
||||
@@ -2030,7 +2033,7 @@ namespace Spring.Http.Rest
|
||||
|
||||
#endregion
|
||||
|
||||
private void LogResponseStatus(Uri uri, HttpMethod method, IClientHttpResponse response)
|
||||
private static void LogResponseStatus(Uri uri, HttpMethod method, IClientHttpResponse response)
|
||||
{
|
||||
#region Instrumentation
|
||||
#if !SILVERLIGHT
|
||||
@@ -2044,7 +2047,8 @@ namespace Spring.Http.Rest
|
||||
#endregion
|
||||
}
|
||||
|
||||
private void HandleResponseError(Uri uri, HttpMethod method, IClientHttpResponse response)
|
||||
private static void HandleResponseError(Uri uri, HttpMethod method, IClientHttpResponse response,
|
||||
IResponseErrorHandler errorHandler)
|
||||
{
|
||||
#region Instrumentation
|
||||
#if !SILVERLIGHT
|
||||
@@ -2057,7 +2061,7 @@ namespace Spring.Http.Rest
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
this._errorHandler.HandleError(response);
|
||||
errorHandler.HandleError(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,7 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
@@ -35,12 +31,10 @@ namespace Spring.Http.Converters
|
||||
public class ByteArrayHttpMessageConverterTests
|
||||
{
|
||||
private ByteArrayHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new ByteArrayHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -67,45 +61,27 @@ namespace Spring.Http.Converters
|
||||
{
|
||||
byte[] body = new byte[] { 0x1, 0x2 };
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(body));
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.ContentLength = body.Length;
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body);
|
||||
message.Headers.ContentLength = body.Length;
|
||||
|
||||
byte[] result = converter.Read<byte[]>(message);
|
||||
Assert.AreEqual(body.Length, result.Length, "Invalid result");
|
||||
Assert.AreEqual(body[0], result[0], "Invalid result");
|
||||
Assert.AreEqual(body[1], result[1], "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
byte[] body = new byte[] { 0x1, 0x2 };
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body, result, "Invalid result");
|
||||
Assert.AreEqual(body, message.GetBodyAsBytes(), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "octet-stream"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.AreEqual(2, message.Headers.ContentLength, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,11 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.ServiceModel.Syndication;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Feed
|
||||
{
|
||||
@@ -39,12 +36,10 @@ namespace Spring.Http.Converters.Feed
|
||||
public class Atom10FeedHttpMessageConverterTests
|
||||
{
|
||||
private Atom10FeedHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new Atom10FeedHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -77,10 +72,7 @@ namespace Spring.Http.Converters.Feed
|
||||
string body = String.Format("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\">Test Feed</title><subtitle type=\"text\">This is a test feed</subtitle><id>Atom10FeedHttpMessageConverterTests.Write</id><rights type=\"text\">Copyright 2010</rights><updated>{0}</updated><author><name>Bruno Baïa</name><uri>http://www.springframework.net/bbaia</uri><email>bruno.baia@springframework.net</email></author><link rel=\"alternate\" href=\"http://www.springframework.net/Feed\" /></feed>",
|
||||
now.ToString("yyyy-MM-ddTHH:mm:sszzz", CultureInfo.InvariantCulture));
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
SyndicationFeed result = converter.Read<SyndicationFeed>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
@@ -94,15 +86,11 @@ namespace Spring.Http.Converters.Feed
|
||||
Assert.AreEqual("Bruno Baïa", result.Authors[0].Name, "Invalid result");
|
||||
Assert.AreEqual("bruno.baia@springframework.net", result.Authors[0].Email, "Invalid result");
|
||||
Assert.AreEqual("http://www.springframework.net/bbaia", result.Authors[0].Uri, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
string expectedBody = String.Format("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\">Test Feed</title><subtitle type=\"text\">This is a test feed</subtitle><id>Atom10FeedHttpMessageConverterTests.Write</id><rights type=\"text\">Copyright 2010</rights><updated>{0}</updated><author><name>Bruno Baïa</name><uri>http://www.springframework.net/bbaia</uri><email>bruno.baia@springframework.net</email></author><link rel=\"alternate\" href=\"http://www.springframework.net/Feed\" /></feed>",
|
||||
@@ -113,22 +101,13 @@ namespace Spring.Http.Converters.Feed
|
||||
body.Authors.Add(sp);
|
||||
body.Copyright = new TextSyndicationContent("Copyright 2010");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "atom+xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,11 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.ServiceModel.Syndication;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Feed
|
||||
{
|
||||
@@ -39,12 +36,10 @@ namespace Spring.Http.Converters.Feed
|
||||
public class Rss20FeedHttpMessageConverterTests
|
||||
{
|
||||
private Rss20FeedHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new Rss20FeedHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -78,10 +73,7 @@ namespace Spring.Http.Converters.Feed
|
||||
string body = String.Format("<rss xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel><title>Test Feed</title><link>http://www.springframework.net/Feed</link><description>This is a test feed</description><copyright>Copyright 2010</copyright><managingEditor>bruno.baia@springframework.net</managingEditor><lastBuildDate>{0}</lastBuildDate><a10:id>Atom10FeedHttpMessageConverterTests.Write</a10:id></channel></rss>",
|
||||
now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture).Remove(29, 1));
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
SyndicationFeed result = converter.Read<SyndicationFeed>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
@@ -93,15 +85,11 @@ namespace Spring.Http.Converters.Feed
|
||||
Assert.AreEqual("Copyright 2010", result.Copyright.Text, "Invalid result");
|
||||
Assert.IsTrue(result.Authors.Count == 1, "Invalid result");
|
||||
Assert.AreEqual("bruno.baia@springframework.net", result.Authors[0].Email, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
string expectedBody = String.Format("<rss xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel><title>Test Feed</title><link>http://www.springframework.net/Feed</link><description>This is a test feed</description><copyright>Copyright 2010</copyright><managingEditor>bruno.baia@springframework.net</managingEditor><lastBuildDate>{0}</lastBuildDate><a10:id>Atom10FeedHttpMessageConverterTests.Write</a10:id></channel></rss>",
|
||||
@@ -112,22 +100,13 @@ namespace Spring.Http.Converters.Feed
|
||||
body.Authors.Add(sp);
|
||||
body.Copyright = new TextSyndicationContent("Copyright 2010");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "rss+xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the FileInfoHttpMessageConverter class.
|
||||
/// </summary>
|
||||
/// <author>Bruno Baia</author>
|
||||
[TestFixture]
|
||||
public class FileInfoHttpMessageConverterTests
|
||||
{
|
||||
private FileInfoHttpMessageConverter converter;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
converter = new FileInfoHttpMessageConverter();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanRead()
|
||||
{
|
||||
Assert.IsFalse(converter.CanRead(typeof(FileInfo), MediaType.ALL));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanWrite()
|
||||
{
|
||||
Assert.IsTrue(converter.CanWrite(typeof(FileInfo), new MediaType("application", "octet-stream")));
|
||||
Assert.IsTrue(converter.CanWrite(typeof(FileInfo), new MediaType("application", "xml")));
|
||||
Assert.IsTrue(converter.CanWrite(typeof(FileInfo), MediaType.ALL));
|
||||
Assert.IsFalse(converter.CanWrite(typeof(string), new MediaType("application", "octet-stream")));
|
||||
}
|
||||
|
||||
//[Test]
|
||||
//public void Write()
|
||||
//{
|
||||
// FileInfo body = new FileInfo(@"C:\File.txt");
|
||||
|
||||
// MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
// converter.Write(body, null, message);
|
||||
|
||||
// Assert.AreEqual(body, message.GetBodyAsBytes(), "Invalid result");
|
||||
// Assert.AreEqual(new MediaType("text", "plain"), message.Headers.ContentType, "Invalid content-type");
|
||||
//}
|
||||
|
||||
[Test]
|
||||
public void WriteWithUnknownExtension()
|
||||
{
|
||||
FileInfo body = new FileInfo(@"C:\Dummy.unknown");
|
||||
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
//Assert.AreEqual(body, message.GetBodyAsBytes(), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "octet-stream"), message.Headers.ContentType, "Invalid content-type");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteWithKnownExtension()
|
||||
{
|
||||
FileInfo body = new FileInfo(@"C:\Dummy.txt");
|
||||
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
Assert.AreEqual(new MediaType("text", "plain"), message.Headers.ContentType, "Invalid content-type");
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void WriteWithCustomExtension()
|
||||
{
|
||||
FileInfo body = new FileInfo(@"C:\Dummy.myext");
|
||||
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.MimeMapping.Add(".myext", "spring/custom");
|
||||
converter.Write(body, null, message);
|
||||
|
||||
Assert.AreEqual(new MediaType("spring", "custom"), message.Headers.ContentType, "Invalid content-type");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,13 +20,11 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
@@ -39,12 +37,10 @@ namespace Spring.Http.Converters
|
||||
public class FormHttpMessageConverterTests
|
||||
{
|
||||
private FormHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new FormHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -76,13 +72,8 @@ namespace Spring.Http.Converters
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
MediaType mediaType = new MediaType("application", "x-www-form-urlencoded", charSet);
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.ContentType = mediaType;
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, charSetEncoding);
|
||||
message.Headers.ContentType = mediaType;
|
||||
|
||||
NameValueCollection result = converter.Read<NameValueCollection>(message);
|
||||
Assert.AreEqual(3, result.Count, "Invalid result");
|
||||
@@ -92,15 +83,11 @@ namespace Spring.Http.Converters
|
||||
Assert.AreEqual("value 2+1", result.GetValues("name 2")[0], "Invalid result");
|
||||
Assert.AreEqual("value 2+2", result.GetValues("name 2")[1], "Invalid result");
|
||||
Assert.IsNull(result["name 3"], "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteForm()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string expectedBody = "name+1=value+1&name+2=value+2%2b1&name+2=value+2%2b2&name+3";
|
||||
NameValueCollection body = new NameValueCollection();
|
||||
body.Add("name 1", "value 1");
|
||||
@@ -110,30 +97,19 @@ namespace Spring.Http.Converters
|
||||
string charSet = "ISO-8859-1";
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, MediaType.APPLICATION_FORM_URLENCODED, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, charSetEncoding.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(charSetEncoding), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "x-www-form-urlencoded"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.AreEqual(charSetEncoding.GetBytes(expectedBody).Length, message.Headers.ContentLength, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Ignore] //TODO: relative path (needs IResource ?)
|
||||
public void WriteMultipart()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
IDictionary<string, object> parts = new Dictionary<string, object>();
|
||||
parts.Add("name 1", "value 1");
|
||||
parts.Add("name 2", "value 2+1");
|
||||
@@ -142,12 +118,7 @@ namespace Spring.Http.Converters
|
||||
parts.Add("xml", entity);
|
||||
parts.Add("logo", new FileInfo(@"C:\Users\Bruno\Pictures\Hero\downloadfile.jpeg"));
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(parts, MediaType.MULTIPART_FORM_DATA, message);
|
||||
|
||||
@@ -158,15 +129,11 @@ namespace Spring.Http.Converters
|
||||
string boundary = contentType.GetParameter("boundary");
|
||||
Assert.IsNotNull(boundary, "Invalid content-type");
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
string resultAsString = Encoding.UTF8.GetString(result);
|
||||
Assert.IsTrue(resultAsString.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 1\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 1\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(resultAsString.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 2\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 2+1\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(resultAsString.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"xml\"\r\nContent-Type: text/xml\r\n\r\n<root><child/></root>\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(resultAsString.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"logo\"; filename=\"C:\\Users\\Bruno\\Pictures\\Hero\\downloadfile.jpeg\"\r\nContent-Type: application/octet-stream\r\n\r\n"), "Invalid content-disposition");
|
||||
|
||||
mocks.VerifyAll();
|
||||
string result = message.GetBodyAsString(Encoding.UTF8);
|
||||
Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 1\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 1\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 2\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 2+1\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"xml\"\r\nContent-Type: text/xml\r\n\r\n<root><child/></root>\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"logo\"; filename=\"C:\\Users\\Bruno\\Pictures\\Hero\\downloadfile.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n"), "Invalid content-disposition");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,9 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Json
|
||||
{
|
||||
@@ -36,12 +33,10 @@ namespace Spring.Http.Converters.Json
|
||||
public class JsonHttpMessageConverterTests
|
||||
{
|
||||
private JsonHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new JsonHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -64,43 +59,27 @@ namespace Spring.Http.Converters.Json
|
||||
{
|
||||
string body = "{\"ID\":\"1\",\"Name\":\"Bruno Baïa\"}";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
CustomClass result = converter.Read<CustomClass>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
Assert.AreEqual("1", result.ID, "Invalid result");
|
||||
Assert.AreEqual("Bruno Baïa", result.Name, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string expectedBody = "{\"ID\":\"1\",\"Name\":\"Bruno Baïa\"}";
|
||||
CustomClass body = new CustomClass("1", "Bruno Baïa");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "json"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
#region Test classes
|
||||
|
||||
@@ -18,13 +18,9 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
@@ -37,12 +33,10 @@ namespace Spring.Http.Converters
|
||||
public class StringHttpMessageConverterTests
|
||||
{
|
||||
private StringHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new StringHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -72,74 +66,45 @@ namespace Spring.Http.Converters
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
MediaType mediaType = new MediaType("text", "plain", charSet);
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.ContentType = mediaType;
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, charSetEncoding);
|
||||
message.Headers.ContentType = mediaType;
|
||||
|
||||
string result = converter.Read<string>(message);
|
||||
Assert.AreEqual(body, result, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteDefaultCharset()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string body = "H\u00e9llo W\u00f6rld";
|
||||
string charSet = "ISO-8859-1";
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
MediaType mediaType = new MediaType("text", "plain", charSet);
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body, charSetEncoding.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(body, message.GetBodyAsString(charSetEncoding), "Invalid result");
|
||||
Assert.AreEqual(mediaType, message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.AreEqual(charSetEncoding.GetBytes(body).Length, message.Headers.ContentLength, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteUTF8()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string body = "H\u00e9llo W\u00f6rld";
|
||||
string charSet = "UTF-8";
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
MediaType mediaType = new MediaType("text", "plain", charSet);
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, mediaType, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body, charSetEncoding.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(body, message.GetBodyAsString(charSetEncoding), "Invalid result");
|
||||
Assert.AreEqual(mediaType, message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.AreEqual(charSetEncoding.GetBytes(body).Length, message.Headers.ContentLength, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,11 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
@@ -38,12 +35,10 @@ namespace Spring.Http.Converters.Xml
|
||||
public class DataContractHttpMessageConverterTests
|
||||
{
|
||||
private DataContractHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new DataContractHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -77,43 +72,27 @@ namespace Spring.Http.Converters.Xml
|
||||
<ID>1</ID><Name>Bruno Baïa</Name>
|
||||
</DataContractHttpMessageConverterTests.DataContractClass>";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
DataContractClass result = converter.Read<DataContractClass>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
Assert.AreEqual("1", result.ID, "Invalid result");
|
||||
Assert.AreEqual("Bruno Baïa", result.Name, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string expectedBody = "<DataContractHttpMessageConverterTests.DataContractClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Spring.Http.Converters.Xml\"><ID>1</ID><Name>Bruno Baïa</Name></DataContractHttpMessageConverterTests.DataContractClass>";
|
||||
DataContractClass body = new DataContractClass("1", "Bruno Baïa");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
#region Test classes
|
||||
|
||||
@@ -20,14 +20,11 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
@@ -39,12 +36,10 @@ namespace Spring.Http.Converters.Xml
|
||||
public class XElementHttpMessageConverterTests
|
||||
{
|
||||
private XElementHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new XElementHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -73,10 +68,7 @@ namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
string body = "<?xml version='1.0' encoding='UTF-8' ?><Root><TestElement testAttribute='value'/><TestElement testAttribute='novalue'/></Root>";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
XElement result = converter.Read<XElement>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
@@ -88,35 +80,22 @@ namespace Spring.Http.Converters.Xml
|
||||
select el)
|
||||
.Single();
|
||||
Assert.IsNotNull(xResult, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
XElement body = new XElement("Root",
|
||||
new XElement("TestElement", 1),
|
||||
new XElement("TestElement", 2));
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body.ToString(SaveOptions.DisableFormatting), Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(body.ToString(SaveOptions.DisableFormatting), message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,13 +19,10 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
@@ -37,12 +34,10 @@ namespace Spring.Http.Converters.Xml
|
||||
public class XmlDocumentHttpMessageConverterTests
|
||||
{
|
||||
private XmlDocumentHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new XmlDocumentHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -71,10 +66,7 @@ namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
string body = "<TestElement testAttribute='value' />";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
XmlDocument result = converter.Read<XmlDocument>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
@@ -83,34 +75,21 @@ namespace Spring.Http.Converters.Xml
|
||||
Assert.AreEqual("TestElement", xmlNodeResult.LocalName, "Invalid result");
|
||||
Assert.IsNotNull(xmlNodeResult.Attributes["testAttribute"], "Invalid result");
|
||||
Assert.AreEqual("value", xmlNodeResult.Attributes["testAttribute"].Value, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
XmlDocument body = new XmlDocument();
|
||||
body.LoadXml("<TestElement testAttribute='value' />");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body.OuterXml, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(body.OuterXml, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,9 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
@@ -35,12 +32,10 @@ namespace Spring.Http.Converters.Xml
|
||||
public class XmlSerializableHttpMessageConverterTests
|
||||
{
|
||||
private XmlSerializableHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new XmlSerializableHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -70,43 +65,27 @@ namespace Spring.Http.Converters.Xml
|
||||
<Name>Bruno Baïa</Name>
|
||||
</CustomClass>";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
CustomClass result = converter.Read<CustomClass>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
Assert.AreEqual("1", result.ID, "Invalid result");
|
||||
Assert.AreEqual("Bruno Baïa", result.Name, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string expectedBody = "<CustomClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><ID>1</ID><Name>Bruno Baïa</Name></CustomClass>";
|
||||
CustomClass body = new CustomClass("1", "Bruno Baïa");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
#region Test classes
|
||||
|
||||
58
test/Spring/Spring.Http.Tests/Http/MockHttpInputMessage.cs
Normal file
58
test/Spring/Spring.Http.Tests/Http/MockHttpInputMessage.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Spring.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// Mocked IHttpInputMessage implementation.
|
||||
/// </summary>
|
||||
/// <author>Arjen Poutsma</author>
|
||||
/// <author>Bruno Baia (.NET)</author>
|
||||
public class MockHttpInputMessage : IHttpInputMessage
|
||||
{
|
||||
private HttpHeaders headers;
|
||||
private Stream body;
|
||||
|
||||
public MockHttpInputMessage(byte[] body)
|
||||
{
|
||||
this.headers = new HttpHeaders();
|
||||
this.body = new MemoryStream(body);
|
||||
}
|
||||
|
||||
public MockHttpInputMessage(string body, Encoding charset)
|
||||
: this(charset.GetBytes(body))
|
||||
{
|
||||
}
|
||||
|
||||
public HttpHeaders Headers
|
||||
{
|
||||
get { return this.headers; }
|
||||
}
|
||||
|
||||
public Stream Body
|
||||
{
|
||||
get { return this.body; }
|
||||
}
|
||||
}
|
||||
}
|
||||
72
test/Spring/Spring.Http.Tests/Http/MockHttpOuputMessage.cs
Normal file
72
test/Spring/Spring.Http.Tests/Http/MockHttpOuputMessage.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Spring.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// Mocked IHttpOutputMessage implementation.
|
||||
/// </summary>
|
||||
/// <author>Arjen Poutsma</author>
|
||||
/// <author>Bruno Baia (.NET)</author>
|
||||
public class MockHttpOutputMessage : IHttpOutputMessage
|
||||
{
|
||||
private HttpHeaders headers;
|
||||
private Action<Stream> body;
|
||||
private byte[] bodyAsBytes;
|
||||
|
||||
public MockHttpOutputMessage()
|
||||
{
|
||||
this.headers = new HttpHeaders();
|
||||
}
|
||||
|
||||
public HttpHeaders Headers
|
||||
{
|
||||
get { return this.headers; }
|
||||
}
|
||||
|
||||
public Action<Stream> Body
|
||||
{
|
||||
set { this.body = value; }
|
||||
}
|
||||
|
||||
public byte[] GetBodyAsBytes()
|
||||
{
|
||||
if (bodyAsBytes == null)
|
||||
{
|
||||
using (MemoryStream requestStream = new MemoryStream())
|
||||
{
|
||||
this.body(requestStream);
|
||||
bodyAsBytes = requestStream.ToArray();
|
||||
}
|
||||
}
|
||||
return bodyAsBytes;
|
||||
}
|
||||
|
||||
public String GetBodyAsString(Encoding charset)
|
||||
{
|
||||
byte[] bytes = GetBodyAsBytes();
|
||||
return charset.GetString(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="Http\Converters\ByteArrayHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FileInfoHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FormHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\Atom10FeedHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\FeedHttpMessageConverterIntegrationTests.cs" />
|
||||
@@ -103,8 +104,10 @@
|
||||
<Compile Include="Http\Converters\Xml\XmlDocumentHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Xml\XmlHttpMessageConverterIntegrationTests.cs" />
|
||||
<Compile Include="Http\Converters\Xml\XmlSerializableHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\MockHttpOuputMessage.cs" />
|
||||
<Compile Include="Http\HttpHeadersTests.cs" />
|
||||
<Compile Include="Http\MediaTypeTests.cs" />
|
||||
<Compile Include="Http\MockHttpInputMessage.cs" />
|
||||
<Compile Include="Http\Rest\HttpStatusCodeExceptionTests.cs" />
|
||||
<Compile Include="Http\Rest\RestTemplateIntegrationTests.cs" />
|
||||
<Compile Include="Util\SerializationTestUtils.cs" />
|
||||
|
||||
@@ -107,6 +107,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="Http\Converters\ByteArrayHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FileInfoHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\Atom10FeedHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\FeedHttpMessageConverterIntegrationTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\Rss20FeedHttpMessageConverterTests.cs" />
|
||||
@@ -121,8 +122,10 @@
|
||||
<Compile Include="Http\Converters\Xml\XmlDocumentHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Xml\XmlHttpMessageConverterIntegrationTests.cs" />
|
||||
<Compile Include="Http\Converters\Xml\XmlSerializableHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\MockHttpOuputMessage.cs" />
|
||||
<Compile Include="Http\HttpHeadersTests.cs" />
|
||||
<Compile Include="Http\MediaTypeTests.cs" />
|
||||
<Compile Include="Http\MockHttpInputMessage.cs" />
|
||||
<Compile Include="Http\Rest\HttpStatusCodeExceptionTests.cs" />
|
||||
<Compile Include="Http\Rest\RestTemplateIntegrationTests.cs" />
|
||||
<Compile Include="Util\SerializationTestUtils.cs" />
|
||||
|
||||
@@ -125,6 +125,7 @@
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Http\Converters\Feed\Rss20FeedHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FileInfoHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FormHttpMessageConverterTests.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
@@ -138,6 +139,8 @@
|
||||
<Compile Include="Http\Converters\Xml\XmlDocumentHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\HttpHeadersTests.cs" />
|
||||
<Compile Include="Http\MediaTypeTests.cs" />
|
||||
<Compile Include="Http\MockHttpInputMessage.cs" />
|
||||
<Compile Include="Http\MockHttpOuputMessage.cs" />
|
||||
<Compile Include="Http\Rest\HttpStatusCodeExceptionTests.cs" />
|
||||
<Compile Include="Http\Rest\RestTemplateIntegrationTests.cs" />
|
||||
<Compile Include="Http\Rest\RestTemplateTests.cs" />
|
||||
|
||||
Reference in New Issue
Block a user