public async Task Given_AuthKey_Options_When_RenderAsync_Invoked_Then_It_Should_Be_Used_As_Request_Key( string queryKey, OpenApiAuthLevelType configuredAuthLevel, string configKey, string expectedRequestKey, bool throwsException = false) { var endpoint = "swagger/ui"; var baseUrl = "https://localhost:7071"; var ui = new SwaggerUI(); ui.AddMetadata(new OpenApiInfo()); var uiType = ui.GetType(); //Generate Request Object with query key var queryDict = new Dictionary <string, StringValues>(); if (queryKey != null) { queryDict["code"] = queryKey; } var req = new Mock <IHttpRequestDataObject>(); req.SetupGet(p => p.Query).Returns(new QueryCollection(queryDict)); uiType.GetField("_req", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ui, req.Object); //Set BaseUrl uiType.GetField("_baseUrl", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ui, baseUrl); //Set html string just to contain url placeholder var swaggerUrlPlaceholder = uiType.GetField("SwaggerUrlPlaceholder", BindingFlags.Static | BindingFlags.NonPublic) .GetRawConstantValue() as string; uiType.GetField("_indexHtml", BindingFlags.Instance | BindingFlags.NonPublic) .SetValue(ui, swaggerUrlPlaceholder); Func <Task <string> > action = async() => await ui.RenderAsync(endpoint, configuredAuthLevel, configKey); if (throwsException) { await action.Should().ThrowAsync <InvalidOperationException>(); } else { var result = await action(); if (expectedRequestKey != null) { result.Should().Contain($"code={expectedRequestKey}"); } else { result.Should().NotContain($"code="); } } }
public void Given_NullOptions_When_AddServer_Invoked_Then_It_Should_Return_BaseUrl(string scheme, string host, string routePrefix, string expected) { var req = new Mock <IHttpRequestDataObject>(); req.SetupGet(p => p.Scheme).Returns(scheme); var hostString = new HostString(host); req.SetupGet(p => p.Host).Returns(hostString); var ui = new SwaggerUI(); ui.AddServer(req.Object, routePrefix, null); var fi = ui.GetType().GetField("_baseUrl", BindingFlags.Instance | BindingFlags.NonPublic); (fi.GetValue(ui) as string).Should().Be(expected); }
public void Given_Options_When_IsAuthKeyRequired_Invoked_Then_It_Should_Return_Result( OpenApiAuthLevelType configuredAuthLevel, string configKey, bool throwsException, bool expected) { var ui = new SwaggerUI(); var method = ui.GetType() .GetMethod("IsAuthKeyRequired", BindingFlags.Instance | BindingFlags.NonPublic); Func <bool> action = () => (bool)method.Invoke(ui, new object[] { configuredAuthLevel, configKey }); if (throwsException) { action.Should().Throw <TargetInvocationException>().And.InnerException.Should() .BeOfType <InvalidOperationException>(); } else { action.Invoke().Should().Be(expected); } }
public void Given_NullOptions_When_AddServer_Invoked_Then_It_Should_Return_SwaggerUIApiPrefix(string scheme, string host, string routePrefix, bool optionsSet, string expected) { var req = new Mock <IHttpRequestDataObject>(); req.SetupGet(p => p.Scheme).Returns(scheme); var hostString = new HostString(host); req.SetupGet(p => p.Host).Returns(hostString); var ui = new SwaggerUI(); var options = new Mock <IOpenApiConfigurationOptions>(); options.SetupGet(p => p.Servers).Returns(new List <OpenApiServer>()); ui.AddServer(req.Object, routePrefix, optionsSet ? options.Object: null); var fi = ui.GetType().GetField("_swaggerUiApiPrefix", BindingFlags.Instance | BindingFlags.NonPublic); (fi.GetValue(ui) as string).Should().Be(expected); }