public async Task MAuthAuthenticatingHandler_WithValidRequest_WillAuthenticate(string method) { // Arrange var testData = await TestData.For(method); var actual = new AssertSigningHandler(); var handler = new MAuthAuthenticatingHandler(new MAuthWebApiOptions() { ApplicationUuid = TestExtensions.ServerUuid, MAuthServiceUrl = TestExtensions.TestUri, PrivateKey = TestExtensions.ServerPrivateKey, MAuthServerHandler = new MAuthServerHandler() }, actual); using (var server = new HttpClient(handler)) { // Act var response = await server.SendAsync( await testData.Request.Sign(TestExtensions.ClientOptions(testData.SignedTime))); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(testData.MAuthHeader, actual.MAuthHeader); Assert.Equal(testData.SignedTime, actual.MAuthTimeHeader.FromUnixTimeSeconds()); } }
public async Task MAuthMiddleware_WithValidRequest_WillAuthenticate(string method) { // Arrange var testData = await TestData.For(method); using (var server = new TestServer(new WebHostBuilder().Configure(app => { app.UseMAuthAuthentication(options => { options.ApplicationUuid = TestExtensions.ServerUuid; options.MAuthServiceUrl = TestExtensions.TestUri; options.PrivateKey = TestExtensions.ServerPrivateKey; options.MAuthServerHandler = new MAuthServerHandler(); options.HideExceptionsAndReturnUnauthorized = false; }); app.Run(async context => await new StreamWriter(context.Response.Body).WriteAsync("Done.")); }))) { // Act var response = await server.CreateClient().SendAsync( await testData.Request.Sign(TestExtensions.ClientOptions(testData.SignedTime))); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); } }
public async Task MAuthMiddleware_WithValidRequest_WillAuthenticate(string method) { // Arrange var testData = await TestData.For(method); using (var server = TestServer.Create(app => { app.UseMAuthAuthentication(options => { options.ApplicationUuid = TestExtensions.ServerUuid; options.MAuthServiceUrl = TestExtensions.TestUri; options.PrivateKey = TestExtensions.ServerPrivateKey; options.MAuthServerHandler = new MAuthServerHandler(); }); app.Run(async context => await context.Response.WriteAsync("Done.")); })) { // Act var response = await server.HttpClient.SendAsync( await testData.Request.Sign(TestExtensions.ClientOptions(testData.SignedTime))); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); } }
public async Task SignRequest_WithValidRequest_WillSignProperly(string method) { // Arrange var testData = await TestData.For(method); var expectedMAuthHeader = $"MWS {TestExtensions.ClientUuid.ToHyphenString()}:{testData.Payload}"; // Act var actual = await testData.Request.Sign(TestExtensions.ClientOptions(testData.SignedTime)); // Assert Assert.Equal(expectedMAuthHeader, actual.Headers.GetFirstValueOrDefault <string>(Constants.MAuthHeaderKey)); Assert.Equal( testData.SignedTime.ToUnixTimeSeconds(), actual.Headers.GetFirstValueOrDefault <long>(Constants.MAuthTimeHeaderKey) ); }
public static async Task SignRequest_WithValidRequest_WillSignProperly(string method) { // Arrange var testData = await method.FromResource(); var expectedMAuthHeader = testData.MAuthHeader; // Act var actual = await testData.ToHttpRequestMessage().Sign(TestExtensions.ClientOptions(testData.SignedTime)); // Assert Assert.Equal(expectedMAuthHeader, actual.Headers.GetFirstValueOrDefault <string>(Constants.MAuthHeaderKey)); Assert.Equal( testData.SignedTime.ToUnixTimeSeconds(), actual.Headers.GetFirstValueOrDefault <long>(Constants.MAuthTimeHeaderKey) ); }
public async Task SendAsync_WithValidRequest_WillSignProperly(string method) { // Arrange var testData = await TestData.For(method); var actual = new AssertSigningHandler(); var signingHandler = new MAuthSigningHandler(TestExtensions.ClientOptions(testData.SignedTime), actual); // Act using (var client = new HttpClient(signingHandler)) { await client.SendAsync(testData.Request); } // Assert Assert.Equal(testData.MAuthHeader, actual.MAuthHeader); Assert.Equal(testData.SignedTime, actual.MAuthTimeHeader.FromUnixTimeSeconds()); }
public static async Task SendAsync_WithDefault_WillSignProperly_BothMWSAndMWSV2(string method) { // Arrange var testData = await method.FromResource(); var actual = new AssertSigningHandler(); var signingHandler = new MAuthSigningHandler(TestExtensions.ClientOptions(testData.SignedTime), actual); // Act using (var client = new HttpClient(signingHandler)) { await client.SendAsync(testData.ToDefaultHttpRequestMessage()); } // Assert Assert.Equal(testData.MAuthHeader, actual.MAuthHeader); Assert.Equal(testData.SignedTime, long.Parse(actual.MAuthTimeHeader).FromUnixTimeSeconds()); Assert.Equal(testData.MAuthHeaderV2, actual.MAuthHeaderV2); Assert.Equal(testData.SignedTime, long.Parse(actual.MAuthTimeHeader).FromUnixTimeSeconds()); }
public static async Task SendAsync_WithDisableV1_WillSignProperlyWithMWSV2(string method) { // Arrange var testData = await method.FromResourceV2(); var actual = new AssertSigningHandler(); var version = MAuthVersion.MWSV2; var clientOptions = TestExtensions.ClientOptions(testData.SignedTime); clientOptions.DisableV1 = true; var signingHandler = new MAuthSigningHandler(clientOptions, actual); // Act using (var client = new HttpClient(signingHandler)) { await client.SendAsync(testData.ToHttpRequestMessage(version)); } // Assert Assert.Equal(testData.MAuthHeaderV2, actual.MAuthHeaderV2); Assert.Equal(testData.SignedTime, long.Parse(actual.MAuthTimeHeaderV2).FromUnixTimeSeconds()); }
public async Task MAuthMiddleware_WithNonSeekableBodyStream_WillRestoreBodyStream(string method) { // Arrange var testData = await TestData.For(method); var canSeek = false; var body = string.Empty; using (var server = new TestServer(new WebHostBuilder().Configure(app => { app .UseMiddleware <RequestBodyAsNonSeekableMiddleware>() .UseMAuthAuthentication(options => { options.ApplicationUuid = TestExtensions.ServerUuid; options.MAuthServiceUrl = TestExtensions.TestUri; options.PrivateKey = TestExtensions.ServerPrivateKey; options.MAuthServerHandler = new MAuthServerHandler(); }) .Run(async context => { canSeek = context.Request.Body.CanSeek; context.Request.Body.Seek(0, SeekOrigin.Begin); body = await new StreamReader(context.Request.Body).ReadToEndAsync(); }); }))) { // Act var response = await server.CreateClient().SendAsync( await testData.Request.Sign(TestExtensions.ClientOptions(testData.SignedTime))); // Assert Assert.True(canSeek); Assert.Equal(testData.Content ?? string.Empty, body); } }
public async Task MAuthMiddleware_WithNonSeekableBodyStream_WillRestoreBodyStream(string method) { // Arrange var testData = await TestData.For(method); var canSeek = false; var body = string.Empty; using (var server = WebApp.Start("http://localhost:29999/", app => { app.UseMAuthAuthentication(options => { options.ApplicationUuid = TestExtensions.ServerUuid; options.MAuthServiceUrl = TestExtensions.TestUri; options.PrivateKey = TestExtensions.ServerPrivateKey; options.MAuthServerHandler = new MAuthServerHandler(); }); app.Run(async context => { canSeek = context.Request.Body.CanSeek; body = await new StreamReader(context.Request.Body).ReadToEndAsync(); await context.Response.WriteAsync("Done."); }); })) { // Act var response = await new HttpClient().SendAsync( await testData.Request.Sign(TestExtensions.ClientOptions(testData.SignedTime))); // Assert Assert.True(canSeek); Assert.Equal(testData.Content ?? string.Empty, body); } }