summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndres G. Aragoneses <knocte@gmail.com>2013-02-15 21:16:40 +0000
committerAndres G. Aragoneses <knocte@gmail.com>2013-02-15 21:16:40 +0000
commitb3fefbf47721e2d9365695a74705fac228fefe2b (patch)
tree85df5a1c486f1a80fb23fc8e7e747a772ff54583
parentb7c04706e8547abe1e056dbba3b9641b503a94ec (diff)
Lastfm: add some tests around LastfmRequest class
To be able to refactor it without the danger of breaking it.
-rw-r--r--src/Libraries/Lastfm/Lastfm.csproj1
-rw-r--r--src/Libraries/Lastfm/Lastfm/LastfmRequest.cs27
-rw-r--r--src/Libraries/Lastfm/Lastfm/Tests/LastfmRequestTests.cs117
-rw-r--r--src/Libraries/Lastfm/Makefile.am3
-rw-r--r--tests/Makefile.am1
5 files changed, 146 insertions, 3 deletions
diff --git a/src/Libraries/Lastfm/Lastfm.csproj b/src/Libraries/Lastfm/Lastfm.csproj
index 1f09b9365..25cb35342 100644
--- a/src/Libraries/Lastfm/Lastfm.csproj
+++ b/src/Libraries/Lastfm/Lastfm.csproj
@@ -82,6 +82,7 @@
<Compile Include="Lastfm.Data\LastfmAlbumData.cs" />
<Compile Include="Lastfm\LastfmRequest.cs" />
<Compile Include="Lastfm.Data\ILastfmInfo.cs" />
+ <Compile Include="Lastfm\Tests\LastfmRequestTests.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
diff --git a/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs b/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
index e18ad0d51..9234d4dd7 100644
--- a/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
+++ b/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
@@ -54,6 +54,19 @@ namespace Lastfm
public delegate void SendRequestHandler ();
+ internal interface IWebRequestCreator
+ {
+ HttpWebRequest Create (string requestUriString);
+ }
+
+ internal class WebRequestCreator : IWebRequestCreator
+ {
+ public HttpWebRequest Create (string requestUriString)
+ {
+ return (HttpWebRequest) HttpWebRequest.Create (requestUriString);
+ }
+ }
+
public class LastfmRequest
{
private const string API_ROOT = "http://ws.audioscrobbler.com/2.0/";
@@ -61,10 +74,17 @@ namespace Lastfm
private Dictionary<string, string> parameters = new Dictionary<string, string> ();
private Stream response_stream;
private string response_string;
+ IWebRequestCreator web_request_creator;
public LastfmRequest ()
{}
+ internal LastfmRequest (string method, RequestType request_type, ResponseFormat response_format, IWebRequestCreator web_request_creator)
+ : this (method, request_type, response_format)
+ {
+ this.web_request_creator = web_request_creator;
+ }
+
public LastfmRequest (string method) : this (method, RequestType.Read, ResponseFormat.Json)
{}
@@ -73,6 +93,9 @@ namespace Lastfm
this.method = method;
this.request_type = request_type;
this.response_format = response_format;
+ if (this.web_request_creator == null) {
+ this.web_request_creator = new WebRequestCreator ();
+ }
}
private string method;
@@ -271,7 +294,7 @@ namespace Lastfm
private Stream Get (string uri, string accept)
{
- HttpWebRequest request = (HttpWebRequest) WebRequest.Create (uri);
+ HttpWebRequest request = web_request_creator.Create (uri);
if (accept != null) {
request.Accept = accept;
}
@@ -293,7 +316,7 @@ namespace Lastfm
private Stream Post (string uri, string data)
{
// Do not trust docs : it doesn't work if parameters are in the request body
- HttpWebRequest request = (HttpWebRequest) WebRequest.Create (String.Concat (uri, "?", data));
+ HttpWebRequest request = web_request_creator.Create (String.Concat (uri, "?", data));
request.UserAgent = LastfmCore.UserAgent;
request.Timeout = 10000;
request.Method = "POST";
diff --git a/src/Libraries/Lastfm/Lastfm/Tests/LastfmRequestTests.cs b/src/Libraries/Lastfm/Lastfm/Tests/LastfmRequestTests.cs
new file mode 100644
index 000000000..cf5cff75b
--- /dev/null
+++ b/src/Libraries/Lastfm/Lastfm/Tests/LastfmRequestTests.cs
@@ -0,0 +1,117 @@
+//
+// LastfmRequestTests.cs
+//
+// Author:
+// Andres G Aragoneses <knocte@gmail.com>
+//
+// Copyright (C) 2013 Andres G. Aragoneses
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#if ENABLE_TESTS
+
+using System;
+using System.Net;
+using NUnit.Framework;
+
+namespace Lastfm.Tests
+{
+ [TestFixture]
+ public class Tests
+ {
+ class FakeWebRequestCreator : IWebRequestCreator
+ {
+ public HttpWebRequest Create (string requestUriString)
+ {
+ Uri = requestUriString;
+ return (HttpWebRequest)WebRequest.Create (requestUriString);
+ }
+
+ internal string Uri { get; set; }
+ }
+
+ [Test]
+ public void EmptyGet ()
+ {
+ var expected = "http://ws.audioscrobbler.com/2.0/?method=someMethod&api_key=344e9141fffeb02201e1ae455d92ae9f&format=json";
+ var creator = new FakeWebRequestCreator ();
+ new LastfmRequest ("someMethod", RequestType.Read, ResponseFormat.Json, creator).Send ();
+ Assert.AreEqual (expected, creator.Uri);
+ }
+
+ [Test]
+ public void GetWithParams ()
+ {
+ var expected = "http://ws.audioscrobbler.com/2.0/?method=someMethod&api_key=344e9141fffeb02201e1ae455d92ae9f&x=y&a=b&format=json";
+ var creator = new FakeWebRequestCreator ();
+ var req = new LastfmRequest ("someMethod", RequestType.Read, ResponseFormat.Json, creator);
+ req.AddParameter ("x", "y");
+ req.AddParameter ("a", "b");
+ req.Send ();
+ Assert.AreEqual (expected, creator.Uri);
+ }
+
+ [Test]
+ public void EmptyRawGet ()
+ {
+ var expected = "http://ws.audioscrobbler.com/2.0/?method=someMethod&api_key=344e9141fffeb02201e1ae455d92ae9f&raw=true";
+ var creator = new FakeWebRequestCreator ();
+ new LastfmRequest ("someMethod", RequestType.Read, ResponseFormat.Raw, creator).Send ();
+ Assert.AreEqual (expected, creator.Uri);
+ }
+
+ [Test]
+ public void EmptyJsonPost ()
+ {
+ var expected = "http://ws.audioscrobbler.com/2.0/?method=someMethod&api_key=344e9141fffeb02201e1ae455d92ae9f&format=json&sk=&api_sig=33ca04b6d45c54eb1405b3d7cb7735ea";
+ var creator = new FakeWebRequestCreator ();
+ new LastfmRequest ("someMethod", RequestType.Write, ResponseFormat.Json, creator).Send ();
+ Assert.AreEqual (expected, creator.Uri);
+ }
+
+ [Test]
+ public void JsonPostWithParams ()
+ {
+ var expected = "http://ws.audioscrobbler.com/2.0/?method=someMethod&api_key=344e9141fffeb02201e1ae455d92ae9f&x=y&a=b&format=json&sk=&api_sig=6b369269588df3d3b1ac67834d703c6d";
+ var creator = new FakeWebRequestCreator ();
+ var req = new LastfmRequest ("someMethod", RequestType.Write, ResponseFormat.Json, creator);
+ req.AddParameter ("x", "y");
+ req.AddParameter ("a", "b");
+ req.Send ();
+ Assert.AreEqual (expected, creator.Uri);
+ }
+
+ [Test]
+ public void JsonRawWithParams ()
+ {
+ var expected = "http://ws.audioscrobbler.com/2.0/?method=someMethod&api_key=344e9141fffeb02201e1ae455d92ae9f&x=y&a=b&raw=true&sk=&api_sig=3b419688648ce7e124c0056aba9a6438";
+ var creator = new FakeWebRequestCreator ();
+ var req = new LastfmRequest ("someMethod", RequestType.Write, ResponseFormat.Raw, creator);
+ req.AddParameter ("x", "y");
+ req.AddParameter ("a", "b");
+ req.Send ();
+ Assert.AreEqual (expected, creator.Uri);
+ }
+
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/src/Libraries/Lastfm/Makefile.am b/src/Libraries/Lastfm/Makefile.am
index 7681cf1ba..7b77b5edb 100644
--- a/src/Libraries/Lastfm/Makefile.am
+++ b/src/Libraries/Lastfm/Makefile.am
@@ -17,7 +17,8 @@ SOURCES = \
Lastfm/IQueue.cs \
Lastfm/LastfmCore.cs \
Lastfm/LastfmRequest.cs \
- Lastfm/RadioConnection.cs
+ Lastfm/RadioConnection.cs \
+ Lastfm/Tests/LastfmRequestTests.cs
include $(top_srcdir)/build/build.mk
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ad3cd33a4..aad42b17a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -10,6 +10,7 @@ TEST_ASSEMBLIES = \
Hyena.dll \
Hyena.Gui.dll \
Hyena.Data.Sqlite.dll \
+ Lastfm.dll \
Migo.dll \
Mono.Media.dll \
Banshee.Core.dll \