Skip to content

Commit

Permalink
Fix percent encoding. See #18
Browse files Browse the repository at this point in the history
  • Loading branch information
tipstrade committed May 20, 2021
1 parent b7f3de4 commit 88d77e0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Tests/AntiFraudTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void TestGetAntiFraudHeaders() {
[Fact]
public void TestGetPropertiesForMethod() {
var props = AntiFraud.AntiFraud.GetPropertiesForMethod(ConnectionMethod.DESKTOP_APP_DIRECT);
Assert.Equal(12, props.Count());
Assert.Equal(14, props.Count());
}
}
}
5 changes: 4 additions & 1 deletion Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ protected Client GetClient() {
client.AntiFraud.PopulateLocalIPs();
client.AntiFraud.PopulateMACAddresses();
client.AntiFraud.PopulateUserAgent();
client.AntiFraud.VendorForwarded = new Forwarded[] {
new Forwarded { By = System.Net.IPAddress.Parse("8.8.8.8"), For = System.Net.IPAddress.Parse("fe80::21a6:9255:4c0b:78e4%14")}
};

// Even though the documentation states that that these are optional, the API returns an error
client.AntiFraud.UserAgent.DeviceManufacturer = "Dell";
client.AntiFraud.UserAgent.DeviceModel = "XPS";
client.AntiFraud.UserAgent.DeviceModel = "XPS Gaming PC";

client.AntiFraud.MultiFactor = new[] {
new MultiFactor() {
Expand Down
2 changes: 1 addition & 1 deletion TipsTrade-HMRC/AntiFraud/AntiFraud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private Dictionary<string, string> GetAntiFraudHeaders(List<string> errors) {

string headerValue;
if (value is string str) {
headerValue = str; // string are enumerable so catch them here
headerValue = HttpUtility.UrlEncode(str); // string are enumerable so catch them here

} else if (value is IDictionary dict) {
var sb = new StringBuilder();
Expand Down
3 changes: 2 additions & 1 deletion TipsTrade-HMRC/AntiFraud/Forwarded.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net;
using System.Web;

namespace TipsTrade.HMRC.AntiFraud {
/// <summary>Represents an object that contains information on hops over the internet that terminate TLS.</summary>
Expand All @@ -15,7 +16,7 @@ public string GetHeaderValue() {
if (By == null) throw new InvalidOperationException($"The {nameof(By)} property cannot be null.");
if (For == null) throw new InvalidOperationException($"The {nameof(For)} property cannot be null.");

return $"by={By}&for={For}";
return $"by={HttpUtility.UrlEncode(By.ToString())}&for={HttpUtility.UrlEncode(For.ToString())}";
}
}
}
10 changes: 5 additions & 5 deletions TipsTrade-HMRC/AntiFraud/UserAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class UserAgent : IAntiFraudValue {

/// <summary>Gets or sets the device model.</summary>
public string DeviceModel { get; set; }

/// <summary>Gets or sets the operating system family.</summary>
public string OSFamily { get; set; }

Expand All @@ -20,10 +20,10 @@ public class UserAgent : IAntiFraudValue {
/// <summary>Retuns a string that contains the anti fraud header value.</summary>
public string GetHeaderValue() {
var dict = new Dictionary<string, string> {
{"os-family", OSFamily },
{"os-version", OSVersion },
{"device-manufacturer", DeviceManufacturer },
{"device-model", DeviceModel }
{"os-family", HttpUtility.UrlEncode(OSFamily) },
{"os-version", HttpUtility.UrlEncode(OSVersion) },
{"device-manufacturer", HttpUtility.UrlEncode(DeviceManufacturer) },
{"device-model", HttpUtility.UrlEncode(DeviceModel) }
};

return dict.GetHeaderValue();
Expand Down

0 comments on commit 88d77e0

Please sign in to comment.