首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当Mountebank开始使用dotnet时无法访问mountebank的冒充端口-测试容器

当Mountebank开始使用dotnet时无法访问mountebank的冒充端口-测试容器
EN

Stack Overflow用户
提问于 2021-11-30 07:11:06
回答 1查看 369关注 0票数 0

在我的dotnet-testcontainers测试中,我使用dotnet-testcontainers https://github.com/HofmeisterAn/dotnet-testcontainers来旋转一个带有xUnit的容器。我可以成功地创建一个挂载银行客户端,并成功地创建一个冒名顶替者。问题是,当测试运行时,应用程序试图调用http://localhost:3000上的冒名顶替者,却拒绝了连接。

我可以成功地打开http://localhost:2525,并可以看到Mountebank默认页面。所以坐骑银行运转良好。我还确认,通过查看码头容器日志,在端口3000上成功地创建了这个冒充者。我还试着用邮递员给我在http:localhost:3000的冒名顶替者打电话,结果被拒绝了。

有什么问题吗?这是否是码头容器中的端口3000没有公开之类的问题?下面是我的代码:

代码语言:javascript
运行
复制
MountebankClient mbClient = new MountebankClient();

var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
                .WithImage("bbyars/mountebank")
                .WithName("mountebank")
                .WithPortBinding(2525, false)
                .WithHostname("localhost");

var testContainers = testcontainersBuilder.Build();
await testContainers.StartAsync();

var testImposter = mbClient.CreateHttpImposter(3000);
testImposter.AddStub().ReturnsBody(HttpStatusCode.OK, File.ReadAllText(@".\Stubs\testImposter.json"));
mbClient.Submit(testImposter);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-02 05:38:11

可能对其他会面临这个问题的人有用。搞清楚问题出在哪里了。我没有映射容器中的主机端口和冒牌货端口。了解已发布的端口https://docs.docker.com/config/containers/container-networking/和使用WithExposedPort(3000),然后使用WithPortBinding(3000,3000)对该端口进行端口绑定,其中,该方法中的first端口是主机端口,second端口是容器端口。

代码语言:javascript
运行
复制
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
        .WithImage("bbyars/mountebank")
        .WithName("mountebank")
        .WithPortBinding(2525, 2525)
        .WithExposedPort(3000)
        .WithExposedPort(3100)
        .WithPortBinding(3000, 3000)
        .WithPortBinding(3100, 3100)                 
        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(2525))
        .WithHostname("localhost");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70165557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档