Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions Tests/MCPify.Tests/ClientCredentialsAuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,22 @@ public TestClientCredentialsServer()
{
var port = GetRandomUnusedPort();
BaseUrl = $"http://localhost:{port}";
_host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder =>

var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls(BaseUrl);

var app = builder.Build();
app.MapPost("/token", async context =>
{
await context.Response.WriteAsJsonAsync(new
{
builder.UseUrls(BaseUrl);
builder.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapPost("/token", async context =>
{
await context.Response.WriteAsJsonAsync(new
{
access_token = "cc_token",
token_type = "Bearer",
expires_in = 3600
});
});
});
});
})
.Build();
access_token = "cc_token",
token_type = "Bearer",
expires_in = 3600
});
});

_host = app;
}

public async Task StartAsync() => await _host.StartAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,7 @@ public class OAuthChallengeTokenValidationTests
[Fact]
public async Task PostWithoutSession_ReturnsUnauthorizedChallenge_WhenTokenValidationEnabled()
{
using var host = await new HostBuilder()
.ConfigureWebHost(webBuilder =>
{
webBuilder
.UseTestServer()
.ConfigureServices(services =>
{
services.AddLogging();
services.AddRouting();
services.AddMcpify(options =>
{
options.Transport = McpTransportType.Http;
options.TokenValidation = new TokenValidationOptions
{
EnableJwtValidation = true,
ValidateAudience = true
};
});
})
.Configure(app =>
{
app.UseRouting();
app.UseMcpifyContext();
app.UseMcpifyOAuth();
app.UseEndpoints(endpoints =>
{
endpoints.MapMcpifyEndpoint();
});
});
})
.StartAsync();
await using var host = await CreateHostAsync();

var options = host.Services.GetRequiredService<McpifyOptions>();
Assert.True(options.TokenValidation?.EnableJwtValidation, "Token validation should be enabled");
Expand All @@ -73,4 +43,29 @@ public async Task PostWithoutSession_ReturnsUnauthorizedChallenge_WhenTokenValid
Assert.Contains(response.Headers.WwwAuthenticate, header =>
string.Equals(header.Scheme, "Bearer", StringComparison.OrdinalIgnoreCase));
}

private static async Task<WebApplication> CreateHostAsync()
{
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseTestServer();

builder.Services.AddLogging();
builder.Services.AddMcpify(options =>
{
options.Transport = McpTransportType.Http;
options.TokenValidation = new TokenValidationOptions
{
EnableJwtValidation = true,
ValidateAudience = true
};
});

var app = builder.Build();
app.UseMcpifyContext();
app.UseMcpifyOAuth();
app.MapMcpifyEndpoint();

await app.StartAsync();
return app;
}
}
43 changes: 17 additions & 26 deletions Tests/MCPify.Tests/Integration/OAuthMetadataEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,33 +113,24 @@ public async Task GetMetadata_FallsBackToAuthorizationUrlAuthority_WhenAuthoriza
Assert.Contains("https://auth.example.com", metadata!.AuthorizationServers);
}

private async Task<IHost> CreateHostAsync(Action<IServiceProvider>? configure = null, Action<McpifyOptions>? configureOptions = null)
private static async Task<WebApplication> CreateHostAsync(Action<IServiceProvider>? configure = null, Action<McpifyOptions>? configureOptions = null)
{
return await new HostBuilder()
.ConfigureWebHost(webBuilder =>
{
webBuilder
.UseTestServer()
.ConfigureServices(services =>
{
services.AddMcpify(options =>
{
configureOptions?.Invoke(options);
});
services.AddLogging();
services.AddRouting();
})
.Configure(app =>
{
configure?.Invoke(app.ApplicationServices);
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapMcpifyEndpoint();
});
});
})
.StartAsync();
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseTestServer();

builder.Services.AddMcpify(options =>
{
configureOptions?.Invoke(options);
});
builder.Services.AddLogging();

var app = builder.Build();

configure?.Invoke(app.Services);
app.MapMcpifyEndpoint();

await app.StartAsync();
return app;
}

private class ProtectedResourceMetadata
Expand Down
53 changes: 24 additions & 29 deletions Tests/MCPify.Tests/Integration/TestApiServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,42 @@ public TestApiServer()
var port = GetRandomUnusedPort();
BaseUrl = $"http://localhost:{port}";

_host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder =>
{
builder.UseUrls(BaseUrl);
builder.Configure(ConfigureApp);
})
.Build();
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls(BaseUrl);

var app = builder.Build();
ConfigureApp(app);
_host = app;
}

public async Task StartAsync() => await _host.StartAsync();

public HttpClient CreateClient() => new() { BaseAddress = new Uri(BaseUrl) };

private void ConfigureApp(IApplicationBuilder app)
private void ConfigureApp(WebApplication app)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
app.MapGet("/users/{id:int}", async context =>
{
endpoints.MapGet("/users/{id:int}", async context =>
var id = int.Parse(context.Request.RouteValues["id"]?.ToString() ?? "0");
await context.Response.WriteAsJsonAsync(new
{
var id = int.Parse(context.Request.RouteValues["id"]?.ToString() ?? "0");
await context.Response.WriteAsJsonAsync(new
{
id,
path = context.Request.Path.Value,
query = context.Request.QueryString.Value
});
id,
path = context.Request.Path.Value,
query = context.Request.QueryString.Value
});
});

endpoints.MapPost("/echo", async context =>
{
using var reader = new StreamReader(context.Request.Body);
var body = await reader.ReadToEndAsync();
await context.Response.WriteAsync(body);
});
app.MapPost("/echo", async context =>
{
using var reader = new StreamReader(context.Request.Body);
var body = await reader.ReadToEndAsync();
await context.Response.WriteAsync(body);
});

endpoints.MapGet("/auth-check", async context =>
{
var auth = context.Request.Headers.Authorization.ToString();
await context.Response.WriteAsJsonAsync(new { authorization = auth });
});
app.MapGet("/auth-check", async context =>
{
var auth = context.Request.Headers.Authorization.ToString();
await context.Response.WriteAsJsonAsync(new { authorization = auth });
});
}

Expand Down
Loading