From 6ce393d0f50314cd2af3d0f13c84a38c78159748 Mon Sep 17 00:00:00 2001 From: Quinn Purdy Date: Mon, 18 Dec 2023 10:57:54 -0500 Subject: [PATCH] Added test to confirm that the API key is being passed in with requests by WaaS's HTTP Client --- Assets/SequenceSDK/WaaS/HttpClient.cs | 14 ++++++++++++-- .../SequenceSDK/WaaS/Tests/HttpClientTests.cs | 19 +++++++++++++++++++ .../WaaS/Tests/HttpClientTests.cs.meta | 3 +++ .../SequenceSDK/WaaS/Tests/WaaSTests.asmdef | 3 ++- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 Assets/SequenceSDK/WaaS/Tests/HttpClientTests.cs create mode 100644 Assets/SequenceSDK/WaaS/Tests/HttpClientTests.cs.meta diff --git a/Assets/SequenceSDK/WaaS/HttpClient.cs b/Assets/SequenceSDK/WaaS/HttpClient.cs index ae9e4d34..7b23d4c5 100644 --- a/Assets/SequenceSDK/WaaS/HttpClient.cs +++ b/Assets/SequenceSDK/WaaS/HttpClient.cs @@ -37,7 +37,8 @@ public void AddDefaultHeader(string key, string value) this._defaultHeaders[key] = value; } - public async Task SendRequest(string path, T args, [CanBeNull] Dictionary headers = null, string overrideUrl = null) + public (UnityWebRequest, string, string) BuildRequest(string path, T args, + [CanBeNull] Dictionary headers = null, string overrideUrl = null) { string url = _url + "/" + path; if (overrideUrl != null) @@ -74,7 +75,16 @@ public async Task SendRequest(string path, T args, [CanBeNull] Dictio string method = request.method; string headersString = ExtractHeaders(request); string curlRequest = $"curl -X {method} '{url}' {headersString} -d '{requestJson}'"; - Debug.Log("Equivalent curl command: " + curlRequest); + + return (request, curlRequest, url); + } + + public async Task SendRequest(string path, T args, [CanBeNull] Dictionary headers = null, string overrideUrl = null) + { + (UnityWebRequest, string, string) newRequest = BuildRequest(path, args, headers, overrideUrl); + UnityWebRequest request = newRequest.Item1; + string curlRequest = newRequest.Item2; + string url = newRequest.Item3; try { diff --git a/Assets/SequenceSDK/WaaS/Tests/HttpClientTests.cs b/Assets/SequenceSDK/WaaS/Tests/HttpClientTests.cs new file mode 100644 index 00000000..08430b9d --- /dev/null +++ b/Assets/SequenceSDK/WaaS/Tests/HttpClientTests.cs @@ -0,0 +1,19 @@ +using NUnit.Framework; +using Sequence.Config; + +namespace Sequence.WaaS.Tests +{ + public class HttpClientTests + { + [Test] + public void TestHttpClientIncludesAPIKey() + { + HttpClient client = new HttpClient("https://randomurl.com"); + var request = client.BuildRequest("", null); + string header = request.Item1.GetRequestHeader("X-Access-Token"); + Assert.IsTrue(header.Length > 0); + Assert.AreEqual(SequenceConfig.GetConfig().BuilderAPIKey_Prod, header); + Assert.IsTrue(request.Item2.Contains(header)); + } + } +} \ No newline at end of file diff --git a/Assets/SequenceSDK/WaaS/Tests/HttpClientTests.cs.meta b/Assets/SequenceSDK/WaaS/Tests/HttpClientTests.cs.meta new file mode 100644 index 00000000..0c17261c --- /dev/null +++ b/Assets/SequenceSDK/WaaS/Tests/HttpClientTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 00108527afd24cba8bed98de4fb652d6 +timeCreated: 1702910013 \ No newline at end of file diff --git a/Assets/SequenceSDK/WaaS/Tests/WaaSTests.asmdef b/Assets/SequenceSDK/WaaS/Tests/WaaSTests.asmdef index 42b84235..455e6963 100644 --- a/Assets/SequenceSDK/WaaS/Tests/WaaSTests.asmdef +++ b/Assets/SequenceSDK/WaaS/Tests/WaaSTests.asmdef @@ -9,7 +9,8 @@ "GUID:27619889b8ba8c24980f49ee34dbb44a", "GUID:0acc523941302664db1f4e527237feb3", "GUID:6055be8ebefd69e48b49212b09b47b2f", - "GUID:72aec190c59574cbabd6cbdd81011e4d" + "GUID:72aec190c59574cbabd6cbdd81011e4d", + "GUID:a35e3a53d4439435f8b36ed2c6158cd8" ], "includePlatforms": [ "Editor",