2016-11-07 71 views
0

我想为我的WebApi和IdentityServer编写验收测试。为了让事情尽可能简单,我复制了here的整个示例项目,但又添加了另一个项目,它基本上和控制台客户端一样,但是作为验收测试。IdentityServer3 xBehave测试

我现在唯一的情况是这样的:

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”。当我通过控制台客户端调用这两个服务器时,它按预期工作。非常令人沮丧。

更新 我取代了“呼叫服务时”的步骤与下面的行只是为了增强简洁:

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

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

+0

此问题是后续的:http://stackoverflow.com/questions/40425587/identityserver3-principals-always-null – Domenic

+0

它并不容易,因为令牌是在“获取令牌”下方的场景中没有设置appClient? cp-paste将令牌提取到每个场景中,并验证它不是那个 –

+0

没有用。我已更新问题 – Domenic

回答

0

在此期间,我发现为什么它不工作的原因:

原来我不得不使用IdentityServer3.AccessTokenValidation的2.6.0或更早版本上的WebAPI。只要我更新到2.7.0或更高版本,它就停止工作。我没有时间来了解两个版本之间究竟发生了什么变化,从而导致问题的原因。但我可以将它隔离到IdentityServer3.AccessTokenValidation

+0

您在项目中安装了什么版本的IdentityModel? –

+0

我已经安装了IdentityModel的版本2.0.0 – Domenic

+0

似乎有一些版本的IdentityModel在最近的IdentityServer.AccessTokenValidatoin中没有工作一段时间。我看到多米尼克昨天推出了一个新版本修复: https://www.nuget.org/packages/IdentityServer3.AccessTokenValidation/2.12.0 也许2.12的作品?可能需要降级IdentityModel。 –