diff --git a/src/Spring/Spring.Http/Http/Converters/AbstractHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/AbstractHttpMessageConverter.cs index f4e253dc..991f415d 100644 --- a/src/Spring/Spring.Http/Http/Converters/AbstractHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/AbstractHttpMessageConverter.cs @@ -258,7 +258,7 @@ namespace Spring.Http.Converters #region Inner class definitions - // TODO : Move this class + // TODO : Move this class ? internal class IgnoreCloseMemoryStream : MemoryStream { public IgnoreCloseMemoryStream() @@ -274,15 +274,13 @@ namespace Spring.Http.Converters { this.Position = 0; - int bufferSize = 65536; - byte[] buffer = new byte[bufferSize]; - + // From .NET 4.0 Stream.CopyTo method int bytesCount; - while ((bytesCount = this.Read(buffer, 0, buffer.Length)) > 0) + byte[] buffer = new byte[0x1000]; + while ((bytesCount = this.Read(buffer, 0, buffer.Length)) != 0) { dest.Write(buffer, 0, bytesCount); } - dest.Flush(); base.Close(); } diff --git a/src/Spring/Spring.Http/Http/Converters/ByteArrayHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/ByteArrayHttpMessageConverter.cs index 90c5d9a4..6ae36d10 100644 --- a/src/Spring/Spring.Http/Http/Converters/ByteArrayHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/ByteArrayHttpMessageConverter.cs @@ -21,7 +21,6 @@ using System; using System.IO; using System.Net; -using System.Text; namespace Spring.Http.Converters { diff --git a/src/Spring/Spring.Http/Http/Converters/Feed/Atom10FeedHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/Feed/Atom10FeedHttpMessageConverter.cs index ee8246b6..9c31e6f9 100644 --- a/src/Spring/Spring.Http/Http/Converters/Feed/Atom10FeedHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/Feed/Atom10FeedHttpMessageConverter.cs @@ -19,7 +19,6 @@ #endregion -using System; using System.Net; using System.Xml; using System.ServiceModel.Syndication; diff --git a/src/Spring/Spring.Http/Http/Converters/Feed/Rss20FeedHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/Feed/Rss20FeedHttpMessageConverter.cs index 8cb356ab..06bc1fef 100644 --- a/src/Spring/Spring.Http/Http/Converters/Feed/Rss20FeedHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/Feed/Rss20FeedHttpMessageConverter.cs @@ -19,7 +19,6 @@ #endregion -using System; using System.Net; using System.Xml; using System.ServiceModel.Syndication; diff --git a/src/Spring/Spring.Http/Http/Converters/Json/JsonHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/Json/JsonHttpMessageConverter.cs index ccdae667..5ab87c99 100644 --- a/src/Spring/Spring.Http/Http/Converters/Json/JsonHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/Json/JsonHttpMessageConverter.cs @@ -31,7 +31,6 @@ using Spring.Util; namespace Spring.Http.Converters.Json { // TODO : Support for known types, etc... - // TODO : Fix Write method /// /// Implementation of that can read and write JSON. @@ -90,8 +89,8 @@ namespace Spring.Http.Converters.Json protected override void WriteInternal(object content, HttpWebRequest request) { // Get the request encoding - MediaType mediaType = MediaType.ParseMediaType(request.ContentType); Encoding encoding; + MediaType mediaType = MediaType.ParseMediaType(request.ContentType); if (mediaType == null || !StringUtils.HasText(mediaType.CharSet)) { encoding = DEFAULT_CHARSET; @@ -109,13 +108,15 @@ namespace Spring.Http.Converters.Json using (XmlDictionaryWriter jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(requestStream, encoding, false)) { serializer.WriteObject(jsonWriter, content); - jsonWriter.Flush(); } // Set the content length in the request headers request.ContentLength = requestStream.Length; - requestStream.CopyToAndClose(request.GetRequestStream()); + using (Stream postStream = request.GetRequestStream()) + { + requestStream.CopyToAndClose(postStream); + } } } } diff --git a/src/Spring/Spring.Http/Http/Converters/StringHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/StringHttpMessageConverter.cs index 1c423a14..a75dedb4 100644 --- a/src/Spring/Spring.Http/Http/Converters/StringHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/StringHttpMessageConverter.cs @@ -73,13 +73,14 @@ namespace Spring.Http.Converters { // Get the response encoding Encoding encoding; - if (!StringUtils.HasText(response.CharacterSet)) + MediaType mediaType = MediaType.ParseMediaType(response.ContentType); + if (mediaType == null || !StringUtils.HasText(mediaType.CharSet)) { encoding = DEFAULT_CHARSET; } else { - encoding = Encoding.GetEncoding(response.CharacterSet); + encoding = Encoding.GetEncoding(mediaType.CharSet); } // Get the response stream diff --git a/src/Spring/Spring.Http/Http/Converters/UrlEncodedFormHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/UrlEncodedFormHttpMessageConverter.cs new file mode 100644 index 00000000..7a0034d9 --- /dev/null +++ b/src/Spring/Spring.Http/Http/Converters/UrlEncodedFormHttpMessageConverter.cs @@ -0,0 +1,181 @@ +#region License + +/* + * Copyright 2002-2010 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.Net; +using System.Web; +using System.Text; +using System.Collections.Specialized; + +using Spring.Util; + +namespace Spring.Http.Converters +{ + /// + /// Implementation of that can handle form data, + /// including multipart form data (i.e. file uploads). + /// + /// + /// + /// This converter supports the 'application/x-www-form-urlencoded' media type. + /// + /// + /// For example, the following snippet shows how to submit an HTML form: + /// + /// RestTemplate template = new RestTemplate(); // UrlEncodedFormHttpMessageConverter is configured by default + /// NameValueCollection form = new NameValueCollection(); + /// form.Add("field 1", "value 1"); + /// form.Add("field 2", "value 2"); + /// form.Add("field 2", "value 3"); + /// template.PostForLocation("http://example.com/myForm", form); + /// + /// + /// + /// Arjen Poutsma + /// Bruno Baia (.NET) + public class UrlEncodedFormHttpMessageConverter : AbstractHttpMessageConverter + { + private Encoding charset = Encoding.GetEncoding("ISO-8859-1"); + + /// + /// Sets the encoding used for writing form data. + /// + public Encoding Charset + { + set { charset = value; } + } + + /// + /// Creates a new instance of the + /// with 'application/x-www-form-urlencoded' media type. + /// + public UrlEncodedFormHttpMessageConverter() : + base(MediaType.APPLICATION_FORM_URLENCODED) + { + } + + /// + /// Indicates whether the given class is supported by this converter. + /// + /// The type to test for support. + /// if supported; otherwise + protected override bool Supports(Type type) + { + return type.Equals(typeof(NameValueCollection)); + } + + /// + /// Abstract template method that reads the actualy object. Invoked from . + /// + /// The type of object to return. + /// The HTTP response to read from. + /// The converted object. + protected override T ReadInternal(HttpWebResponse response) + { + // Get the response encoding + Encoding encoding; + MediaType mediaType = MediaType.ParseMediaType(response.ContentType); + if (mediaType == null || !StringUtils.HasText(mediaType.CharSet)) + { + encoding = this.charset; + } + else + { + encoding = Encoding.GetEncoding(mediaType.CharSet); + } + + // Get the response stream + string body; + using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) + { + body = reader.ReadToEnd(); + } + + string[] pairs = body.Split('&'); + NameValueCollection result = new NameValueCollection(pairs.Length); + foreach (string pair in pairs) + { + int idx = pair.IndexOf('='); + if (idx == -1) + { + result.Add(HttpUtility.UrlDecode(pair, this.charset), null); + } + else + { + string name = HttpUtility.UrlDecode(pair.Substring(0, idx), this.charset); + string value = HttpUtility.UrlDecode(pair.Substring(idx + 1), this.charset); + result.Add(name, value); + } + } + return result as T; + } + + /// + /// Abstract template method that writes the actual body. Invoked from . + /// + /// The object to write to the HTTP request. + /// The HTTP request to write to. + protected override void WriteInternal(object content, HttpWebRequest request) + { + StringBuilder builder = new StringBuilder(); + NameValueCollection form = content as NameValueCollection; + for(int i=0; i < form.AllKeys.Length; i++) + { + string name = form.GetKey(i); + string[] values = form.GetValues(name); + if (values == null) + { + builder.Append(HttpUtility.UrlEncode(name, this.charset)); + } + else + { + for (int j = 0; j < values.Length; j++) + { + string value = values[j]; + builder.Append(HttpUtility.UrlEncode(name, this.charset)); + builder.Append('='); + builder.Append(HttpUtility.UrlEncode(value, this.charset)); + if (j != (values.Length - 1)) + { + builder.Append('&'); + } + } + } + if (i != (form.AllKeys.Length - 1)) + { + builder.Append('&'); + } + } + + // Create a byte array of the data we want to send + byte[] byteData = this.charset.GetBytes(builder.ToString()); + + // Set the content length in the request headers + request.ContentLength = byteData.Length; + + // Write to the request + using (Stream postStream = request.GetRequestStream()) + { + postStream.Write(byteData, 0, byteData.Length); + } + } + } +} diff --git a/src/Spring/Spring.Http/Http/Converters/Xml/AbstractXmlHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/Xml/AbstractXmlHttpMessageConverter.cs index 0bdbf57a..99c8a35d 100644 --- a/src/Spring/Spring.Http/Http/Converters/Xml/AbstractXmlHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/Xml/AbstractXmlHttpMessageConverter.cs @@ -40,7 +40,7 @@ namespace Spring.Http.Converters.Xml /// /// Default encoding for XML. /// - public static readonly Encoding DEFAULT_CHARSET = Encoding.UTF8; + public static readonly Encoding DEFAULT_CHARSET = new UTF8Encoding(false); // Remove byte Order Mask (BOM) when using XmlTextWriter private XmlReaderSettings _xmlReaderSettings; @@ -122,13 +122,15 @@ namespace Spring.Http.Converters.Xml using (XmlTextWriter xmlWriter = new XmlTextWriter(requestStream, encoding)) { WriteXml(xmlWriter, content, request); - xmlWriter.Flush(); } // Set the content length in the request headers request.ContentLength = requestStream.Length; - requestStream.CopyToAndClose(request.GetRequestStream()); + using (Stream postStream = request.GetRequestStream()) + { + requestStream.CopyToAndClose(postStream); + } } } diff --git a/src/Spring/Spring.Http/Http/Converters/Xml/DataContractHttpMessageConverter.cs b/src/Spring/Spring.Http/Http/Converters/Xml/DataContractHttpMessageConverter.cs index 00516275..7bebc2b7 100644 --- a/src/Spring/Spring.Http/Http/Converters/Xml/DataContractHttpMessageConverter.cs +++ b/src/Spring/Spring.Http/Http/Converters/Xml/DataContractHttpMessageConverter.cs @@ -20,10 +20,8 @@ #endregion using System; -using System.IO; using System.Net; using System.Xml; -using System.Text; using System.Runtime.Serialization; namespace Spring.Http.Converters.Xml @@ -40,7 +38,7 @@ namespace Spring.Http.Converters.Xml /// This can be overridden by setting the property. /// /// - /// This converter can read classes annotated with and , and write classes + /// This converter can read classes annotated with and , and write classes /// annotated with with {@link XmlRootElement}, or subclasses thereof. /// /// diff --git a/src/Spring/Spring.Http/Http/HttpMethod.cs b/src/Spring/Spring.Http/Http/HttpMethod.cs index 88fc0346..b29497a4 100644 --- a/src/Spring/Spring.Http/Http/HttpMethod.cs +++ b/src/Spring/Spring.Http/Http/HttpMethod.cs @@ -18,8 +18,6 @@ #endregion -using System; - namespace Spring.Http { /// diff --git a/src/Spring/Spring.Http/Http/Rest/IRequestCallback.cs b/src/Spring/Spring.Http/Http/Rest/IRequestCallback.cs index b1e71352..47ceba6b 100644 --- a/src/Spring/Spring.Http/Http/Rest/IRequestCallback.cs +++ b/src/Spring/Spring.Http/Http/Rest/IRequestCallback.cs @@ -18,7 +18,6 @@ #endregion -using System; using System.Net; namespace Spring.Http.Rest diff --git a/src/Spring/Spring.Http/Http/Rest/IResponseExtractor.cs b/src/Spring/Spring.Http/Http/Rest/IResponseExtractor.cs index 98f568b9..fda84882 100644 --- a/src/Spring/Spring.Http/Http/Rest/IResponseExtractor.cs +++ b/src/Spring/Spring.Http/Http/Rest/IResponseExtractor.cs @@ -18,7 +18,6 @@ #endregion -using System; using System.Net; namespace Spring.Http.Rest diff --git a/src/Spring/Spring.Http/Http/Rest/IRestOperations.cs b/src/Spring/Spring.Http/Http/Rest/IRestOperations.cs index abb3d2ad..561c0084 100644 --- a/src/Spring/Spring.Http/Http/Rest/IRestOperations.cs +++ b/src/Spring/Spring.Http/Http/Rest/IRestOperations.cs @@ -22,8 +22,6 @@ using System; using System.Net; using System.Collections.Generic; -using Spring.Http; - namespace Spring.Http.Rest { /// diff --git a/src/Spring/Spring.Http/Http/Rest/RestTemplate.cs b/src/Spring/Spring.Http/Http/Rest/RestTemplate.cs index 79ec4119..8065d3a8 100644 --- a/src/Spring/Spring.Http/Http/Rest/RestTemplate.cs +++ b/src/Spring/Spring.Http/Http/Rest/RestTemplate.cs @@ -19,14 +19,11 @@ #endregion using System; -using System.IO; using System.Net; using System.Collections.Generic; -using Spring.Http; using Spring.Http.Converters; using Spring.Http.Converters.Xml; -using Spring.Http.Converters.Json; using Spring.Http.Converters.Feed; using Spring.Http.Rest.Support; using UriTemplate = Spring.Util.UriTemplate; // UriTemplate in .NET Framework since 3.5 @@ -201,6 +198,7 @@ namespace Spring.Http.Rest this._messageConverters.Add(new ByteArrayHttpMessageConverter()); this._messageConverters.Add(new StringHttpMessageConverter()); + this._messageConverters.Add(new UrlEncodedFormHttpMessageConverter()); //this._messageConverters.Add(new XmlSerializableHttpMessageConverter()); this._messageConverters.Add(new XmlDocumentHttpMessageConverter()); #if NET_3_5 diff --git a/src/Spring/Spring.Http/Http/Rest/Support/AcceptHeaderRequestCallback.cs b/src/Spring/Spring.Http/Http/Rest/Support/AcceptHeaderRequestCallback.cs index af4aa070..36d0b2cc 100644 --- a/src/Spring/Spring.Http/Http/Rest/Support/AcceptHeaderRequestCallback.cs +++ b/src/Spring/Spring.Http/Http/Rest/Support/AcceptHeaderRequestCallback.cs @@ -23,7 +23,6 @@ using System.Net; using System.Collections.Generic; using Spring.Util; -using Spring.Http; using Spring.Http.Converters; namespace Spring.Http.Rest.Support diff --git a/src/Spring/Spring.Http/Http/Rest/Support/HttpMessageRequestCallback.cs b/src/Spring/Spring.Http/Http/Rest/Support/HttpMessageRequestCallback.cs index 8c87df7b..bbd7088d 100644 --- a/src/Spring/Spring.Http/Http/Rest/Support/HttpMessageRequestCallback.cs +++ b/src/Spring/Spring.Http/Http/Rest/Support/HttpMessageRequestCallback.cs @@ -22,7 +22,6 @@ using System; using System.Net; using System.Collections.Generic; -using Spring.Http; using Spring.Http.Converters; namespace Spring.Http.Rest.Support diff --git a/src/Spring/Spring.Http/Http/Rest/Support/HttpMessageResponseExtractor`1.cs b/src/Spring/Spring.Http/Http/Rest/Support/HttpMessageResponseExtractor`1.cs index c8fd8ac0..253d90b0 100644 --- a/src/Spring/Spring.Http/Http/Rest/Support/HttpMessageResponseExtractor`1.cs +++ b/src/Spring/Spring.Http/Http/Rest/Support/HttpMessageResponseExtractor`1.cs @@ -21,7 +21,6 @@ using System.Net; using System.Collections.Generic; -using Spring.Http; using Spring.Http.Converters; namespace Spring.Http.Rest.Support diff --git a/src/Spring/Spring.Http/Http/Rest/Support/MessageConverterResponseExtractor.cs b/src/Spring/Spring.Http/Http/Rest/Support/MessageConverterResponseExtractor.cs index db04dca8..b0118285 100644 --- a/src/Spring/Spring.Http/Http/Rest/Support/MessageConverterResponseExtractor.cs +++ b/src/Spring/Spring.Http/Http/Rest/Support/MessageConverterResponseExtractor.cs @@ -23,7 +23,6 @@ using System.Net; using System.Collections.Generic; using Spring.Util; -using Spring.Http; using Spring.Http.Converters; namespace Spring.Http.Rest.Support diff --git a/src/Spring/Spring.Http/Http/Rest/Support/MethodRequestCallback.cs b/src/Spring/Spring.Http/Http/Rest/Support/MethodRequestCallback.cs index 7dbdcde1..d12ffa8d 100644 --- a/src/Spring/Spring.Http/Http/Rest/Support/MethodRequestCallback.cs +++ b/src/Spring/Spring.Http/Http/Rest/Support/MethodRequestCallback.cs @@ -21,8 +21,6 @@ using System; using System.Net; -using Spring.Http; - namespace Spring.Http.Rest.Support { /// diff --git a/src/Spring/Spring.Http/Spring.Http.2008.csproj b/src/Spring/Spring.Http/Spring.Http.2008.csproj index feaa0a0a..ae50e1a8 100644 --- a/src/Spring/Spring.Http/Spring.Http.2008.csproj +++ b/src/Spring/Spring.Http/Spring.Http.2008.csproj @@ -87,6 +87,7 @@ 3.5 + 3.5 @@ -98,6 +99,7 @@ Code + diff --git a/src/Spring/Spring.Http/Spring.Http.2010.csproj b/src/Spring/Spring.Http/Spring.Http.2010.csproj index 81f4c29b..ea085ae6 100644 --- a/src/Spring/Spring.Http/Spring.Http.2010.csproj +++ b/src/Spring/Spring.Http/Spring.Http.2010.csproj @@ -102,6 +102,7 @@ + @@ -112,6 +113,7 @@ + diff --git a/src/Spring/Spring.Http/Util/UriTemplate.cs b/src/Spring/Spring.Http/Util/UriTemplate.cs index 1c048667..f71e1cf6 100644 --- a/src/Spring/Spring.Http/Util/UriTemplate.cs +++ b/src/Spring/Spring.Http/Util/UriTemplate.cs @@ -72,17 +72,17 @@ namespace Spring.Util } /// - /// Given the dictionary of variables, expands this template into a full URI. + /// Given the dictionary of variables, expands this template into a full URI. /// The dictionary keys represent variable names, the dicitonary values variable values. /// The order of variables is not significant. /// /// - /// - /// UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); - /// IDictionary<string, string> uriVariables = new Dictionary<String, String>(); - /// uriVariables.Add("booking", "42"); - /// uriVariables.Add("hotel", "1"); - /// Console.Out.WriteLine(template.Expand(uriVariables)); + /// + /// UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + /// IDictionary<string, string> uriVariables = new Dictionary<String, String>(); + /// uriVariables.Add("booking", "42"); + /// uriVariables.Add("hotel", "1"); + /// Console.Out.WriteLine(template.Expand(uriVariables)); /// /// will print:
http://example.com/hotels/1/bookings/42
///
@@ -241,7 +241,7 @@ namespace Spring.Util public Parser(string uriTemplate) { - AssertUtils.ArgumentHasText(uriTemplate, "'uriTemplate' must not be null"); + AssertUtils.ArgumentNotNull(uriTemplate, "'uriTemplate' must not be null"); int index = 0; this.patternBuilder.Append("^"); diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/ByteArrayHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/ByteArrayHttpMessageConverterTests.cs index 2878a36a..9e5e1c4b 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/ByteArrayHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/ByteArrayHttpMessageConverterTests.cs @@ -18,7 +18,6 @@ #endregion -using System; using System.IO; using System.Net; @@ -50,17 +49,17 @@ namespace Spring.Http.Converters { Assert.IsTrue(converter.CanRead(typeof(byte[]), new MediaType("application", "octet-stream"))); Assert.IsTrue(converter.CanRead(typeof(byte[]), new MediaType("application", "xml"))); - Assert.IsTrue(converter.CanWrite(typeof(byte[]), MediaType.ALL)); + Assert.IsTrue(converter.CanRead(typeof(byte[]), MediaType.ALL)); Assert.IsFalse(converter.CanRead(typeof(string), new MediaType("application", "octet-stream"))); } [Test] public void CanWrite() { - Assert.IsTrue(converter.CanRead(typeof(byte[]), new MediaType("application", "octet-stream"))); - Assert.IsTrue(converter.CanRead(typeof(byte[]), new MediaType("application", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(byte[]), new MediaType("application", "octet-stream"))); + Assert.IsTrue(converter.CanWrite(typeof(byte[]), new MediaType("application", "xml"))); Assert.IsTrue(converter.CanWrite(typeof(byte[]), MediaType.ALL)); - Assert.IsFalse(converter.CanRead(typeof(string), new MediaType("application", "octet-stream"))); + Assert.IsFalse(converter.CanWrite(typeof(string), new MediaType("application", "octet-stream"))); } [Test] 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 f4ef15c2..2ec42e06 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Atom10FeedHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Atom10FeedHttpMessageConverterTests.cs @@ -62,11 +62,11 @@ namespace Spring.Http.Converters.Feed [Test] public void CanWrite() { - Assert.IsTrue(converter.CanRead(typeof(SyndicationFeed), new MediaType("application", "atom+xml"))); - Assert.IsTrue(converter.CanRead(typeof(SyndicationItem), new MediaType("application", "atom+xml"))); - Assert.IsTrue(converter.CanRead(typeof(SyndicationFeed), new MediaType("text", "xml"))); - Assert.IsFalse(converter.CanRead(typeof(string), new MediaType("application", "atom+xml"))); - Assert.IsFalse(converter.CanRead(typeof(SyndicationFeed), new MediaType("text", "plain"))); + Assert.IsTrue(converter.CanWrite(typeof(SyndicationFeed), new MediaType("application", "atom+xml"))); + Assert.IsTrue(converter.CanWrite(typeof(SyndicationItem), new MediaType("application", "atom+xml"))); + Assert.IsTrue(converter.CanWrite(typeof(SyndicationFeed), new MediaType("text", "xml"))); + Assert.IsFalse(converter.CanWrite(typeof(string), new MediaType("application", "atom+xml"))); + Assert.IsFalse(converter.CanWrite(typeof(SyndicationFeed), new MediaType("text", "plain"))); } [Test] @@ -74,7 +74,7 @@ namespace Spring.Http.Converters.Feed { DateTime now = DateTime.Now; - string body = String.Format("Test FeedThis is a test feedAtom10FeedHttpMessageConverterTests.WriteCopyright 2010{0}Bruno Baïahttp://www.springframework.net/bbaiabruno.baia@springframework.net", + 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)); HttpWebResponse webResponse = mocks.CreateMock(); @@ -105,7 +105,7 @@ namespace Spring.Http.Converters.Feed 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", + string expectedBody = 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)); SyndicationFeed body = new SyndicationFeed("Test Feed", "This is a test feed", new Uri("http://www.springframework.net/Feed"), "Atom10FeedHttpMessageConverterTests.Write", now); 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 83776525..85bad240 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Rss20FeedHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Feed/Rss20FeedHttpMessageConverterTests.cs @@ -62,12 +62,12 @@ namespace Spring.Http.Converters.Feed [Test] public void CanWrite() { - Assert.IsTrue(converter.CanRead(typeof(SyndicationFeed), new MediaType("application", "rss+xml"))); - Assert.IsTrue(converter.CanRead(typeof(SyndicationItem), new MediaType("application", "rss+xml"))); - Assert.IsTrue(converter.CanRead(typeof(SyndicationFeed), new MediaType("application", "xml"))); - Assert.IsTrue(converter.CanRead(typeof(SyndicationFeed), new MediaType("text", "xml"))); - Assert.IsFalse(converter.CanRead(typeof(string), new MediaType("application", "rss+xml"))); - Assert.IsFalse(converter.CanRead(typeof(SyndicationFeed), new MediaType("text", "plain"))); + Assert.IsTrue(converter.CanWrite(typeof(SyndicationFeed), new MediaType("application", "rss+xml"))); + Assert.IsTrue(converter.CanWrite(typeof(SyndicationItem), new MediaType("application", "rss+xml"))); + Assert.IsTrue(converter.CanWrite(typeof(SyndicationFeed), new MediaType("application", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(SyndicationFeed), new MediaType("text", "xml"))); + Assert.IsFalse(converter.CanWrite(typeof(string), new MediaType("application", "rss+xml"))); + Assert.IsFalse(converter.CanWrite(typeof(SyndicationFeed), new MediaType("text", "plain"))); } [Test] @@ -75,7 +75,7 @@ namespace Spring.Http.Converters.Feed { DateTime now = DateTime.Now; - string body = String.Format("Test Feedhttp://www.springframework.net/FeedThis is a test feedCopyright 2010bruno.baia@springframework.net{0}Atom10FeedHttpMessageConverterTests.Write", + 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)); HttpWebResponse webResponse = mocks.CreateMock(); @@ -104,7 +104,7 @@ namespace Spring.Http.Converters.Feed 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", + string expectedBody = 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)); SyndicationFeed body = new SyndicationFeed("Test Feed", "This is a test feed", new Uri("http://www.springframework.net/Feed"), "Atom10FeedHttpMessageConverterTests.Write", now); 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 51d9f948..057eee09 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Json/JsonHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Json/JsonHttpMessageConverterTests.cs @@ -55,8 +55,8 @@ namespace Spring.Http.Converters.Json [Test] public void CanWrite() { - Assert.IsTrue(converter.CanRead(typeof(CustomClass), new MediaType("application", "json"))); - Assert.IsFalse(converter.CanRead(typeof(CustomClass), new MediaType("text", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(CustomClass), new MediaType("application", "json"))); + Assert.IsFalse(converter.CanWrite(typeof(CustomClass), new MediaType("text", "xml"))); } [Test] diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/StringHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/StringHttpMessageConverterTests.cs index 7762a1c7..684c29a4 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/StringHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/StringHttpMessageConverterTests.cs @@ -18,7 +18,6 @@ #endregion -using System; using System.IO; using System.Net; using System.Text; @@ -50,7 +49,7 @@ namespace Spring.Http.Converters public void CanRead() { Assert.IsTrue(converter.CanRead(typeof(string), new MediaType("text", "plain"))); - Assert.IsTrue(converter.CanWrite(typeof(string), MediaType.ALL)); + Assert.IsTrue(converter.CanRead(typeof(string), MediaType.ALL)); Assert.IsTrue(converter.CanRead(typeof(string), new MediaType("application", "xml"))); Assert.IsFalse(converter.CanRead(typeof(int[]), new MediaType("text", "plain"))); } @@ -58,20 +57,23 @@ namespace Spring.Http.Converters [Test] public void CanWrite() { - Assert.IsTrue(converter.CanRead(typeof(string), new MediaType("text", "plain"))); + Assert.IsTrue(converter.CanWrite(typeof(string), new MediaType("text", "plain"))); Assert.IsTrue(converter.CanWrite(typeof(string), MediaType.ALL)); - Assert.IsTrue(converter.CanRead(typeof(string), new MediaType("application", "xml"))); - Assert.IsFalse(converter.CanRead(typeof(int[]), new MediaType("text", "plain"))); + Assert.IsTrue(converter.CanWrite(typeof(string), new MediaType("application", "xml"))); + Assert.IsFalse(converter.CanWrite(typeof(int[]), new MediaType("text", "plain"))); } [Test] public void Read() { string body = "Hello Bruno Baïa"; + string charSet = "utf-8"; + Encoding charSetEncoding = Encoding.GetEncoding(charSet); + MediaType mediaType = new MediaType("text", "plain", charSet); HttpWebResponse webResponse = mocks.CreateMock(); Expect.Call(webResponse.GetResponseStream()).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); - Expect.Call(webResponse.CharacterSet).Return("utf-8").Repeat.Twice(); + Expect.Call(webResponse.ContentType).Return(mediaType.ToString()); mocks.ReplayAll(); @@ -89,9 +91,10 @@ namespace Spring.Http.Converters string body = "H\u00e9llo W\u00f6rld"; string charSet = "ISO-8859-1"; Encoding charSetEncoding = Encoding.GetEncoding(charSet); + MediaType mediaType = new MediaType("text", "plain", charSet); HttpWebRequest webRequest = mocks.CreateMock(); - Expect.Call(webRequest.ContentType = "text/plain;charset=ISO-8859-1").PropertyBehavior(); + Expect.Call(webRequest.ContentType = mediaType.ToString()).PropertyBehavior(); Expect.Call(webRequest.ContentLength = 1337).PropertyBehavior(); Expect.Call(webRequest.GetRequestStream()).Return(requestStream); diff --git a/test/Spring/Spring.Http.Tests/Http/Converters/UrlEncodedFormHttpMessageConverterTests.cs b/test/Spring/Spring.Http.Tests/Http/Converters/UrlEncodedFormHttpMessageConverterTests.cs new file mode 100644 index 00000000..c96c74d1 --- /dev/null +++ b/test/Spring/Spring.Http.Tests/Http/Converters/UrlEncodedFormHttpMessageConverterTests.cs @@ -0,0 +1,122 @@ +#region License + +/* + * Copyright 2002-2010 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.Net; +using System.Text; +using System.Collections.Specialized; + +using NUnit.Framework; +using Rhino.Mocks; + +namespace Spring.Http.Converters +{ + /// + /// Unit tests for the UrlEncodedFormHttpMessageConverter class. + /// + /// Arjen Poutsma + /// Bruno Baia (.NET) + [TestFixture] + public class UrlEncodedFormHttpMessageConverterTests + { + private UrlEncodedFormHttpMessageConverter converter; + private MockRepository mocks; + + [SetUp] + public void SetUp() + { + mocks = new MockRepository(); + converter = new UrlEncodedFormHttpMessageConverter(); + } + + [Test] + public void CanRead() + { + Assert.IsTrue(converter.CanRead(typeof(NameValueCollection), new MediaType("application", "x-www-form-urlencoded"))); + Assert.IsFalse(converter.CanRead(typeof(NameValueCollection), new MediaType("application", "xml"))); + Assert.IsFalse(converter.CanRead(typeof(string), new MediaType("application", "x-www-form-urlencoded"))); + } + + [Test] + public void CanWrite() + { + Assert.IsTrue(converter.CanWrite(typeof(NameValueCollection), new MediaType("application", "x-www-form-urlencoded"))); + Assert.IsFalse(converter.CanWrite(typeof(NameValueCollection), new MediaType("application", "xml"))); + Assert.IsFalse(converter.CanWrite(typeof(string), new MediaType("application", "x-www-form-urlencoded"))); + } + + [Test] + public void Read() + { + String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"; + string charSet = "ISO-8859-1"; + Encoding charSetEncoding = Encoding.GetEncoding(charSet); + MediaType mediaType = new MediaType("application", "x-www-form-urlencoded", charSet); + + HttpWebResponse webResponse = mocks.CreateMock(); + Expect.Call(webResponse.GetResponseStream()).Return(new MemoryStream(Encoding.UTF8.GetBytes(body))); + Expect.Call(webResponse.ContentType).Return(mediaType.ToString()); + + mocks.ReplayAll(); + + NameValueCollection result = converter.Read(webResponse); + Assert.AreEqual(3, result.Count, "Invalid result"); + Assert.AreEqual(1, result.GetValues(0).Length, "Invalid result"); + Assert.AreEqual("value 1", result.GetValues(0)[0], "Invalid result"); + Assert.AreEqual(2, result.GetValues("name 2").Length, "Invalid result"); + 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 Write() + { + 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"); + body.Add("name 2", "value 2+1"); + body.Add("name 2", "value 2+2"); + body.Add("name 3", null); + string charSet = "ISO-8859-1"; + Encoding charSetEncoding = Encoding.GetEncoding(charSet); + MediaType mediaType = new MediaType("application", "x-www-form-urlencoded", charSet); + + HttpWebRequest webRequest = mocks.CreateMock(); + Expect.Call(webRequest.ContentType = mediaType.ToString()).PropertyBehavior(); + Expect.Call(webRequest.ContentLength = 1337).PropertyBehavior(); + Expect.Call(webRequest.GetRequestStream()).Return(requestStream); + + mocks.ReplayAll(); + + converter.Write(body, null, webRequest); + + byte[] result = requestStream.ToArray(); + Assert.AreEqual(expectedBody, charSetEncoding.GetString(result), "Invalid result"); + + 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 c6a276bf..a9a51fc3 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/DataContractHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/DataContractHttpMessageConverterTests.cs @@ -63,10 +63,10 @@ namespace Spring.Http.Converters.Xml { Assert.IsTrue(converter.CanWrite(typeof(DataContractClass), new MediaType("application", "xml"))); Assert.IsTrue(converter.CanWrite(typeof(DataContractClass), new MediaType("text", "xml"))); - Assert.IsTrue(converter.CanRead(typeof(DataContractClass), new MediaType("application", "soap+xml"))); // application/*+xml - Assert.IsFalse(converter.CanRead(typeof(DataContractClass), new MediaType("text", "plain"))); - Assert.IsFalse(converter.CanRead(typeof(NonDataContractClass), new MediaType("application", "xml"))); - Assert.IsTrue(converter.CanRead(typeof(CollectionDataContractClass), new MediaType("application", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(DataContractClass), new MediaType("application", "soap+xml"))); // application/*+xml + Assert.IsFalse(converter.CanWrite(typeof(DataContractClass), new MediaType("text", "plain"))); + Assert.IsFalse(converter.CanWrite(typeof(NonDataContractClass), new MediaType("application", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(CollectionDataContractClass), new MediaType("application", "xml"))); } [Test] @@ -107,12 +107,8 @@ namespace Spring.Http.Converters.Xml converter.Write(body, null, webRequest); - requestStream.Position = 0; - using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8)) - { - string result = reader.ReadToEnd(); - Assert.AreEqual(expectedBody, result, "Invalid result"); - } + byte[] result = requestStream.ToArray(); + Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result"); mocks.VerifyAll(); } 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 0d377637..5e45edf1 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XElementHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XElementHttpMessageConverterTests.cs @@ -63,9 +63,9 @@ namespace Spring.Http.Converters.Xml { Assert.IsTrue(converter.CanWrite(typeof(XElement), new MediaType("application", "xml"))); Assert.IsTrue(converter.CanWrite(typeof(XElement), new MediaType("text", "xml"))); - Assert.IsTrue(converter.CanRead(typeof(XElement), new MediaType("application", "soap+xml"))); // application/*+xml - Assert.IsFalse(converter.CanRead(typeof(XElement), new MediaType("text", "plain"))); - Assert.IsFalse(converter.CanRead(typeof(String), new MediaType("application", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(XElement), new MediaType("application", "soap+xml"))); // application/*+xml + Assert.IsFalse(converter.CanWrite(typeof(XElement), new MediaType("text", "plain"))); + Assert.IsFalse(converter.CanWrite(typeof(String), new MediaType("application", "xml"))); } [Test] @@ -110,12 +110,8 @@ namespace Spring.Http.Converters.Xml converter.Write(body, null, webRequest); - requestStream.Position = 0; - using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8)) - { - string result = reader.ReadToEnd(); - Assert.AreEqual(body.ToString(SaveOptions.DisableFormatting), result, "Invalid result"); - } + byte[] result = requestStream.ToArray(); + Assert.AreEqual(body.ToString(SaveOptions.DisableFormatting), Encoding.UTF8.GetString(result), "Invalid result"); 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 65f04042..440b307f 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlDocumentHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlDocumentHttpMessageConverterTests.cs @@ -61,9 +61,9 @@ namespace Spring.Http.Converters.Xml { Assert.IsTrue(converter.CanWrite(typeof(XmlDocument), new MediaType("application", "xml"))); Assert.IsTrue(converter.CanWrite(typeof(XmlDocument), new MediaType("text", "xml"))); - Assert.IsTrue(converter.CanRead(typeof(XmlDocument), new MediaType("application", "soap+xml"))); // application/*+xml - Assert.IsFalse(converter.CanRead(typeof(XmlDocument), new MediaType("text", "plain"))); - Assert.IsFalse(converter.CanRead(typeof(String), new MediaType("application", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(XmlDocument), new MediaType("application", "soap+xml"))); // application/*+xml + Assert.IsFalse(converter.CanWrite(typeof(XmlDocument), new MediaType("text", "plain"))); + Assert.IsFalse(converter.CanWrite(typeof(String), new MediaType("application", "xml"))); } [Test] @@ -102,14 +102,10 @@ namespace Spring.Http.Converters.Xml mocks.ReplayAll(); - converter.Write(body, null, webRequest); + converter.Write(body, null, webRequest); - requestStream.Position = 0; - using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8)) - { - string result = reader.ReadToEnd(); - Assert.AreEqual(body.OuterXml, result, "Invalid result"); - } + byte[] result = requestStream.ToArray(); + Assert.AreEqual(body.OuterXml, Encoding.UTF8.GetString(result), "Invalid result"); 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 4c60787a..8915b6d5 100644 --- a/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlSerializableHttpMessageConverterTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Converters/Xml/XmlSerializableHttpMessageConverterTests.cs @@ -57,10 +57,10 @@ namespace Spring.Http.Converters.Xml [Test] public void CanWrite() { - Assert.IsTrue(converter.CanRead(typeof(CustomClass), new MediaType("application", "xml"))); - Assert.IsTrue(converter.CanRead(typeof(CustomClass), new MediaType("text", "xml"))); - Assert.IsTrue(converter.CanRead(typeof(CustomClass), new MediaType("application", "soap+xml"))); // application/*+xml - Assert.IsFalse(converter.CanRead(typeof(CustomClass), new MediaType("text", "plain"))); + Assert.IsTrue(converter.CanWrite(typeof(CustomClass), new MediaType("application", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(CustomClass), new MediaType("text", "xml"))); + Assert.IsTrue(converter.CanWrite(typeof(CustomClass), new MediaType("application", "soap+xml"))); // application/*+xml + Assert.IsFalse(converter.CanWrite(typeof(CustomClass), new MediaType("text", "plain"))); } [Test] @@ -102,12 +102,8 @@ namespace Spring.Http.Converters.Xml converter.Write(body, null, webRequest); - requestStream.Position = 0; - using (StreamReader reader = new StreamReader(requestStream, Encoding.UTF8)) - { - string result = reader.ReadToEnd(); - Assert.AreEqual(expectedBody, result, "Invalid result"); - } + byte[] result = requestStream.ToArray(); + Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result"); mocks.VerifyAll(); } diff --git a/test/Spring/Spring.Http.Tests/Http/Rest/RestTemplateIntegrationTests.cs b/test/Spring/Spring.Http.Tests/Http/Rest/RestTemplateIntegrationTests.cs index 11ca3765..f6c3a397 100644 --- a/test/Spring/Spring.Http.Tests/Http/Rest/RestTemplateIntegrationTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Rest/RestTemplateIntegrationTests.cs @@ -28,9 +28,6 @@ using System.ServiceModel; using System.ServiceModel.Web; using System.ServiceModel.Channels; -using Spring.Http; -using Spring.Http.Rest.Support; - using NUnit.Framework; namespace Spring.Http.Rest @@ -243,98 +240,6 @@ namespace Spring.Http.Rest template.Execute("servererror", null, null); } - - //@BeforeClass - //public static void startJettyServer() throws Exception { - // jettyServer = new Server(8889); - // Context jettyContext = new Context(jettyServer, "/"); - // byte[] bytes = helloWorld.getBytes("UTF-8"); - // contentType = new MediaType("text", "plain", Collections.singletonMap("charset", "utf-8")); - // jettyContext.addServlet(new ServletHolder(new GetServlet(bytes, contentType)), "/get"); - // jettyContext.addServlet(new ServletHolder(new GetServlet(new byte[0], contentType)), "/get/nothing"); - // jettyContext.addServlet( - // new ServletHolder(new PostServlet(helloWorld, URI + "/post/1", bytes, contentType)), - // "/post"); - // jettyContext.addServlet(new ServletHolder(new ErrorServlet(404)), "/errors/notfound"); - // jettyContext.addServlet(new ServletHolder(new ErrorServlet(500)), "/errors/server"); - // jettyContext.addServlet(new ServletHolder(new UriServlet()), "/uri/*"); - // jettyContext.addServlet(new ServletHolder(new MultipartServlet()), "/multipart"); - // jettyServer.start(); - //} - - //@Test - //public void uri() throws InterruptedException, URISyntaxException { - // String result = template.getForObject(URI + "/uri/{query}", String.class, "Z\u00fcrich"); - // Assert.AreEqual("Invalid request URI", "/uri/Z%C3%BCrich", result); - - // result = template.getForObject(URI + "/uri/query={query}", String.class, "foo@bar"); - // Assert.AreEqual("Invalid request URI", "/uri/query=foo@bar", result); - - // result = template.getForObject(URI + "/uri/query={query}", String.class, "T\u014dky\u014d"); - // Assert.AreEqual("Invalid request URI", "/uri/query=T%C5%8Dky%C5%8D", result); - //} - - //@Test - //public void multipart() throws UnsupportedEncodingException { - // MultiValueMap parts = new LinkedMultiValueMap(); - // parts.add("name 1", "value 1"); - // parts.add("name 2", "value 2+1"); - // parts.add("name 2", "value 2+2"); - // Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); - // parts.add("logo", logo); - - // template.postForLocation(URI + "/multipart", parts); - //} - - - //private static class UriServlet extends HttpServlet { - - // @Override - // protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - // resp.setContentType("text/plain"); - // resp.setCharacterEncoding("UTF-8"); - // resp.getWriter().write(req.getRequestURI()); - // } - //} - - //private static class MultipartServlet extends HttpServlet { - - // @Override - // protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - // assertTrue(ServletFileUpload.isMultipartContent(req)); - // FileItemFactory factory = new DiskFileItemFactory(); - // ServletFileUpload upload = new ServletFileUpload(factory); - // try { - // List items = upload.parseRequest(req); - // Assert.AreEqual(4, items.size()); - // FileItem item = (FileItem) items.get(0); - // assertTrue(item.isFormField()); - // Assert.AreEqual("name 1", item.getFieldName()); - // Assert.AreEqual("value 1", item.getString()); - - // item = (FileItem) items.get(1); - // assertTrue(item.isFormField()); - // Assert.AreEqual("name 2", item.getFieldName()); - // Assert.AreEqual("value 2+1", item.getString()); - - // item = (FileItem) items.get(2); - // assertTrue(item.isFormField()); - // Assert.AreEqual("name 2", item.getFieldName()); - // Assert.AreEqual("value 2+2", item.getString()); - - // item = (FileItem) items.get(3); - // Assert.IsFalse(item.isFormField()); - // Assert.AreEqual("logo", item.getFieldName()); - // Assert.AreEqual("logo.jpg", item.getName()); - // Assert.AreEqual("image/jpeg", item.getContentType()); - // } - // catch (FileUploadException ex) { - // throw new ServletException(ex); - // } - - // } - //} - #region REST test service [ServiceContract] diff --git a/test/Spring/Spring.Http.Tests/Http/Rest/RestTemplateTests.cs b/test/Spring/Spring.Http.Tests/Http/Rest/RestTemplateTests.cs index 40666ea8..f43a74b1 100644 --- a/test/Spring/Spring.Http.Tests/Http/Rest/RestTemplateTests.cs +++ b/test/Spring/Spring.Http.Tests/Http/Rest/RestTemplateTests.cs @@ -22,7 +22,6 @@ using System; using System.Net; using System.Collections.Generic; -using Spring.Http; using Spring.Http.Converters; using NUnit.Framework; 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 0d3f1137..9b89d2ae 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 @@ + 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 4786b6fd..5f163891 100644 --- a/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2010.csproj +++ b/test/Spring/Spring.Http.Tests/Spring.Http.Tests.2010.csproj @@ -128,6 +128,7 @@ +