Skip to content

Commit

Permalink
added ability to specify route parameters in endpoint info
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony Denyer committed Dec 16, 2011
1 parent 2a356f9 commit 6c8a8e9
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}
}
6 changes: 4 additions & 2 deletions src/SevenDigital.Api.Wrapper.Unit.Tests/FluentAPITests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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; }
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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('&');

Expand All @@ -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.