Permalink
Browse files

added ability to specify route parameters in endpoint info

  • Loading branch information...
1 parent 2a356f9 commit 6c8a8e9625fe1443afc6ccfc32a08430b6ede3ba Antony Denyer committed Dec 16, 2011
@@ -50,6 +50,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="nunit.framework">
+ <HintPath>..\..\lib\nunit.framework.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
@@ -0,0 +1,109 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Xml.Serialization;
+using FakeItEasy;
+using NUnit.Framework;
+using SevenDigital.Api.Schema.Attributes;
+using SevenDigital.Api.Wrapper.EndpointResolution;
+using SevenDigital.Api.Wrapper.EndpointResolution.OAuth;
+using SevenDigital.Api.Wrapper.Utility.Http;
+
+namespace SevenDigital.Api.Wrapper.Unit.Tests.EndpointResolution
+{
+ [TestFixture]
+ public class EndpointResolverTests
+ {
+ private EndpointResolver _endpointResolver;
+
+ [SetUp]
+ public void Setup()
+ {
+ var urlResolver = A.Fake<IUrlResolver>();
+ A.CallTo(() => urlResolver.Resolve(A<Uri>.Ignored, A<string>.Ignored, A<Dictionary<string, string>>.Ignored))
+ .Returns(string.Empty);
+
+ var apiUri = A.Fake<IApiUri>();
+ A.CallTo(() => apiUri.Uri)
+ .Returns("http://uri/");
+
+ _endpointResolver = new EndpointResolver(urlResolver, new UrlSigner(), new AppSettingsCredentials(), apiUri);
+ }
+
+ [Test]
+ public void should_substitue_route_parameters()
+ {
+ var endpointInfo = new EndPointInfo
+ {
+ Uri = "something/{route}",
+ Parameters = new Dictionary<string, string>
+ {
+ {"route","routevalue"}
+ }
+ };
+
+ var result = _endpointResolver.ConstructEndpoint(endpointInfo);
+
+ Assert.That(result,Is.StringContaining("something/routevalue"));
+ }
+
+ [Test]
+ public void should_substitue_multiple_route_parameters()
+ {
+ var endpointInfo = new EndPointInfo
+ {
+ Uri = "something/{firstRoute}/{secondRoute}/thrid/{thirdRoute}",
+ Parameters = new Dictionary<string, string>
+ {
+ {"firstRoute" , "firstValue"},
+ {"secondRoute","secondValue"},
+ {"thirdRoute" , "thirdValue"}
+
+ }
+ };
+
+ var result = _endpointResolver.ConstructEndpoint(endpointInfo);
+
+ Assert.That(result, Is.StringContaining("something/firstvalue/secondvalue/thrid/thirdvalue"));
+ }
+
+ [Test]
+ public void routes_should_be_case_insensitive()
+ {
+ var endpointInfo = new EndPointInfo
+ {
+ Uri = "something/{firstRoUte}/{secOndrouTe}/thrid/{tHirdRoute}",
+ Parameters = new Dictionary<string, string>
+ {
+ {"firstRoute" , "firstValue"},
+ {"secondRoute","secondValue"},
+ {"thirdRoute" , "thirdValue"}
+
+ }
+ };
+
+ var result = _endpointResolver.ConstructEndpoint(endpointInfo);
+
+ Assert.That(result, Is.StringContaining("something/firstvalue/secondvalue/thrid/thirdvalue"));
+ }
+
+ [Test]
+ public void should_handle_dashes_and_numbers()
+ {
+ var endpointInfo = new EndPointInfo
+ {
+ Uri = "something/{route-66}",
+ Parameters = new Dictionary<string, string>
+ {
+ {"route-66","routevalue"}
+ }
+ };
+
+ var result = _endpointResolver.ConstructEndpoint(endpointInfo);
+
+ Assert.That(result, Is.StringContaining("something/routevalue"));
+ }
+
+ }
+}
@@ -1,7 +1,9 @@
using System;
using System.Linq.Expressions;
+using System.Xml.Serialization;
using FakeItEasy;
using NUnit.Framework;
+using SevenDigital.Api.Schema.Attributes;
using SevenDigital.Api.Wrapper.EndpointResolution;
using SevenDigital.Api.Schema;
using System.Threading;
@@ -86,13 +88,13 @@ public void HitEndpointAsync(EndPointInfo endPointInfo, Action<string> payload)
payload(StubPayload);
}
- public string ConstructEndpoint(EndPointInfo endPointInfo) {
+ public string ConstructEndpoint(EndPointInfo endPointInfo)
+ {
throw new NotImplementedException();
}
public string StubPayload { get; set; }
}
}
-
}
@@ -53,6 +53,7 @@
<Compile Include="ApiUri.cs" />
<Compile Include="AppSettingsCredentials.cs" />
<Compile Include="BasketEndpoint\BasketEndpointTests.cs" />
+ <Compile Include="EndpointResolution\EndpointResolverTests.cs" />
<Compile Include="EndpointResolution\OAuth\UrlSignerTests.cs" />
<Compile Include="FluentAPITests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -1,4 +1,6 @@
using System;
+using System.Linq;
+using System.Text.RegularExpressions;
using SevenDigital.Api.Wrapper.EndpointResolution.OAuth;
using SevenDigital.Api.Wrapper.Utility.Http;
using System.Collections.Generic;
@@ -45,7 +47,7 @@ private Uri GetSignedUrl(EndPointInfo endPointInfo)
apiUri = apiUri.Replace("http://", "https://");
var uriString = string.Format("{0}/{1}?oauth_consumer_key={2}&{3}",
- apiUri, endPointInfo.Uri,
+ apiUri, SubstituteRouteParameters(endPointInfo.Uri,endPointInfo.Parameters),
_oAuthCredentials.ConsumerKey,
endPointInfo.Parameters.ToQueryString()).TrimEnd('&');
@@ -55,5 +57,20 @@ private Uri GetSignedUrl(EndPointInfo endPointInfo)
signedUrl = _urlSigner.SignUrl(uriString, endPointInfo.UserToken, endPointInfo.UserSecret, _oAuthCredentials);
return signedUrl;
}
+
+
+ private string SubstituteRouteParameters(string endpointUri, Dictionary<string, string> parameters)
+ {
+ var regex = new Regex("{(.*?)}");
+ var result = regex.Matches(endpointUri);
+ foreach (var match in result)
+ {
+ var key = match.ToString().Remove(match.ToString().Length - 1).Remove(0, 1);
+ var value = parameters.First(x => x.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)).Value;
+ endpointUri = endpointUri.Replace(match.ToString(), value);
+ }
+
+ return endpointUri.ToLower();
+ }
}
}

0 comments on commit 6c8a8e9

Please sign in to comment.