嗨,我正在尝试编写我的提供商测试。我正在设置提供程序状态中间件并使用 Startup.cs 文件,如示例代码中所示。不过,当我运行提供商测试时,我收到此错误。有什么建议吗? :
验证 API Consumer 和 My API 之间的协议
检索天气的 GET 请求 给定数据 请求失败 - 发送 url 请求时出错 (http://localhost:7058/weather):操作超时
失败:
以下是日志:
Verifier Logs
-------------
2023-07-31T10:46:02.441273Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier: Executing provider states
2023-07-31T10:46:02.441305Z INFO ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier: Running setup provider state change handler 'There is data' for 'A GET request to retrieve the weather'
2023-07-31T10:46:02.445733Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather forecasts"}: pact_verifier::provider_client: Sending HTTP Request ( method: POST, path: /, query: None, headers: Some({"Content-Type": ["application/json"]}), body: Present(54 bytes, application/json) ) to state change handler
2023-07-31T10:46:02.446003Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather forecasts"}: reqwest::connect: starting new connection: http://localhost:7058/
2023-07-31T10:46:02.454204Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: hyper::client::connect::http: connecting to [::1]:7058
2023-07-31T10:46:02.454764Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: hyper::client::connect::http: connected to [::1]:7058
2023-07-31T10:46:02.618901Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: hyper::client::pool: pooling idle connection for ("http", localhost:7058)
2023-07-31T10:46:02.618935Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier::provider_client: State change request: Response { url: Url { scheme: "http", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("localhost")), port: Some(7058), path: "/provider-states", query: None, fragment: None }, status: 200, headers: {"content-length": "0", "date": "Mon, 31 Jul 2023 10:46:02 GMT", "server": "Kestrel"} }
2023-07-31T10:46:02.619044Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier: State Change: "ProviderState { name: "There is data", params: {} }" -> Ok({})
2023-07-31T10:46:02.619059Z INFO ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier: Running provider verification for 'A GET request to retrieve the weather'
2023-07-31T10:46:02.619118Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier: Verifying a HTTP interaction
2023-07-31T10:46:02.619144Z INFO ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier::provider_client: Sending request to provider at http://localhost:7058/
2023-07-31T10:46:02.619146Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather forecasts"}: pact_verifier::provider_client: Provider details = ProviderInfo { name: "Weather API", protocol: "http", host: "localhost", port: Some(7058), path: "/", transports: [ProviderTransport { transport: "http", port: Some(7058), path: Some("/"), scheme: None }] }
2023-07-31T10:46:02.619156Z INFO ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier::provider_client: Sending request HTTP Request ( method: GET, path: /weather, query: None, headers: None, body: Missing )
2023-07-31T10:46:02.619158Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: pact_verifier::provider_client: body:
2023-07-31T10:46:02.619179Z DEBUG ThreadId(01) verify_interaction{interaction="A GET request to retrieve the weather"}: hyper::client::pool: reuse idle connection for ("http", localhost:7058)
这是我从示例代码中得到的测试:
public class ProviderTests : IDisposable
{
private readonly WebApplication server;
public Uri ServerUri { get; }
private readonly PactVerifier verifier;
public ProviderTests(ITestOutputHelper output)
{
ServerUri = new Uri("http://localhost:7058");
var builder = WebApplication.CreateBuilder(Environment.GetCommandLineArgs());
builder.WebHost.UseUrls(ServerUri.ToString());
builder.AddBuilderServices(); //my own method which I use to normally add services to my app, // before it starts
var startup = new TestStartup(builder.Configuration);
startup.ConfigureServices(builder.Services);
server = builder.Build();
startup.Configure(server, builder.Environment);
server.Start();
this.verifier = new PactVerifier(new PactVerifierConfig
{
LogLevel = PactLogLevel.Debug,
Outputters = new List<IOutput>
{
new XunitOutput(output)
}
});
}
public void Dispose()
{
server.DisposeAsync();
verifier.Dispose();
}
[Fact]
public void EnsureSomethingApiHonoursPactWithConsumer()
{
string pactPath = my path
try
{
this.verifier
.ServiceProvider("My API", ServerUri)
.WithFileSource(new FileInfo(pactPath))
.WithProviderStateUrl(new Uri(ServerUri, "/provider-states"))
.Verify();
} catch (Exception e)
{
throw;
}
}
}