Add tests for match

This commit is contained in:
jvyden 2021-10-19 21:47:32 -04:00
parent a459ef2302
commit 15e3870620
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 60 additions and 1 deletions

View file

@ -1,5 +1,6 @@
using System.IO; using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Serialization; using System.Xml.Serialization;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -52,10 +53,22 @@ namespace ProjectLighthouse.Tests {
return await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath))); return await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath)));
} }
public async Task<HttpResponseMessage> AuthenticatedUploadFileRequest(string endpoint, string filePath) { public async Task<HttpResponseMessage> UploadDataRequest(string endpoint, byte[] data) {
return await this.Client.PostAsync(endpoint, new ByteArrayContent(data));
}
public async Task<HttpResponseMessage> AuthenticatedUploadFileRequest(string endpoint, string filePath, string mmAuth) {
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, endpoint); using var requestMessage = new HttpRequestMessage(HttpMethod.Post, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new StringContent(await File.ReadAllTextAsync(filePath)); requestMessage.Content = new StringContent(await File.ReadAllTextAsync(filePath));
return await this.Client.SendAsync(requestMessage); return await this.Client.SendAsync(requestMessage);
} }
public async Task<HttpResponseMessage> AuthenticatedUploadDataRequest(string endpoint, byte[] data, string mmAuth) {
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new ByteArrayContent(data);
return await this.Client.SendAsync(requestMessage);
}
} }
} }

View file

@ -0,0 +1,46 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using ProjectLighthouse.Types;
using Xunit;
using Xunit.Abstractions;
namespace ProjectLighthouse.Tests {
public class MatchTests : LighthouseTest {
private readonly ITestOutputHelper testOutputHelper;
private static SemaphoreSlim semaphore = new(1, 1);
public MatchTests(ITestOutputHelper testOutputHelper) {
this.testOutputHelper = testOutputHelper;
}
[DatabaseFact]
public async Task ShouldReturnOk() {
LoginResult loginResult = await this.Authenticate();
await semaphore.WaitAsync();
HttpResponseMessage result = await AuthenticatedUploadDataRequest("LITTLEBIGPLANETPS3_XML/match", Array.Empty<byte>(), loginResult.AuthTicket);
Assert.True(result.IsSuccessStatusCode);
semaphore.Release();
}
public async Task<int> GetPlayerCount() => Convert.ToInt32(await this.Client.GetStringAsync("LITTLEBIGPLANETPS3_XML/totalPlayerCount"));
[DatabaseFact]
public async Task ShouldIncrementPlayerCount() {
LoginResult loginResult = await this.Authenticate(new Random().Next());
await semaphore.WaitAsync();
int oldPlayerCount = await this.GetPlayerCount();
HttpResponseMessage result = await AuthenticatedUploadDataRequest("LITTLEBIGPLANETPS3_XML/match", Array.Empty<byte>(), loginResult.AuthTicket);
Assert.True(result.IsSuccessStatusCode);
int playerCount = await this.GetPlayerCount();
semaphore.Release();
Assert.Equal(oldPlayerCount + 1, playerCount);
}
}
}