-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathDownloadExceptionTest.cs
More file actions
64 lines (53 loc) · 2.5 KB
/
DownloadExceptionTest.cs
File metadata and controls
64 lines (53 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.AI.Foundry.Local.Tests;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AI.Foundry.Local;
using RichardSzalay.MockHttp;
using Xunit;
public class DownloadExceptionTest
{
[Fact]
public async Task DownloadModelAsync_RethrowsException_AndPrintsUrl()
{
// Arrange
using var mockHttp = new MockHttpMessageHandler();
var client = mockHttp.ToHttpClient();
client.BaseAddress = new Uri("http://localhost:5272");
using var manager = new FoundryLocalManager();
// Inject the mock client
typeof(FoundryLocalManager)
.GetField("_serviceUri", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(manager, client.BaseAddress);
typeof(FoundryLocalManager)
.GetField("_serviceClient", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(manager, client);
// Mock catalog to return a model
var modelInfo = new ModelInfo
{
ModelId = "test-model:1",
Uri = "https://example.com/model.onnx",
Alias = "test-model",
Runtime = new Runtime { DeviceType = DeviceType.CPU }
};
var catalog = new List<ModelInfo> { modelInfo };
var catalogJson = JsonSerializer.Serialize(catalog, ModelGenerationContext.Default.ListModelInfo);
mockHttp.When(HttpMethod.Get, "/foundry/list").Respond("application/json", catalogJson);
mockHttp.When(HttpMethod.Get, "/openai/models").Respond("application/json", "[]");
// Mock download to throw an exception
mockHttp.When(HttpMethod.Post, "/openai/download").Throw(new HttpRequestException("SSL connection failed"));
// Act & Assert
var ex = await Assert.ThrowsAsync<HttpRequestException>(() => manager.DownloadModelAsync("test-model"));
Assert.Equal("SSL connection failed", ex.Message);
// Note: Verifying Console.WriteLine is difficult in this setup without redirecting Console.Out,
// but we verified the exception is rethrown.
}
}