mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-26 10:49:19 +02:00
using var httpClient = new HttpClient();
This commit is contained in:
@@ -1,51 +1,47 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using NFluent;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
public class WireMockServerProxy2Tests
|
||||
{
|
||||
public class WireMockServerProxy2Tests
|
||||
[Fact]
|
||||
public async Task WireMockServer_ProxyAndRecordSettings_ShouldProxy()
|
||||
{
|
||||
[Fact]
|
||||
public async Task WireMockServer_ProxyAndRecordSettings_ShouldProxy()
|
||||
// Assign
|
||||
var serverAsProxy = WireMockServer.Start();
|
||||
serverAsProxy.Given(Request.Create().UsingPost())
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 42 }).WithHeader("Content-Type", "application/json"));
|
||||
|
||||
// Act
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(Request.Create().UsingPost().WithHeader("prx", "1"))
|
||||
.RespondWith(Response.Create().WithProxy(serverAsProxy.Urls[0]));
|
||||
|
||||
var request = new HttpRequestMessage
|
||||
{
|
||||
// Assign
|
||||
var serverAsProxy = WireMockServer.Start();
|
||||
serverAsProxy.Given(Request.Create().UsingPost())
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 42 }).WithHeader("Content-Type", "application/json"));
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri($"{server.Urls[0]}/TST"),
|
||||
Content = new StringContent("test")
|
||||
};
|
||||
request.Headers.Add("prx", "1");
|
||||
|
||||
// Act
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(Request.Create().UsingPost().WithHeader("prx", "1"))
|
||||
.RespondWith(Response.Create().WithProxy(serverAsProxy.Urls[0]));
|
||||
// Assert
|
||||
using var httpClient = new HttpClient();
|
||||
var response = await httpClient.SendAsync(request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
var request = new HttpRequestMessage
|
||||
{
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri($"{server.Urls[0]}/TST"),
|
||||
Content = new StringContent("test")
|
||||
};
|
||||
request.Headers.Add("prx", "1");
|
||||
Check.That(content).IsEqualTo("{\"p\":42}");
|
||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.Created);
|
||||
Check.That(response.Content.Headers.GetValues("Content-Type").First()).IsEqualTo("application/json");
|
||||
|
||||
// Assert
|
||||
var response = await new HttpClient().SendAsync(request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Check.That(content).IsEqualTo("{\"p\":42}");
|
||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.Created);
|
||||
Check.That(response.Content.Headers.GetValues("Content-Type").First()).IsEqualTo("application/json");
|
||||
|
||||
server.Dispose();
|
||||
serverAsProxy.Dispose();
|
||||
}
|
||||
server.Dispose();
|
||||
serverAsProxy.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user