-
Notifications
You must be signed in to change notification settings - Fork 19
Add UploadSource header support for XGPM/XVC1 path #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
src/PackageUploader.ClientApi.Test/IngestionHttpClientWireTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using PackageUploader.ClientApi.Client.Ingestion; | ||
| using PackageUploader.ClientApi.Client.Ingestion.Client; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace PackageUploader.ClientApi.Test; | ||
|
|
||
| /// <summary> | ||
| /// Wire-level integration tests that verify the UploadSource header actually | ||
| /// travels over a real TCP connection. | ||
| /// </summary> | ||
| [TestClass] | ||
| [TestCategory("Integration")] | ||
| public class IngestionHttpClientWireTests | ||
| { | ||
| private static async Task<(Dictionary<string, string> Headers, List<string> RawHeaderLines)> | ||
| CaptureWireRequestAsync(TcpListener listener, TimeSpan timeout) | ||
| { | ||
| using var cts = new CancellationTokenSource(timeout); | ||
| using var tcpClient = await listener.AcceptTcpClientAsync(cts.Token); | ||
| using var stream = tcpClient.GetStream(); | ||
| using var reader = new StreamReader(stream, Encoding.ASCII, leaveOpen: true); | ||
|
|
||
| var rawHeaderLines = new List<string>(); | ||
| var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| // Read request line | ||
| string requestLine = await reader.ReadLineAsync(cts.Token); | ||
|
|
||
| // Read headers until blank line | ||
| while (true) | ||
| { | ||
| string line = await reader.ReadLineAsync(cts.Token); | ||
| if (string.IsNullOrEmpty(line)) break; | ||
| rawHeaderLines.Add(line); | ||
| int ci = line.IndexOf(":"); | ||
| if (ci > 0) | ||
| { | ||
| headers[line[..ci].Trim()] = line[(ci + 1)..].Trim(); | ||
| } | ||
| } | ||
|
|
||
| // Send minimal 200 OK with JSON body | ||
| const string body = "{\"id\":\"wire-test\"}"; | ||
| string resp = "HTTP/1.1 200 OK\r\n" + | ||
| "Content-Type: application/json\r\n" + | ||
| $"Content-Length: {Encoding.UTF8.GetByteCount(body)}\r\n" + | ||
| "Connection: close\r\n" + | ||
| "\r\n" + body; | ||
| await stream.WriteAsync(Encoding.UTF8.GetBytes(resp), cts.Token); | ||
| await stream.FlushAsync(cts.Token); | ||
|
|
||
| return (headers, rawHeaderLines); | ||
| } | ||
|
|
||
| private static async Task<(Dictionary<string, string> Headers, List<string> RawHeaderLines)> | ||
| SendRequestAndCaptureHeaders(UploadSourceConfig config) | ||
| { | ||
| var listener = new TcpListener(IPAddress.Loopback, 0); | ||
| listener.Start(); | ||
| try | ||
| { | ||
| int port = ((IPEndPoint)listener.LocalEndpoint).Port; | ||
| var serverTask = CaptureWireRequestAsync(listener, TimeSpan.FromSeconds(10)); | ||
|
|
||
| using var httpClient = new HttpClient | ||
| { | ||
| BaseAddress = new Uri($"http://127.0.0.1:{port}/"), | ||
| Timeout = TimeSpan.FromSeconds(10), | ||
| }; | ||
|
|
||
| var client = new IngestionHttpClient( | ||
| new NullLogger<IngestionHttpClient>(), httpClient, null, config); | ||
|
|
||
| try | ||
| { | ||
| await client.GetGameProductByLongIdAsync("wire-test", CancellationToken.None); | ||
| } | ||
| catch { } | ||
|
|
||
| return await serverTask.WaitAsync(TimeSpan.FromSeconds(10)); | ||
| } | ||
| finally | ||
| { | ||
| listener.Stop(); | ||
| } | ||
| } | ||
|
|
||
| #region Wire-Level UploadSource Tests | ||
|
|
||
| [TestMethod] | ||
| public async Task WireLevel_DefaultConfig_SendsPackageUploaderHeader() | ||
| { | ||
| var (headers, _) = await SendRequestAndCaptureHeaders(null); | ||
|
|
||
| Assert.IsTrue(headers.ContainsKey("UploadSource"), | ||
| "UploadSource header must be present on the wire. " + | ||
| "Headers: " + string.Join(", ", headers.Keys)); | ||
|
|
||
| Assert.AreEqual("PackageUploader", headers["UploadSource"], | ||
| "Default UploadSource must be PackageUploader on the wire"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task WireLevel_XgpmConfig_SendsXgpmHeader() | ||
| { | ||
| var config = new UploadSourceConfig { UploadSource = "XGPM" }; | ||
| var (headers, _) = await SendRequestAndCaptureHeaders(config); | ||
|
|
||
| Assert.IsTrue(headers.ContainsKey("UploadSource"), | ||
| "UploadSource header must be present on the wire"); | ||
|
|
||
| Assert.AreEqual("XGPM", headers["UploadSource"], | ||
| "UploadSource must be XGPM on the wire when configured"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task WireLevel_AllStandardHeaders_ArriveOnServer() | ||
| { | ||
| var config = new UploadSourceConfig { UploadSource = "XGPM" }; | ||
| var (headers, _) = await SendRequestAndCaptureHeaders(config); | ||
|
|
||
| Assert.IsTrue(headers.ContainsKey("Request-ID"), | ||
| "Request-ID must be present on the wire"); | ||
| Assert.IsFalse(string.IsNullOrWhiteSpace(headers["Request-ID"]), | ||
| "Request-ID must have a non-empty value"); | ||
|
|
||
| Assert.IsTrue(headers.ContainsKey("MethodOfAccess"), | ||
| "MethodOfAccess must be present on the wire"); | ||
|
|
||
| Assert.IsTrue(headers.ContainsKey("UploadSource")); | ||
| Assert.AreEqual("XGPM", headers["UploadSource"]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task WireLevel_UploadSourceHeader_AppearsExactlyOnce() | ||
| { | ||
| var config = new UploadSourceConfig { UploadSource = "XGPM" }; | ||
| var (_, rawHeaderLines) = await SendRequestAndCaptureHeaders(config); | ||
|
|
||
| int count = rawHeaderLines | ||
| .Count(l => l.StartsWith("UploadSource:", StringComparison.OrdinalIgnoreCase)); | ||
|
|
||
| Assert.AreEqual(1, count, | ||
| $"UploadSource header must appear exactly once. Found {count}."); | ||
| } | ||
|
|
||
| #endregion | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.