コード例 #1
0
        public async Task AuthorizedCredentialsTestWithDi()
        {
            using var server        = new TestServer(WebHostBuilderHelper.CreateBuilder());
            using HttpClient client = server.CreateClient();

            // Arrange
            client.DefaultRequestHeaders.Add(HeaderNames.Authorization, AuthorizationHeaderHelper.GetBasic());

            // Act
            HttpResponseMessage response = await client.GetAsync("api/test");

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "StatusCode != OK");
        }
コード例 #2
0
        public async Task UnauthorizedValidCredentialsTestWithOptions()
        {
            using var server        = new TestServer(WebHostBuilderHelper.CreateBuilder(o => { }));
            using HttpClient client = server.CreateClient();

            // Arrange
            client.DefaultRequestHeaders.Add(HeaderNames.Authorization, AuthorizationHeaderHelper.GetBasic());

            // Act
            HttpResponseMessage response = await client.GetAsync("api/test");

            // Assert
            AuthenticationHeaderValue wwwAuth = response.Headers.WwwAuthenticate.Single();
            NameValueHeaderValue      nvh     = NameValueHeaderValue.Parse(wwwAuth.Parameter);

            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode, "StatusCode != Unauthorized");
            Assert.AreEqual("Basic", wwwAuth.Scheme, "Scheme != Basic");
            Assert.AreEqual("realm", nvh.Name, "!realm");
            Assert.AreEqual("\"Basic Realm\"", nvh.Value, "!basic realm");
        }