Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.ReSharper*
*.user
_ReSharper*/*
*.ncrunchsolution
249 changes: 148 additions & 101 deletions DynamicRest.UnitTests/Xml/XmlResponsesTests.cs
Original file line number Diff line number Diff line change
@@ -1,108 +1,155 @@
using System;
using DynamicRest.Xml;
using Machine.Specifications;

namespace DynamicRest.UnitTests.Xml
{
[Subject(typeof(StandardResultBuilder))]
public class When_a_response_contains_a_collection {

static StandardResultBuilder _resultBuilder;
static dynamic _response;

Establish context = () =>
{
_resultBuilder = new StandardResultBuilder(RestService.Xml);
};

Because the_response_is_created = () => { _response = _resultBuilder.CreateResult(_xml); };

It should_contain_the_media_0_url = () => (_response.item.media[0].url as string).ShouldEqual("http://media0url");
It should_contain_the_media_1_url = () => (_response.item.media[1].url as string).ShouldEqual("http://media1url");
It should_contain_the_image_0_url = () => (_response.item.images.image[0].src as string).ShouldEqual("http://image0url");
It should_contain_the_image_1_url = () => (_response.item.images.image[1].src as string).ShouldEqual("http://image1url");
It should_contain_the_link_using_array_access = () => (_response.item.link[0].Value as string).ShouldEqual("http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956");
It should_contain_the_attachment_title = () => (_response.item.attachments.attachment[0].title.Value as string).ShouldEqual("this is the title");
using System;
using DynamicRest.Xml;
using Machine.Specifications;
using Machine.Specifications.Model;

namespace DynamicRest.UnitTests.Xml
{
[Subject(typeof(StandardResultBuilder))]
public class When_a_response_contains_a_collection {

static StandardResultBuilder _resultBuilder;
static dynamic _response;

Establish context = () =>
{
_resultBuilder = new StandardResultBuilder(RestService.Xml);
};

Because the_response_is_created = () => { _response = _resultBuilder.CreateResult(_xml); };

It should_contain_the_media_0_url = () => (_response.item.media[0].url as string).ShouldEqual("http://media0url");
It should_contain_the_media_1_url = () => (_response.item.media[1].url as string).ShouldEqual("http://media1url");
It should_contain_the_image_0_url = () => (_response.item.images.image[0].src as string).ShouldEqual("http://image0url");
It should_contain_the_image_1_url = () => (_response.item.images.image[1].src as string).ShouldEqual("http://image1url");
It should_contain_the_link_using_array_access = () => (_response.item.link[0].Value as string).ShouldEqual("http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956");
It should_contain_the_attachment_title = () => (_response.item.attachments.attachment[0].title.Value as string).ShouldEqual("this is the title");
It should_contain_the_numbers_of_attachments = () => ((int)_response.item.attachments.Count).ShouldEqual(1);

It should_contain_the_pubdate =
() => ((DateTime)_response.item.pubDate).ShouldEqual(new DateTime(2011, 3, 8, 11, 21, 16));

It should_work_when_a_refence_is_used_as_an_array = () =>
{
var linkAsArray = _response.item.link;
((string)linkAsArray[0].Value).ShouldEqual("http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956");
};

static string _xml = @"
<news>
<item>
<title>Gaddafi renews attack on rebels</title>
<version>5</version>
<description>Forces loyal to Libyan leader Col Muammar Gaddafi launch fresh air strikes on the rebel-held Ras Lanuf, as they try to retake the oil-rich town.</description>
<link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956</link>
<link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956</link>
<guid>http://www.bbc.co.uk/news/world-africa-12673956</guid>
<pubDate>Tue, 08 Mar 2011 11:21:16 GMT</pubDate>
<media url=""http://media0url""/>
<media url=""http://media1url""/>
<images>
<image src=""http://image0url"" />
<image src=""http://image1url"" />
</images>
<attachments>
<attachment>
<title>this is the title</title>
</attachment>
</attachments>
</item>
</news>";
}

[Subject(typeof(XmlNode))]
public class When_accessing_a_non_existing_element {

static StandardResultBuilder _resultBuilder;
static dynamic _response;

Establish context = () =>
{
_resultBuilder = new StandardResultBuilder(RestService.Xml);
_response = _resultBuilder.CreateResult(_xml);
};

private Because the_response_is_created = () => _thrownException = Catch.Exception(() => { var junk = _response.item.desc; });

It should_work_when_a_refence_is_used_as_an_array = () =>
_thrownException.Message.ShouldEqual("No element or attribute named 'desc' found in the response.");

static string _xml = @"
<news>
<item>
<title>Gaddafi renews attack on rebels</title>
<description>Forces loyal to Libyan leader Col Muammar Gaddafi launch fresh air strikes on the rebel-held Ras Lanuf, as they try to retake the oil-rich town.</description>
<link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956</link>
<link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956</link>
<guid>http://www.bbc.co.uk/news/world-africa-12673956</guid>
<pubDate>Tue, 08 Mar 2011 11:21:16 GMT</pubDate>
<media url=""http://media0url""/>
<media url=""http://media1url""/>
<images>
<image src=""http://image0url"" />
<image src=""http://image1url"" />
</images>
<attachments>
<attachment>
<title>this is the title</title>
</attachment>
</attachments>
</item>
</news>";

private static Exception _thrownException;
() => ((DateTime)_response.item.pubDate).ShouldEqual(new DateTime(2011, 3, 8, 11, 21, 16));

It should_work_when_a_refence_is_used_as_an_array = () =>
{
var linkAsArray = _response.item.link;
((string)linkAsArray[0].Value).ShouldEqual("http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956");
};

static string _xml = @"
<news>
<item>
<title>Gaddafi renews attack on rebels</title>
<version>5</version>
<description>Forces loyal to Libyan leader Col Muammar Gaddafi launch fresh air strikes on the rebel-held Ras Lanuf, as they try to retake the oil-rich town.</description>
<link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956</link>
<link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956</link>
<guid>http://www.bbc.co.uk/news/world-africa-12673956</guid>
<pubDate>Tue, 08 Mar 2011 11:21:16 GMT</pubDate>
<media url=""http://media0url""/>
<media url=""http://media1url""/>
<images>
<image src=""http://image0url"" />
<image src=""http://image1url"" />
</images>
<attachments>
<attachment>
<title>this is the title</title>
</attachment>
</attachments>
</item>
</news>";
}

[Subject(typeof(StandardResultBuilder))]
public class When_a_response_contains_an_empty_collection
{
static StandardResultBuilder _resultBuilder;
static dynamic _response;

Establish context = () =>
{
_resultBuilder = new StandardResultBuilder(RestService.Xml);
};

Because the_response_is_created = () => { _response = _resultBuilder.CreateResult(_xml); };

It should_count_zero_elements = () => ((int)_response.images.Count).ShouldEqual(0);

static string _xml = @"
<item>
<images></images>
</item>";
}

[Subject(typeof(StandardResultBuilder))]
public class When_a_parsing_in_a_datetime
{
static StandardResultBuilder _resultBuilder;
static dynamic _response;

Establish context = () =>
{
_resultBuilder = new StandardResultBuilder(RestService.Xml);
};

Because the_response_is_created = () => { _response = _resultBuilder.CreateResult(_xml); };


It should_pass = () => ShouldExtensionMethods.ShouldEqual(DateTime.Parse(_response.created), TimeStamp);

static string _xml = @"
<item>
<created>2007-10-10T09:02:17Z</created>
</item>";

static DateTime TimeStamp = DateTime.Parse("2007-10-10T09:02:17Z");
}

[Subject(typeof(XmlNode))]
public class When_accessing_a_non_existing_element {

static StandardResultBuilder _resultBuilder;
static dynamic _response;

Establish context = () =>
{
_resultBuilder = new StandardResultBuilder(RestService.Xml);
_response = _resultBuilder.CreateResult(_xml);
};

private Because the_response_is_created = () => _thrownException = Catch.Exception(() => { var junk = _response.item.desc; });

It should_work_when_a_refence_is_used_as_an_array = () =>
_thrownException.Message.ShouldEqual("No element or attribute named 'desc' found in the response.");

static string _xml = @"
<news>
<item>
<title>Gaddafi renews attack on rebels</title>
<description>Forces loyal to Libyan leader Col Muammar Gaddafi launch fresh air strikes on the rebel-held Ras Lanuf, as they try to retake the oil-rich town.</description>
<link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956</link>
<link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12673956</link>
<guid>http://www.bbc.co.uk/news/world-africa-12673956</guid>
<pubDate>Tue, 08 Mar 2011 11:21:16 GMT</pubDate>
<media url=""http://media0url""/>
<media url=""http://media1url""/>
<images>
<image src=""http://image0url"" />
<image src=""http://image1url"" />
</images>
<attachments>
<attachment>
<title>this is the title</title>
</attachment>
</attachments>
</item>
</news>";

private static Exception _thrownException;
}



namespace When_casting_strings_to_datetimes
{
[Subject(typeof(XmlString))]
Expand Down Expand Up @@ -217,7 +264,7 @@ public class When_parsing_fails
private static Exception The_result;
}
}

namespace When_casting_strings_to_bools
{
public class When_parsing_the_empty_string
Expand Down Expand Up @@ -289,5 +336,5 @@ public class When_parsing_weird_casing

protected static bool Result { get; set; }
}
}
}
}
Loading