首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IdentityServer3 xBehave试验

IdentityServer3 xBehave试验
EN

Stack Overflow用户
提问于 2016-11-07 10:21:30
回答 1查看 69关注 0票数 0

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

我现在唯一想到的情况是:

代码语言:javascript
复制
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));
    }
}
}

我现在总是得到状态代码‘未经授权’,而不是'ok‘。当我通过控制台客户端调用这两个服务器时,它按照预期工作。非常令人沮丧。

Update I用以下行替换了“调用服务时”步骤,以增强简单性:

代码语言:javascript
复制
var client = new HttpClient();
client.SetBearerToken(token.AccessToken);
var result = client.GetStringAsync($"{ServerUrl}/test").Result;

问题仍然是一样的:现在抛出一个HttpRequestException (401未经授权)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-09 10:15:58

与此同时,我发现了它不起作用的原因:

结果显示,我必须在WebApi上使用IdghtyServer3.AccessTokenVal环流的2.6.0版本或更早的版本。一旦我更新到2.7.0或更高版本,它就会停止工作。我没有时间去找出这两个版本之间到底发生了什么变化,因此是什么导致了这个问题。但我可以将其分离到it 3.AccessTokenVal环流

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40462794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档