Skip to content

Commit 48ffd42

Browse files
authored
Merge pull request #44 from RusticiSoftware/ExtraParams
Add the params in extended to the request URL
2 parents c090627 + fe861eb commit 48ffd42

2 files changed

Lines changed: 45 additions & 22 deletions

File tree

TinCan/RemoteLRS.cs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ limitations under the License.
2222
using Newtonsoft.Json.Linq;
2323
using TinCan.Documents;
2424
using TinCan.LRSResponses;
25+
using System.Linq;
2526

2627
namespace TinCan
2728
{
@@ -30,7 +31,7 @@ public class RemoteLRS : ILRS
3031
public Uri endpoint { get; set; }
3132
public TCAPIVersion version { get; set; }
3233
public String auth { get; set; }
33-
public Dictionary<String, String> extended { get; set; }
34+
public Dictionary<String, String> extended { get; set; } = new Dictionary<String, String>();
3435

3536
public void SetAuth(String username, String password)
3637
{
@@ -51,8 +52,8 @@ private class MyHTTPRequest
5152
{
5253
public String method { get; set; }
5354
public String resource { get; set; }
54-
public Dictionary<String, String> queryParams { get; set; }
55-
public Dictionary<String, String> headers { get; set; }
55+
public Dictionary<String, String> queryParams { get; set; } = new Dictionary<String, String>();
56+
public Dictionary<String, String> headers { get; set; } = new Dictionary<String, String>();
5657
public String contentType { get; set; }
5758
public byte[] content { get; set; }
5859
}
@@ -81,6 +82,20 @@ public MyHTTPResponse(HttpWebResponse webResp)
8182
}
8283
}
8384

85+
private string AppendParamsToExistingQueryString(string currentQueryString, IEnumerable<KeyValuePair<string, string>> parameters)
86+
{
87+
foreach (KeyValuePair<String, String> entry in parameters)
88+
{
89+
if (currentQueryString != "")
90+
{
91+
currentQueryString += "&";
92+
}
93+
currentQueryString += HttpUtility.UrlEncode(entry.Key) + "=" + HttpUtility.UrlEncode(entry.Value);
94+
}
95+
96+
return currentQueryString;
97+
}
98+
8499
private MyHTTPResponse MakeSyncRequest(MyHTTPRequest req)
85100
{
86101
String url;
@@ -97,21 +112,13 @@ private MyHTTPResponse MakeSyncRequest(MyHTTPRequest req)
97112
url += req.resource;
98113
}
99114

100-
if (req.queryParams != null)
115+
String qs = "";
116+
qs = AppendParamsToExistingQueryString(qs, req.queryParams);
117+
qs = AppendParamsToExistingQueryString(qs, extended.Where(w => !req.queryParams.ContainsKey(w.Key)));
118+
119+
if (qs != "")
101120
{
102-
String qs = "";
103-
foreach (KeyValuePair<String, String> entry in req.queryParams)
104-
{
105-
if (qs != "")
106-
{
107-
qs += "&";
108-
}
109-
qs += HttpUtility.UrlEncode(entry.Key) + "=" + HttpUtility.UrlEncode(entry.Value);
110-
}
111-
if (qs != "")
112-
{
113-
url += "?" + qs;
114-
}
121+
url += "?" + qs;
115122
}
116123

117124
// TODO: handle special properties we recognize, such as content type, modified since, etc.
@@ -123,13 +130,11 @@ private MyHTTPResponse MakeSyncRequest(MyHTTPRequest req)
123130
{
124131
webReq.Headers.Add("Authorization", auth);
125132
}
126-
if (req.headers != null)
133+
foreach (KeyValuePair<String, String> entry in req.headers)
127134
{
128-
foreach (KeyValuePair<String, String> entry in req.headers)
129-
{
130-
webReq.Headers.Add(entry.Key, entry.Value);
131-
}
135+
webReq.Headers.Add(entry.Key, entry.Value);
132136
}
137+
133138

134139
if (req.contentType != null)
135140
{

TinCanTests/RemoteLRSResourceTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,5 +339,23 @@ public void TestDeleteAgentProfile()
339339
LRSResponse lrsRes = lrs.DeleteAgentProfile(doc);
340340
Assert.IsTrue(lrsRes.success);
341341
}
342+
343+
[Test]
344+
public void TestExtendedParameters()
345+
{
346+
// RemoteLRS doesn't provide a helpful interface for testing
347+
// that we successfully altered the request URL, but this test
348+
// is helpful in manual testing and it at least ensures that
349+
// specifying values in extended doesn't cause errors.
350+
lrs.extended.Add("test", "param");
351+
var doc = new StateDocument();
352+
doc.activity = Support.activity;
353+
doc.agent = Support.agent;
354+
doc.id = "test";
355+
doc.content = System.Text.Encoding.UTF8.GetBytes("Test value");
356+
357+
LRSResponse lrsRes = lrs.SaveState(doc);
358+
Assert.IsTrue(lrsRes.success);
359+
}
342360
}
343361
}

0 commit comments

Comments
 (0)