diff --git a/src/Spring/Spring.Http/Http/Client/WebClientHttpRequest.cs b/src/Spring/Spring.Http/Http/Client/WebClientHttpRequest.cs index 8f409d58..b980e3d1 100644 --- a/src/Spring/Spring.Http/Http/Client/WebClientHttpRequest.cs +++ b/src/Spring/Spring.Http/Http/Client/WebClientHttpRequest.cs @@ -102,7 +102,6 @@ namespace Spring.Http.Client /// public Action Body { - get { return this.body; } set { this.body = value; } } diff --git a/src/Spring/Spring.Http/Http/Converters/FileInfoHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/FileInfoHttpMessageConverter.cs index 84896f84..03e3a920 100644 --- a/src/Spring/Spring.Http/Http/Converters/FileInfoHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/FileInfoHttpMessageConverter.cs @@ -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 { + /// + /// Implementation of that can write files. + /// + /// + /// 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. + /// /// Bruno Baia - public class FileInfoHttpMessageConverter : AbstractHttpMessageConverter + public class FileInfoHttpMessageConverter : IHttpMessageConverter { + // Pre-defined mapping between file extension and mime types + private static IDictionary defaultMimeMapping; + + private IList _supportedMediaTypes; + private IDictionary _mimeMapping; + /// - /// Creates a new instance of the - /// with 'text/plain; charset=ISO-8859-1', and '*/*' media types. + /// Gets or sets the mapping between file extension and mime types. /// - public FileInfoHttpMessageConverter() : - base(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL) + public IDictionary MimeMapping { + get + { + if (this._mimeMapping == null) + { + this._mimeMapping = new Dictionary(defaultMimeMapping); + } + return _mimeMapping; + } + set { _mimeMapping = value; } + } + + static FileInfoHttpMessageConverter() + { + defaultMimeMapping = new Dictionary(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"); } /// - /// Indicates whether the given class is supported by this converter. + /// Creates a new instance of the + /// with 'application/octet-stream', and '*/*' media types. /// - /// The type to test for support. - /// if supported; otherwise - protected override bool Supports(Type type) + public FileInfoHttpMessageConverter() + { + this._supportedMediaTypes = new List(); + this._supportedMediaTypes.Add(MediaType.APPLICATION_OCTET_STREAM); + this._supportedMediaTypes.Add(MediaType.ALL); + } + + #region IHttpMessageConverter Membres + + /// + /// Indicates whether the given class can be read by this converter. + /// + /// The class to test for readability + /// + /// The media type to read, can be null if not specified. Typically the value of a 'Content-Type' header. + /// + /// if readable; otherwise + public bool CanRead(Type type, MediaType mediaType) + { + return false; + } + + /// + /// Indicates whether the given class can be written by this converter. + /// + /// The class to test for writability + /// + /// The media type to write, can be null if not specified. Typically the value of an 'Accept' header. + /// + /// if writable; otherwise + public bool CanWrite(Type type, MediaType mediaType) { return type.Equals(typeof(FileInfo)); } /// - /// Abstract template method that reads the actualy object. Invoked from . + /// Gets the list of objects supported by this converter. + /// + public IList SupportedMediaTypes + { + get { return this._supportedMediaTypes; } + } + + /// + /// Read an object of the given type form the given HTTP message, and returns it. /// - /// The type of object to return. + /// + /// The type of object to return. This type must have previously been passed to the + /// method of this interface, which must have returned . + /// /// The HTTP message to read from. /// The converted object. /// In case of conversion errors - protected override T ReadInternal(IHttpInputMessage message) + public T Read(IHttpInputMessage message) where T : class { throw new NotSupportedException(); } /// - /// Abstract template method that writes the actual body. Invoked from . + /// Write an given object to the given HTTP message. /// - /// The object to write to the HTTP message. + /// + /// The object to write to the HTTP message. The type of this object must have previously been + /// passed to the method of this interface, which must have returned . + /// + /// + /// 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 method of this interface, + /// which must have returned . + /// /// The HTTP message to write to. /// In case of conversion errors - 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 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; + } + } } } diff --git a/src/Spring/Spring.Http/Http/IHttpOutputMessage.cs b/src/Spring/Spring.Http/Http/IHttpOutputMessage.cs index 3610f27b..5d1ad655 100644 --- a/src/Spring/Spring.Http/Http/IHttpOutputMessage.cs +++ b/src/Spring/Spring.Http/Http/IHttpOutputMessage.cs @@ -42,6 +42,6 @@ namespace Spring.Http /// /// Sets the delegate that writes the body message as a stream. /// - Action Body { get; set; } + Action Body { set; } } } diff --git a/src/Spring/Spring.Http/Http/Rest/RestTemplate.cs b/src/Spring/Spring.Http/Http/Rest/RestTemplate.cs index 93605a38..417f06b3 100644 --- a/src/Spring/Spring.Http/Http/Rest/RestTemplate.cs +++ b/src/Spring/Spring.Http/Http/Rest/RestTemplate.cs @@ -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 state = new ExecuteState(uri, method, responseExtractor, methodCompleted); + ExecuteState state = new ExecuteState(uri, method, responseExtractor, this._errorHandler, methodCompleted); if (requestCallback != null) { @@ -1900,7 +1900,7 @@ namespace Spring.Http.Rest } } - private void ResponseReceivedCallback(ExecuteCompletedEventArgs responseReceived) where T : class + private static void ResponseReceivedCallback(ExecuteCompletedEventArgs responseReceived) where T : class { ExecuteState state = (ExecuteState)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 where T : class + private sealed class ExecuteState where T : class { public Uri Uri; public HttpMethod Method; public IResponseExtractor ResponseExtractor; + public IResponseErrorHandler ResponseErrorHandler; public Action> MethodCompleted; public ExecuteState(Uri uri, HttpMethod method, IResponseExtractor responseExtractor, + IResponseErrorHandler responseErrorHandler, Action> 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); } } } diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/ByteArrayHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/ByteArrayHttpMessageConverterTests.cs index 42e18782..ff917877 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/ByteArrayHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/ByteArrayHttpMessageConverterTests.cs @@ -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(); - Expect.Call(message.Body).Return(new MemoryStream(body)); - HttpHeaders headers = new HttpHeaders(); - headers.ContentLength = body.Length; - Expect.Call(message.Headers).Return(headers).Repeat.Any(); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body); + message.Headers.ContentLength = body.Length; byte[] result = converter.Read(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(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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(); } } } diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Atom10FeedHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Atom10FeedHttpMessageConverterTests.cs index 59684b71..e49412e6 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Atom10FeedHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Atom10FeedHttpMessageConverterTests.cs @@ -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("Test FeedThis is a test feedAtom10FeedHttpMessageConverterTests.WriteCopyright 2010{0}Bruno Baïahttp://www.springframework.net/bbaiabruno.baia@springframework.net", now.ToString("yyyy-MM-ddTHH:mm:sszzz", CultureInfo.InvariantCulture)); - IHttpInputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8); SyndicationFeed result = converter.Read(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("Test FeedThis is a test feedAtom10FeedHttpMessageConverterTests.WriteCopyright 2010{0}Bruno Baïahttp://www.springframework.net/bbaiabruno.baia@springframework.net", @@ -113,22 +101,13 @@ namespace Spring.Http.Converters.Feed body.Authors.Add(sp); body.Copyright = new TextSyndicationContent("Copyright 2010"); - IHttpOutputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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(); } } } diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Rss20FeedHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Rss20FeedHttpMessageConverterTests.cs index d8d583a8..fafe1b68 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Rss20FeedHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Rss20FeedHttpMessageConverterTests.cs @@ -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("Test Feedhttp://www.springframework.net/FeedThis is a test feedCopyright 2010bruno.baia@springframework.net{0}Atom10FeedHttpMessageConverterTests.Write", now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture).Remove(29, 1)); - IHttpInputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8); SyndicationFeed result = converter.Read(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("Test Feedhttp://www.springframework.net/FeedThis is a test feedCopyright 2010bruno.baia@springframework.net{0}Atom10FeedHttpMessageConverterTests.Write", @@ -112,22 +100,13 @@ namespace Spring.Http.Converters.Feed body.Authors.Add(sp); body.Copyright = new TextSyndicationContent("Copyright 2010"); - IHttpOutputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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(); } } } diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/FileInfoHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/FileInfoHttpMessageConverterTests.cs new file mode 100644 index 00000000..b9c9b33c --- /dev/null +++ b/test/Spring/Spring.Http.Tests/Http/Converters/FileInfoHttpMessageConverterTests.cs @@ -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 +{ + /// + /// Unit tests for the FileInfoHttpMessageConverter class. + /// + /// Bruno Baia + [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"); + } + } +} diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/FormHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/FormHttpMessageConverterTests.cs index 22ed1ce6..4690593e 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/FormHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/FormHttpMessageConverterTests.cs @@ -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(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - HttpHeaders headers = new HttpHeaders(); - headers.ContentType = mediaType; - Expect.Call(message.Headers).Return(headers).Repeat.Any(); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, charSetEncoding); + message.Headers.ContentType = mediaType; NameValueCollection result = converter.Read(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(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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 parts = new Dictionary(); 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(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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\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\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"); } } } diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/Json/JsonHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/Json/JsonHttpMessageConverterTests.cs index 6007aff8..f0cd87cf 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Json/JsonHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Json/JsonHttpMessageConverterTests.cs @@ -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(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8); CustomClass result = converter.Read(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(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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 diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/StringHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/StringHttpMessageConverterTests.cs index 67a3c7ad..f26a3221 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/StringHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/StringHttpMessageConverterTests.cs @@ -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(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - HttpHeaders headers = new HttpHeaders(); - headers.ContentType = mediaType; - Expect.Call(message.Headers).Return(headers).Repeat.Any(); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, charSetEncoding); + message.Headers.ContentType = mediaType; string result = converter.Read(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(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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(); } } } diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/DataContractHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/DataContractHttpMessageConverterTests.cs index dd01c298..aef4bbf0 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/DataContractHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/DataContractHttpMessageConverterTests.cs @@ -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 1Bruno Baïa "; - IHttpInputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8); DataContractClass result = converter.Read(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 = "1Bruno Baïa"; DataContractClass body = new DataContractClass("1", "Bruno Baïa"); - IHttpOutputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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 diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XElementHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XElementHttpMessageConverterTests.cs index b4fc6458..92cb0cde 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XElementHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XElementHttpMessageConverterTests.cs @@ -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 = ""; - IHttpInputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8); XElement result = converter.Read(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(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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(); } } } diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlDocumentHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlDocumentHttpMessageConverterTests.cs index 5e94ea0f..7f90143d 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlDocumentHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlDocumentHttpMessageConverterTests.cs @@ -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 = ""; - IHttpInputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8); XmlDocument result = converter.Read(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(""); - IHttpOutputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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(); } } } diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlSerializableHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlSerializableHttpMessageConverterTests.cs index d79cda48..bd7afba1 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlSerializableHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlSerializableHttpMessageConverterTests.cs @@ -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 Bruno Baïa "; - IHttpInputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - - mocks.ReplayAll(); + MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8); CustomClass result = converter.Read(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 = "1Bruno Baïa"; CustomClass body = new CustomClass("1", "Bruno Baïa"); - IHttpOutputMessage message = mocks.CreateMock(); - Expect.Call(message.Body).PropertyBehavior(); - HttpHeaders headers = new HttpHeaders(); - Expect.Call(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 diff --git a/test/Spring/Spring.Http.Tests/Http/MockHttpInputMessage.cs b/test/Spring/Spring.Http.Tests/Http/MockHttpInputMessage.cs new file mode 100644 index 00000000..8832292a --- /dev/null +++ b/test/Spring/Spring.Http.Tests/Http/MockHttpInputMessage.cs @@ -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 +{ + /// + /// Mocked IHttpInputMessage implementation. + /// + /// Arjen Poutsma + /// Bruno Baia (.NET) + 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; } + } + } +} diff --git a/test/Spring/Spring.Http.Tests/Http/MockHttpOuputMessage.cs b/test/Spring/Spring.Http.Tests/Http/MockHttpOuputMessage.cs new file mode 100644 index 00000000..2a397ad0 --- /dev/null +++ b/test/Spring/Spring.Http.Tests/Http/MockHttpOuputMessage.cs @@ -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 +{ + /// + /// Mocked IHttpOutputMessage implementation. + /// + /// Arjen Poutsma + /// Bruno Baia (.NET) + public class MockHttpOutputMessage : IHttpOutputMessage + { + private HttpHeaders headers; + private Action body; + private byte[] bodyAsBytes; + + public MockHttpOutputMessage() + { + this.headers = new HttpHeaders(); + } + + public HttpHeaders Headers + { + get { return this.headers; } + } + + public Action 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); + } + } +} diff --git a/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2005.csproj b/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2005.csproj index 16757b81..9939d01f 100644 --- a/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2005.csproj +++ b/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2005.csproj @@ -91,6 +91,7 @@ + @@ -103,8 +104,10 @@ + + diff --git a/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2008.csproj b/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2008.csproj index cb1b70a9..e2175e11 100644 --- a/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2008.csproj +++ b/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2008.csproj @@ -107,6 +107,7 @@ + @@ -121,8 +122,10 @@ + + diff --git a/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2010.csproj b/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2010.csproj index f8f7bd38..843474a3 100644 --- a/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2010.csproj +++ b/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2010.csproj @@ -125,6 +125,7 @@ Code + Code @@ -138,6 +139,8 @@ + +