REST client API: Polishing (SPRNET-1345)
This commit is contained in:
@@ -18,11 +18,7 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
@@ -35,12 +31,10 @@ namespace Spring.Http.Converters
|
||||
public class ByteArrayHttpMessageConverterTests
|
||||
{
|
||||
private ByteArrayHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new ByteArrayHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -67,45 +61,27 @@ namespace Spring.Http.Converters
|
||||
{
|
||||
byte[] body = new byte[] { 0x1, 0x2 };
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(body));
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.ContentLength = body.Length;
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body);
|
||||
message.Headers.ContentLength = body.Length;
|
||||
|
||||
byte[] result = converter.Read<byte[]>(message);
|
||||
Assert.AreEqual(body.Length, result.Length, "Invalid result");
|
||||
Assert.AreEqual(body[0], result[0], "Invalid result");
|
||||
Assert.AreEqual(body[1], result[1], "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
byte[] body = new byte[] { 0x1, 0x2 };
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body, result, "Invalid result");
|
||||
Assert.AreEqual(body, message.GetBodyAsBytes(), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "octet-stream"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.AreEqual(2, message.Headers.ContentLength, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,11 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.ServiceModel.Syndication;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Feed
|
||||
{
|
||||
@@ -39,12 +36,10 @@ namespace Spring.Http.Converters.Feed
|
||||
public class Atom10FeedHttpMessageConverterTests
|
||||
{
|
||||
private Atom10FeedHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new Atom10FeedHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -77,10 +72,7 @@ namespace Spring.Http.Converters.Feed
|
||||
string body = String.Format("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\">Test Feed</title><subtitle type=\"text\">This is a test feed</subtitle><id>Atom10FeedHttpMessageConverterTests.Write</id><rights type=\"text\">Copyright 2010</rights><updated>{0}</updated><author><name>Bruno Baïa</name><uri>http://www.springframework.net/bbaia</uri><email>bruno.baia@springframework.net</email></author><link rel=\"alternate\" href=\"http://www.springframework.net/Feed\" /></feed>",
|
||||
now.ToString("yyyy-MM-ddTHH:mm:sszzz", CultureInfo.InvariantCulture));
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
SyndicationFeed result = converter.Read<SyndicationFeed>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
@@ -94,15 +86,11 @@ namespace Spring.Http.Converters.Feed
|
||||
Assert.AreEqual("Bruno Baïa", result.Authors[0].Name, "Invalid result");
|
||||
Assert.AreEqual("bruno.baia@springframework.net", result.Authors[0].Email, "Invalid result");
|
||||
Assert.AreEqual("http://www.springframework.net/bbaia", result.Authors[0].Uri, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
string expectedBody = String.Format("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title type=\"text\">Test Feed</title><subtitle type=\"text\">This is a test feed</subtitle><id>Atom10FeedHttpMessageConverterTests.Write</id><rights type=\"text\">Copyright 2010</rights><updated>{0}</updated><author><name>Bruno Baïa</name><uri>http://www.springframework.net/bbaia</uri><email>bruno.baia@springframework.net</email></author><link rel=\"alternate\" href=\"http://www.springframework.net/Feed\" /></feed>",
|
||||
@@ -113,22 +101,13 @@ namespace Spring.Http.Converters.Feed
|
||||
body.Authors.Add(sp);
|
||||
body.Copyright = new TextSyndicationContent("Copyright 2010");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "atom+xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,11 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.ServiceModel.Syndication;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Feed
|
||||
{
|
||||
@@ -39,12 +36,10 @@ namespace Spring.Http.Converters.Feed
|
||||
public class Rss20FeedHttpMessageConverterTests
|
||||
{
|
||||
private Rss20FeedHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new Rss20FeedHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -78,10 +73,7 @@ namespace Spring.Http.Converters.Feed
|
||||
string body = String.Format("<rss xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel><title>Test Feed</title><link>http://www.springframework.net/Feed</link><description>This is a test feed</description><copyright>Copyright 2010</copyright><managingEditor>bruno.baia@springframework.net</managingEditor><lastBuildDate>{0}</lastBuildDate><a10:id>Atom10FeedHttpMessageConverterTests.Write</a10:id></channel></rss>",
|
||||
now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture).Remove(29, 1));
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
SyndicationFeed result = converter.Read<SyndicationFeed>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
@@ -93,15 +85,11 @@ namespace Spring.Http.Converters.Feed
|
||||
Assert.AreEqual("Copyright 2010", result.Copyright.Text, "Invalid result");
|
||||
Assert.IsTrue(result.Authors.Count == 1, "Invalid result");
|
||||
Assert.AreEqual("bruno.baia@springframework.net", result.Authors[0].Email, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
string expectedBody = String.Format("<rss xmlns:a10=\"http://www.w3.org/2005/Atom\" version=\"2.0\"><channel><title>Test Feed</title><link>http://www.springframework.net/Feed</link><description>This is a test feed</description><copyright>Copyright 2010</copyright><managingEditor>bruno.baia@springframework.net</managingEditor><lastBuildDate>{0}</lastBuildDate><a10:id>Atom10FeedHttpMessageConverterTests.Write</a10:id></channel></rss>",
|
||||
@@ -112,22 +100,13 @@ namespace Spring.Http.Converters.Feed
|
||||
body.Authors.Add(sp);
|
||||
body.Copyright = new TextSyndicationContent("Copyright 2010");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "rss+xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the FileInfoHttpMessageConverter class.
|
||||
/// </summary>
|
||||
/// <author>Bruno Baia</author>
|
||||
[TestFixture]
|
||||
public class FileInfoHttpMessageConverterTests
|
||||
{
|
||||
private FileInfoHttpMessageConverter converter;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
converter = new FileInfoHttpMessageConverter();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanRead()
|
||||
{
|
||||
Assert.IsFalse(converter.CanRead(typeof(FileInfo), MediaType.ALL));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanWrite()
|
||||
{
|
||||
Assert.IsTrue(converter.CanWrite(typeof(FileInfo), new MediaType("application", "octet-stream")));
|
||||
Assert.IsTrue(converter.CanWrite(typeof(FileInfo), new MediaType("application", "xml")));
|
||||
Assert.IsTrue(converter.CanWrite(typeof(FileInfo), MediaType.ALL));
|
||||
Assert.IsFalse(converter.CanWrite(typeof(string), new MediaType("application", "octet-stream")));
|
||||
}
|
||||
|
||||
//[Test]
|
||||
//public void Write()
|
||||
//{
|
||||
// FileInfo body = new FileInfo(@"C:\File.txt");
|
||||
|
||||
// MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
// converter.Write(body, null, message);
|
||||
|
||||
// Assert.AreEqual(body, message.GetBodyAsBytes(), "Invalid result");
|
||||
// Assert.AreEqual(new MediaType("text", "plain"), message.Headers.ContentType, "Invalid content-type");
|
||||
//}
|
||||
|
||||
[Test]
|
||||
public void WriteWithUnknownExtension()
|
||||
{
|
||||
FileInfo body = new FileInfo(@"C:\Dummy.unknown");
|
||||
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
//Assert.AreEqual(body, message.GetBodyAsBytes(), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "octet-stream"), message.Headers.ContentType, "Invalid content-type");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteWithKnownExtension()
|
||||
{
|
||||
FileInfo body = new FileInfo(@"C:\Dummy.txt");
|
||||
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
Assert.AreEqual(new MediaType("text", "plain"), message.Headers.ContentType, "Invalid content-type");
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void WriteWithCustomExtension()
|
||||
{
|
||||
FileInfo body = new FileInfo(@"C:\Dummy.myext");
|
||||
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.MimeMapping.Add(".myext", "spring/custom");
|
||||
converter.Write(body, null, message);
|
||||
|
||||
Assert.AreEqual(new MediaType("spring", "custom"), message.Headers.ContentType, "Invalid content-type");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,13 +20,11 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
@@ -39,12 +37,10 @@ namespace Spring.Http.Converters
|
||||
public class FormHttpMessageConverterTests
|
||||
{
|
||||
private FormHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new FormHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -76,13 +72,8 @@ namespace Spring.Http.Converters
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
MediaType mediaType = new MediaType("application", "x-www-form-urlencoded", charSet);
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.ContentType = mediaType;
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, charSetEncoding);
|
||||
message.Headers.ContentType = mediaType;
|
||||
|
||||
NameValueCollection result = converter.Read<NameValueCollection>(message);
|
||||
Assert.AreEqual(3, result.Count, "Invalid result");
|
||||
@@ -92,15 +83,11 @@ namespace Spring.Http.Converters
|
||||
Assert.AreEqual("value 2+1", result.GetValues("name 2")[0], "Invalid result");
|
||||
Assert.AreEqual("value 2+2", result.GetValues("name 2")[1], "Invalid result");
|
||||
Assert.IsNull(result["name 3"], "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteForm()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string expectedBody = "name+1=value+1&name+2=value+2%2b1&name+2=value+2%2b2&name+3";
|
||||
NameValueCollection body = new NameValueCollection();
|
||||
body.Add("name 1", "value 1");
|
||||
@@ -110,30 +97,19 @@ namespace Spring.Http.Converters
|
||||
string charSet = "ISO-8859-1";
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, MediaType.APPLICATION_FORM_URLENCODED, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, charSetEncoding.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(charSetEncoding), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "x-www-form-urlencoded"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.AreEqual(charSetEncoding.GetBytes(expectedBody).Length, message.Headers.ContentLength, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Ignore] //TODO: relative path (needs IResource ?)
|
||||
public void WriteMultipart()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
IDictionary<string, object> parts = new Dictionary<string, object>();
|
||||
parts.Add("name 1", "value 1");
|
||||
parts.Add("name 2", "value 2+1");
|
||||
@@ -142,12 +118,7 @@ namespace Spring.Http.Converters
|
||||
parts.Add("xml", entity);
|
||||
parts.Add("logo", new FileInfo(@"C:\Users\Bruno\Pictures\Hero\downloadfile.jpeg"));
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(parts, MediaType.MULTIPART_FORM_DATA, message);
|
||||
|
||||
@@ -158,15 +129,11 @@ namespace Spring.Http.Converters
|
||||
string boundary = contentType.GetParameter("boundary");
|
||||
Assert.IsNotNull(boundary, "Invalid content-type");
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
string resultAsString = Encoding.UTF8.GetString(result);
|
||||
Assert.IsTrue(resultAsString.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 1\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 1\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(resultAsString.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 2\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 2+1\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(resultAsString.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"xml\"\r\nContent-Type: text/xml\r\n\r\n<root><child/></root>\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(resultAsString.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"logo\"; filename=\"C:\\Users\\Bruno\\Pictures\\Hero\\downloadfile.jpeg\"\r\nContent-Type: application/octet-stream\r\n\r\n"), "Invalid content-disposition");
|
||||
|
||||
mocks.VerifyAll();
|
||||
string result = message.GetBodyAsString(Encoding.UTF8);
|
||||
Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 1\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 1\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"name 2\"\r\nContent-Type: text/plain;charset=ISO-8859-1\r\n\r\nvalue 2+1\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"xml\"\r\nContent-Type: text/xml\r\n\r\n<root><child/></root>\r\n"), "Invalid content-disposition");
|
||||
Assert.IsTrue(result.Contains("--" + boundary + "\r\nContent-Disposition: form-data; name=\"logo\"; filename=\"C:\\Users\\Bruno\\Pictures\\Hero\\downloadfile.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n"), "Invalid content-disposition");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,9 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Json
|
||||
{
|
||||
@@ -36,12 +33,10 @@ namespace Spring.Http.Converters.Json
|
||||
public class JsonHttpMessageConverterTests
|
||||
{
|
||||
private JsonHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new JsonHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -64,43 +59,27 @@ namespace Spring.Http.Converters.Json
|
||||
{
|
||||
string body = "{\"ID\":\"1\",\"Name\":\"Bruno Baïa\"}";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
CustomClass result = converter.Read<CustomClass>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
Assert.AreEqual("1", result.ID, "Invalid result");
|
||||
Assert.AreEqual("Bruno Baïa", result.Name, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string expectedBody = "{\"ID\":\"1\",\"Name\":\"Bruno Baïa\"}";
|
||||
CustomClass body = new CustomClass("1", "Bruno Baïa");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "json"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
#region Test classes
|
||||
|
||||
@@ -18,13 +18,9 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters
|
||||
{
|
||||
@@ -37,12 +33,10 @@ namespace Spring.Http.Converters
|
||||
public class StringHttpMessageConverterTests
|
||||
{
|
||||
private StringHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new StringHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -72,74 +66,45 @@ namespace Spring.Http.Converters
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
MediaType mediaType = new MediaType("text", "plain", charSet);
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.ContentType = mediaType;
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, charSetEncoding);
|
||||
message.Headers.ContentType = mediaType;
|
||||
|
||||
string result = converter.Read<string>(message);
|
||||
Assert.AreEqual(body, result, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteDefaultCharset()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string body = "H\u00e9llo W\u00f6rld";
|
||||
string charSet = "ISO-8859-1";
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
MediaType mediaType = new MediaType("text", "plain", charSet);
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body, charSetEncoding.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(body, message.GetBodyAsString(charSetEncoding), "Invalid result");
|
||||
Assert.AreEqual(mediaType, message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.AreEqual(charSetEncoding.GetBytes(body).Length, message.Headers.ContentLength, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteUTF8()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string body = "H\u00e9llo W\u00f6rld";
|
||||
string charSet = "UTF-8";
|
||||
Encoding charSetEncoding = Encoding.GetEncoding(charSet);
|
||||
MediaType mediaType = new MediaType("text", "plain", charSet);
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, mediaType, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body, charSetEncoding.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(body, message.GetBodyAsString(charSetEncoding), "Invalid result");
|
||||
Assert.AreEqual(mediaType, message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.AreEqual(charSetEncoding.GetBytes(body).Length, message.Headers.ContentLength, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,11 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
@@ -38,12 +35,10 @@ namespace Spring.Http.Converters.Xml
|
||||
public class DataContractHttpMessageConverterTests
|
||||
{
|
||||
private DataContractHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new DataContractHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -77,43 +72,27 @@ namespace Spring.Http.Converters.Xml
|
||||
<ID>1</ID><Name>Bruno Baïa</Name>
|
||||
</DataContractHttpMessageConverterTests.DataContractClass>";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
DataContractClass result = converter.Read<DataContractClass>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
Assert.AreEqual("1", result.ID, "Invalid result");
|
||||
Assert.AreEqual("Bruno Baïa", result.Name, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string expectedBody = "<DataContractHttpMessageConverterTests.DataContractClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Spring.Http.Converters.Xml\"><ID>1</ID><Name>Bruno Baïa</Name></DataContractHttpMessageConverterTests.DataContractClass>";
|
||||
DataContractClass body = new DataContractClass("1", "Bruno Baïa");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
#region Test classes
|
||||
|
||||
@@ -20,14 +20,11 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
@@ -39,12 +36,10 @@ namespace Spring.Http.Converters.Xml
|
||||
public class XElementHttpMessageConverterTests
|
||||
{
|
||||
private XElementHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new XElementHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -73,10 +68,7 @@ namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
string body = "<?xml version='1.0' encoding='UTF-8' ?><Root><TestElement testAttribute='value'/><TestElement testAttribute='novalue'/></Root>";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
XElement result = converter.Read<XElement>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
@@ -88,35 +80,22 @@ namespace Spring.Http.Converters.Xml
|
||||
select el)
|
||||
.Single();
|
||||
Assert.IsNotNull(xResult, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
XElement body = new XElement("Root",
|
||||
new XElement("TestElement", 1),
|
||||
new XElement("TestElement", 2));
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body.ToString(SaveOptions.DisableFormatting), Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(body.ToString(SaveOptions.DisableFormatting), message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,13 +19,10 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
@@ -37,12 +34,10 @@ namespace Spring.Http.Converters.Xml
|
||||
public class XmlDocumentHttpMessageConverterTests
|
||||
{
|
||||
private XmlDocumentHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new XmlDocumentHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -71,10 +66,7 @@ namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
string body = "<TestElement testAttribute='value' />";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
XmlDocument result = converter.Read<XmlDocument>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
@@ -83,34 +75,21 @@ namespace Spring.Http.Converters.Xml
|
||||
Assert.AreEqual("TestElement", xmlNodeResult.LocalName, "Invalid result");
|
||||
Assert.IsNotNull(xmlNodeResult.Attributes["testAttribute"], "Invalid result");
|
||||
Assert.AreEqual("value", xmlNodeResult.Attributes["testAttribute"].Value, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
XmlDocument body = new XmlDocument();
|
||||
body.LoadXml("<TestElement testAttribute='value' />");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(body.OuterXml, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(body.OuterXml, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,9 @@
|
||||
|
||||
#endregion
|
||||
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
|
||||
namespace Spring.Http.Converters.Xml
|
||||
{
|
||||
@@ -35,12 +32,10 @@ namespace Spring.Http.Converters.Xml
|
||||
public class XmlSerializableHttpMessageConverterTests
|
||||
{
|
||||
private XmlSerializableHttpMessageConverter converter;
|
||||
private MockRepository mocks;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
converter = new XmlSerializableHttpMessageConverter();
|
||||
}
|
||||
|
||||
@@ -70,43 +65,27 @@ namespace Spring.Http.Converters.Xml
|
||||
<Name>Bruno Baïa</Name>
|
||||
</CustomClass>";
|
||||
|
||||
IHttpInputMessage message = mocks.CreateMock<IHttpInputMessage>();
|
||||
Expect.Call<Stream>(message.Body).Return(new MemoryStream(Encoding.UTF8.GetBytes(body)));
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(body, Encoding.UTF8);
|
||||
|
||||
CustomClass result = converter.Read<CustomClass>(message);
|
||||
Assert.IsNotNull(result, "Invalid result");
|
||||
Assert.AreEqual("1", result.ID, "Invalid result");
|
||||
Assert.AreEqual("Bruno Baïa", result.Name, "Invalid result");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write()
|
||||
{
|
||||
MemoryStream requestStream = new MemoryStream();
|
||||
|
||||
string expectedBody = "<CustomClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><ID>1</ID><Name>Bruno Baïa</Name></CustomClass>";
|
||||
CustomClass body = new CustomClass("1", "Bruno Baïa");
|
||||
|
||||
IHttpOutputMessage message = mocks.CreateMock<IHttpOutputMessage>();
|
||||
Expect.Call(message.Body).PropertyBehavior();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
Expect.Call<HttpHeaders>(message.Headers).Return(headers).Repeat.Any();
|
||||
|
||||
mocks.ReplayAll();
|
||||
MockHttpOutputMessage message = new MockHttpOutputMessage();
|
||||
|
||||
converter.Write(body, null, message);
|
||||
|
||||
message.Body(requestStream);
|
||||
byte[] result = requestStream.ToArray();
|
||||
Assert.AreEqual(expectedBody, Encoding.UTF8.GetString(result), "Invalid result");
|
||||
Assert.AreEqual(expectedBody, message.GetBodyAsString(Encoding.UTF8), "Invalid result");
|
||||
Assert.AreEqual(new MediaType("application", "xml"), message.Headers.ContentType, "Invalid content-type");
|
||||
//Assert.IsTrue(message.Headers.ContentLength > -1, "Invalid content-length");
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
#region Test classes
|
||||
|
||||
58
test/Spring/Spring.Http.Tests/Http/MockHttpInputMessage.cs
Normal file
58
test/Spring/Spring.Http.Tests/Http/MockHttpInputMessage.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Spring.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// Mocked IHttpInputMessage implementation.
|
||||
/// </summary>
|
||||
/// <author>Arjen Poutsma</author>
|
||||
/// <author>Bruno Baia (.NET)</author>
|
||||
public class MockHttpInputMessage : IHttpInputMessage
|
||||
{
|
||||
private HttpHeaders headers;
|
||||
private Stream body;
|
||||
|
||||
public MockHttpInputMessage(byte[] body)
|
||||
{
|
||||
this.headers = new HttpHeaders();
|
||||
this.body = new MemoryStream(body);
|
||||
}
|
||||
|
||||
public MockHttpInputMessage(string body, Encoding charset)
|
||||
: this(charset.GetBytes(body))
|
||||
{
|
||||
}
|
||||
|
||||
public HttpHeaders Headers
|
||||
{
|
||||
get { return this.headers; }
|
||||
}
|
||||
|
||||
public Stream Body
|
||||
{
|
||||
get { return this.body; }
|
||||
}
|
||||
}
|
||||
}
|
||||
72
test/Spring/Spring.Http.Tests/Http/MockHttpOuputMessage.cs
Normal file
72
test/Spring/Spring.Http.Tests/Http/MockHttpOuputMessage.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Spring.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// Mocked IHttpOutputMessage implementation.
|
||||
/// </summary>
|
||||
/// <author>Arjen Poutsma</author>
|
||||
/// <author>Bruno Baia (.NET)</author>
|
||||
public class MockHttpOutputMessage : IHttpOutputMessage
|
||||
{
|
||||
private HttpHeaders headers;
|
||||
private Action<Stream> body;
|
||||
private byte[] bodyAsBytes;
|
||||
|
||||
public MockHttpOutputMessage()
|
||||
{
|
||||
this.headers = new HttpHeaders();
|
||||
}
|
||||
|
||||
public HttpHeaders Headers
|
||||
{
|
||||
get { return this.headers; }
|
||||
}
|
||||
|
||||
public Action<Stream> Body
|
||||
{
|
||||
set { this.body = value; }
|
||||
}
|
||||
|
||||
public byte[] GetBodyAsBytes()
|
||||
{
|
||||
if (bodyAsBytes == null)
|
||||
{
|
||||
using (MemoryStream requestStream = new MemoryStream())
|
||||
{
|
||||
this.body(requestStream);
|
||||
bodyAsBytes = requestStream.ToArray();
|
||||
}
|
||||
}
|
||||
return bodyAsBytes;
|
||||
}
|
||||
|
||||
public String GetBodyAsString(Encoding charset)
|
||||
{
|
||||
byte[] bytes = GetBodyAsBytes();
|
||||
return charset.GetString(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="Http\Converters\ByteArrayHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FileInfoHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FormHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\Atom10FeedHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\FeedHttpMessageConverterIntegrationTests.cs" />
|
||||
@@ -103,8 +104,10 @@
|
||||
<Compile Include="Http\Converters\Xml\XmlDocumentHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Xml\XmlHttpMessageConverterIntegrationTests.cs" />
|
||||
<Compile Include="Http\Converters\Xml\XmlSerializableHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\MockHttpOuputMessage.cs" />
|
||||
<Compile Include="Http\HttpHeadersTests.cs" />
|
||||
<Compile Include="Http\MediaTypeTests.cs" />
|
||||
<Compile Include="Http\MockHttpInputMessage.cs" />
|
||||
<Compile Include="Http\Rest\HttpStatusCodeExceptionTests.cs" />
|
||||
<Compile Include="Http\Rest\RestTemplateIntegrationTests.cs" />
|
||||
<Compile Include="Util\SerializationTestUtils.cs" />
|
||||
|
||||
@@ -107,6 +107,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="Http\Converters\ByteArrayHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FileInfoHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\Atom10FeedHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\FeedHttpMessageConverterIntegrationTests.cs" />
|
||||
<Compile Include="Http\Converters\Feed\Rss20FeedHttpMessageConverterTests.cs" />
|
||||
@@ -121,8 +122,10 @@
|
||||
<Compile Include="Http\Converters\Xml\XmlDocumentHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\Xml\XmlHttpMessageConverterIntegrationTests.cs" />
|
||||
<Compile Include="Http\Converters\Xml\XmlSerializableHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\MockHttpOuputMessage.cs" />
|
||||
<Compile Include="Http\HttpHeadersTests.cs" />
|
||||
<Compile Include="Http\MediaTypeTests.cs" />
|
||||
<Compile Include="Http\MockHttpInputMessage.cs" />
|
||||
<Compile Include="Http\Rest\HttpStatusCodeExceptionTests.cs" />
|
||||
<Compile Include="Http\Rest\RestTemplateIntegrationTests.cs" />
|
||||
<Compile Include="Util\SerializationTestUtils.cs" />
|
||||
|
||||
@@ -125,6 +125,7 @@
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Http\Converters\Feed\Rss20FeedHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FileInfoHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\Converters\FormHttpMessageConverterTests.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
@@ -138,6 +139,8 @@
|
||||
<Compile Include="Http\Converters\Xml\XmlDocumentHttpMessageConverterTests.cs" />
|
||||
<Compile Include="Http\HttpHeadersTests.cs" />
|
||||
<Compile Include="Http\MediaTypeTests.cs" />
|
||||
<Compile Include="Http\MockHttpInputMessage.cs" />
|
||||
<Compile Include="Http\MockHttpOuputMessage.cs" />
|
||||
<Compile Include="Http\Rest\HttpStatusCodeExceptionTests.cs" />
|
||||
<Compile Include="Http\Rest\RestTemplateIntegrationTests.cs" />
|
||||
<Compile Include="Http\Rest\RestTemplateTests.cs" />
|
||||
|
||||
Reference in New Issue
Block a user