[banshee] Lastfm: simplify previous tests addition by dropping an interface



commit c53d258466ccaf82b70ba28c68528a8f01497188
Author: Andres G. Aragoneses <knocte gmail com>
Date:   Fri Feb 15 21:53:07 2013 +0000

    Lastfm: simplify previous tests addition by dropping an interface
    
    The interface IWebRequestCreate already exists in .NET class libraries,
    so use it in favour of our own IWebRequestCreator. Only difference is
    that this one uses System.Uri instead of string as the type of the uri
    parameter, so we need a bit of refactoring to make this compile.
    
    Tests still pass of course. No change of behaviour in this commit.

 src/Libraries/Lastfm/Lastfm/LastfmRequest.cs       |   19 ++++++----------
 .../Lastfm/Lastfm/Tests/LastfmRequestTests.cs      |   22 ++++++++++----------
 2 files changed, 18 insertions(+), 23 deletions(-)
---
diff --git a/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs b/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
index 9234d4d..b59cfcf 100644
--- a/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
+++ b/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
@@ -54,16 +54,11 @@ namespace Lastfm
 
     public delegate void SendRequestHandler ();
 
-    internal interface IWebRequestCreator
+    internal class WebRequestCreator : IWebRequestCreate
     {
-        HttpWebRequest Create (string requestUriString);
-    }
-
-    internal class WebRequestCreator : IWebRequestCreator
-    {
-        public HttpWebRequest Create (string requestUriString)
+        public WebRequest Create (Uri uri)
         {
-            return (HttpWebRequest) HttpWebRequest.Create (requestUriString);
+            return (HttpWebRequest) HttpWebRequest.Create (uri);
         }
     }
 
@@ -74,12 +69,12 @@ namespace Lastfm
         private Dictionary<string, string> parameters = new Dictionary<string, string> ();
         private Stream response_stream;
         private string response_string;
-        IWebRequestCreator web_request_creator;
+        IWebRequestCreate web_request_creator;
 
         public LastfmRequest ()
         {}
 
-        internal LastfmRequest (string method, RequestType request_type, ResponseFormat response_format, 
IWebRequestCreator web_request_creator)
+        internal LastfmRequest (string method, RequestType request_type, ResponseFormat response_format, 
IWebRequestCreate web_request_creator)
             : this (method, request_type, response_format)
         {
             this.web_request_creator = web_request_creator;
@@ -294,7 +289,7 @@ namespace Lastfm
 
         private Stream Get (string uri, string accept)
         {
-            HttpWebRequest request = web_request_creator.Create (uri);
+            var request = (HttpWebRequest)web_request_creator.Create (new Uri (uri));
             if (accept != null) {
                 request.Accept = accept;
             }
@@ -316,7 +311,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 = web_request_creator.Create (String.Concat (uri, "?", data));
+            var request = (HttpWebRequest)web_request_creator.Create (new Uri (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
index cf5cff7..51f4fd0 100644
--- a/src/Libraries/Lastfm/Lastfm/Tests/LastfmRequestTests.cs
+++ b/src/Libraries/Lastfm/Lastfm/Tests/LastfmRequestTests.cs
@@ -37,15 +37,15 @@ namespace Lastfm.Tests
     [TestFixture]
     public class Tests
     {
-        class FakeWebRequestCreator : IWebRequestCreator
+        class FakeWebRequestCreator : IWebRequestCreate
         {
-            public HttpWebRequest Create (string requestUriString)
+            public WebRequest Create (Uri uri)
             {
-                Uri = requestUriString;
-                return (HttpWebRequest)WebRequest.Create (requestUriString);
+                Uri = uri;
+                return (HttpWebRequest)WebRequest.Create (uri);
             }
 
-            internal string Uri { get; set; }
+            internal Uri Uri { get; set; }
         }
 
         [Test]
@@ -54,7 +54,7 @@ namespace Lastfm.Tests
             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);
+            Assert.AreEqual (expected, creator.Uri.ToString ());
         }
 
         [Test]
@@ -66,7 +66,7 @@ namespace Lastfm.Tests
             req.AddParameter ("x", "y");
             req.AddParameter ("a", "b");
             req.Send ();
-            Assert.AreEqual (expected, creator.Uri);
+            Assert.AreEqual (expected, creator.Uri.ToString ());
         }
 
         [Test]
@@ -75,7 +75,7 @@ namespace Lastfm.Tests
             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);
+            Assert.AreEqual (expected, creator.Uri.ToString ());
         }
 
         [Test]
@@ -84,7 +84,7 @@ namespace Lastfm.Tests
             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);
+            Assert.AreEqual (expected, creator.Uri.ToString ());
         }
 
         [Test]
@@ -96,7 +96,7 @@ namespace Lastfm.Tests
             req.AddParameter ("x", "y");
             req.AddParameter ("a", "b");
             req.Send ();
-            Assert.AreEqual (expected, creator.Uri);
+            Assert.AreEqual (expected, creator.Uri.ToString ());
         }
 
         [Test]
@@ -108,7 +108,7 @@ namespace Lastfm.Tests
             req.AddParameter ("x", "y");
             req.AddParameter ("a", "b");
             req.Send ();
-            Assert.AreEqual (expected, creator.Uri);
+            Assert.AreEqual (expected, creator.Uri.ToString ());
         }
 
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]