IdentityServer3 xBehave 测试
IdentityServer3 xBehave test
我想为我的 WebApi 和 IdentityServer 编写验收测试。为了让事情尽可能简单,我从 here 复制了整个示例项目,但添加了另一个项目,它基本上与控制台客户端相同,但作为验收测试。
我现在得到的唯一情况是:
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));
}
}
}
我现在总是得到状态码 'unauthorized' 而不是 'ok'。当我通过控制台客户端调用这两个服务器时,它按预期工作。很郁闷。
更新
我用以下几行替换了 "when calling a service" 步骤只是为了简化:
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
我想为我的 WebApi 和 IdentityServer 编写验收测试。为了让事情尽可能简单,我从 here 复制了整个示例项目,但添加了另一个项目,它基本上与控制台客户端相同,但作为验收测试。
我现在得到的唯一情况是:
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));
}
}
}
我现在总是得到状态码 'unauthorized' 而不是 'ok'。当我通过控制台客户端调用这两个服务器时,它按预期工作。很郁闷。
更新 我用以下几行替换了 "when calling a service" 步骤只是为了简化:
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