繁体   English   中英

IdentityServer3 xBehave测试

[英]IdentityServer3 xBehave test

我想为WebApi和IdentityServer编写验收测试。 为了使事情尽可能简单,我从此处复制了整个示例项目,但添加了另一个项目,该项目与控制台客户端基本相同,只是作为验收测试。

我现在唯一的情况是:

namespace SpecTesting
{
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using FluentAssertions;
using IdentityModel.Client;
using Xbehave;

public class JustLikeConsole
{
    private static readonly string ServerUrl = "http://localhost:11111";
    private static readonly string IdentityServerUrl = "http://localhost:5000";

    private IDisposable webApp;
    private IDisposable identityServer;
    private HttpClient appClient;
    private HttpClient identityServerClient;

    [Background]
    public void Background()
    {
        "establish server"._(() =>
        {
            this.identityServer = Microsoft.Owin.Hosting.WebApp.Start<IdSrv.Startup>(IdentityServerUrl);
            this.webApp = Microsoft.Owin.Hosting.WebApp.Start<Apis.Startup>(ServerUrl);
            this.appClient = new HttpClient { BaseAddress = new Uri(ServerUrl) };
            this.identityServerClient = new HttpClient { BaseAddress = new Uri(IdentityServerUrl) };
        }).Teardown(() =>
        {
            this.identityServerClient.Dispose();
            this.appClient.Dispose();
            this.webApp.Dispose();
            this.identityServer.Dispose();
        });
    }

    [Scenario]
    public void SimplestScenario(HttpResponseMessage response)
    {
        var clientId = "silicon";
        var clientSecret = "F621F470-9731-4A25-80EF-67A6F7C5F4B8";
        var expectedJson = $"{{ client: '{clientId}' }}";

        "get token"._(() =>
        {
            var client = new TokenClient(
                $"{IdentityServerUrl}/connect/token",
                clientId,
                clientSecret);

            var token = client.RequestClientCredentialsAsync("api1").Result;
            this.appClient.SetBearerToken(token.AccessToken);
        });

        "when calling the service"._(()
            => response = this.appClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/test"), CancellationToken.None).Result);

        "it should return status code 'OK'"._(()
            => response.StatusCode.Should().Be(HttpStatusCode.OK));

        "it should equal expected json"._(()
            => response.Content.ReadAsStringAsync().Result.Should().Be(expectedJson));
    }
}
}

现在,我总是得到状态代码“未经授权”而不是“确定”。 当我通过控制台客户端调用两个服务器时,它可以正常工作。 非常沮丧。

更新我用以下几行替换了“调用服务时”步骤,以增强简单性:

var client = new HttpClient();
client.SetBearerToken(token.AccessToken);
var result = client.GetStringAsync($"{ServerUrl}/test").Result;

问题仍然相同:现在抛出HttpRequestException(未经授权的401)。

同时,我找到了它不起作用的原因:

原来,我必须在WebApi上使用IdentityServer3.AccessTokenValidation的2.6.0版或更早版本。 一旦我更新到2.7.0或更高版本,它将停止工作。 我没有时间找出这两个版本之间到底发生了什么变化,从而找出导致问题的原因。 但是我可以将其隔离到IdentityServer3.AccessTokenValidation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM