Skip to content

Commit

Permalink
Merge pull request #42 from smartystreets/xan/fix-url-prefix-bug
Browse files Browse the repository at this point in the history
Fix tests; Fix urlPrefix bug
  • Loading branch information
XanSmarty authored Mar 21, 2024
2 parents 126c008 + 10cc056 commit 532e275
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/sdk/InternationalAutcompleteApi/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static Request BuildRequest(Lookup lookup)
var request = new Request();

if (lookup.AddressID != null) {
request.SetUrlPrefix("/" + lookup.AddressID);
request.SetUrlComponents("/" + lookup.AddressID);
}

request.SetParameter("search", lookup.Search);
Expand Down
11 changes: 6 additions & 5 deletions src/sdk/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class Request
{
private string urlPrefix;
private string urlComponents;
private readonly Dictionary<string, string> parameters;
private byte[] payload;

Expand All @@ -29,6 +30,7 @@ public Request()
this.Headers = new Dictionary<string, string>();
this.parameters = new Dictionary<string, string>();
this.urlPrefix = "";
this.urlComponents = "";
}

public void SetHeader(string header, string value)
Expand All @@ -44,14 +46,13 @@ public void SetParameter(string name, string value)
this.parameters[name] = value;
}

public void SetUrlPrefix(string value)
{
this.urlPrefix = value;
public void SetUrlComponents(string value) {
this.urlComponents = value;
}

public String GetUrlPrefix()
public void SetUrlPrefix(string value)
{
return this.urlPrefix;
this.urlPrefix = value + this.urlComponents;
}

public string GetUrl()
Expand Down
6 changes: 1 addition & 5 deletions src/sdk/URLPrefixSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ public URLPrefixSender(string urlPrefix, ISender inner)

public Response Send(Request request)
{
if (request.GetUrlPrefix() != null) {
request.SetUrlPrefix(this.urlPrefix + request.GetUrlPrefix());
} else {
request.SetUrlPrefix(this.urlPrefix);
}
request.SetUrlPrefix(this.urlPrefix);
return this.inner.Send(request);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/USEnrichmentApi/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private void Send(Lookup lookup)
private SmartyStreets.Request BuildRequest(Lookup lookup)
{
SmartyStreets.Request request = new SmartyStreets.Request();
request.SetUrlPrefix("/" + lookup.GetSmartyKey() + "/" + lookup.GetDatasetName() + "/" + lookup.GetDataSubsetName());
request.SetUrlComponents("/" + lookup.GetSmartyKey() + "/" + lookup.GetDatasetName() + "/" + lookup.GetDataSubsetName());
return request;
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/tests/URLPrefixSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void TestRequestURLPresent()
var urlPrefixSender = new URLPrefixSender("http://localhost/", mockSender);

var request = new Request();
request.SetUrlPrefix("jimbo");
request.SetUrlComponents("jimbo");

urlPrefixSender.Send(request);

Expand All @@ -32,5 +32,20 @@ public void TestRequestURLAbsent()

Assert.AreEqual("http://localhost/?", request.GetUrl());
}

[Test]
public void TestMultipleSends()
{
var mockSender = new MockSender(null);
var urlPrefixSender = new URLPrefixSender("http://localhost/", mockSender);

var request = new Request();
request.SetUrlComponents("jimbo");

urlPrefixSender.Send(request);
urlPrefixSender.Send(request);

Assert.AreEqual("http://localhost/jimbo?", request.GetUrl());
}
}
}
16 changes: 7 additions & 9 deletions src/tests/USEnrichmentApi/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,30 @@ public class ClientTests
public void Setup()
{
this.capturingSender = new RequestCapturingSender();
this.urlSender = new URLPrefixSender("http://localhost/", this.capturingSender);
this.urlSender = new URLPrefixSender("http://localhost", this.capturingSender);
}


[Test]
public void TestSendingFullyPopulatedPrincipalLookup(string matchStrategy)
public void TestSendingFullyPopulatedPrincipalLookup()
{
var serializer = new FakeSerializer(null);
var client = new Client(this.urlSender, serializer);
const string expectedUrlTemplate = "http://localhost/1/property/principal";
var expectedUrl = string.Format(expectedUrlTemplate, matchStrategy);
const string expectedUrl= "http://localhost/1/property/principal?";

client.SendPropertyPrincipalLookup("1");

Assert.AreEqual(expectedUrl, this.capturingSender.Request.GetUrl());
}

[Test]
public void TestSendingFullyPopulatedFinancialLookup(string matchStrategy)
public void TestSendingFullyPopulatedFinancialLookup()
{
var serializer = new FakeSerializer(null);
var client = new Client(this.urlSender, serializer);
const string expectedUrlTemplate = "http://localhost/1/property/financial";
var expectedUrl = string.Format(expectedUrlTemplate, matchStrategy);
const string expectedUrl= "http://localhost/1/property/financial?";

client.SendPropertyPrincipalLookup("1");
client.SendPropertyFinancialLookup("1");

Assert.AreEqual(expectedUrl, this.capturingSender.Request.GetUrl());
}
Expand All @@ -50,7 +48,7 @@ public void TestRejectNullLookup()
var serializer = new FakeSerializer(null);
var client = new Client(this.urlSender, serializer);

Assert.Throws<ArgumentNullException>(() => client.SendPropertyFinancialLookup(null));
Assert.Throws<SmartyStreets.SmartyException>(() => client.SendPropertyFinancialLookup(null));
}
}
}

0 comments on commit 532e275

Please sign in to comment.