Update C# mapping code generator for WithStatusCode (#930)

This commit is contained in:
Stef Heyenrath
2023-05-06 10:24:53 +02:00
committed by GitHub
parent 1214ba5108
commit ccd8026884
3 changed files with 38 additions and 8 deletions

View File

@@ -1,9 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Net;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using Newtonsoft.Json;
using Stef.Validation; using Stef.Validation;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Constants; using WireMock.Constants;
@@ -125,9 +126,13 @@ internal class MappingConverter
// Response // Response
sb.AppendLine(" .RespondWith(Response.Create()"); sb.AppendLine(" .RespondWith(Response.Create()");
if (response.ResponseMessage.StatusCode is { } statusCode) if (response.ResponseMessage.StatusCode is int or string)
{ {
sb.AppendLine($" .WithStatusCode({statusCode})"); sb.AppendLine($" .WithStatusCode({JsonConvert.SerializeObject(response.ResponseMessage.StatusCode)})");
}
else if (response.ResponseMessage.StatusCode is HttpStatusCode httpStatusCode)
{
sb.AppendLine($" .WithStatusCode({(int)httpStatusCode})");
} }
if (response.ResponseMessage.Headers is { }) if (response.ResponseMessage.Headers is { })

View File

@@ -13,14 +13,24 @@ server
server server
.Given(Request.Create() .Given(Request.Create()
.UsingMethod("GET") .UsingMethod("POST")
.WithPath("/foo2") .WithPath("/foo2")
.WithParam("p2", "abc") .WithParam("p2", "abc")
) )
.WithGuid("1b731398-4a5b-457f-a6e3-d65e541c428f") .WithGuid("1b731398-4a5b-457f-a6e3-d65e541c428f")
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(201) .WithStatusCode("201")
.WithHeader("hk", "hv") .WithHeader("hk", "hv")
.WithBody("2") .WithBody("2")
); );
server
.Given(Request.Create()
.UsingMethod("DELETE")
.WithUrl("https://localhost/test")
)
.WithGuid("f74fd144-df53-404f-8e35-da22a640bd5f")
.RespondWith(Response.Create()
.WithStatusCode(208)
);

View File

@@ -2,6 +2,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
@@ -717,6 +718,7 @@ public class WireMockAdminApiTests
// Arrange // Arrange
var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"); var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
var guid2 = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f"); var guid2 = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
var guid3 = Guid.Parse("f74fd144-df53-404f-8e35-da22a640bd5f");
var server = WireMockServer.StartWithAdminInterface(); var server = WireMockServer.StartWithAdminInterface();
server server
@@ -738,21 +740,34 @@ public class WireMockAdminApiTests
Request.Create() Request.Create()
.WithPath("/foo2") .WithPath("/foo2")
.WithParam("p2", "abc") .WithParam("p2", "abc")
.UsingGet() .UsingPost()
) )
.WithGuid(guid2) .WithGuid(guid2)
.RespondWith( .RespondWith(
Response.Create() Response.Create()
.WithStatusCode(201) .WithStatusCode("201")
.WithHeader("hk", "hv") .WithHeader("hk", "hv")
.WithBody("2") .WithBody("2")
); );
server
.Given(
Request.Create()
.WithUrl("https://localhost/test")
.UsingDelete()
)
.WithGuid(guid3)
.RespondWith(
Response.Create()
.WithStatusCode(HttpStatusCode.AlreadyReported)
.WithBodyAsJson(new { x = 1 })
);
// Act // Act
var api = RestClient.For<IWireMockAdminApi>(server.Url); var api = RestClient.For<IWireMockAdminApi>(server.Url);
var mappings = await api.GetMappingsAsync().ConfigureAwait(false); var mappings = await api.GetMappingsAsync().ConfigureAwait(false);
mappings.Should().HaveCount(2); mappings.Should().HaveCount(3);
var code = await api.GetMappingsCodeAsync().ConfigureAwait(false); var code = await api.GetMappingsCodeAsync().ConfigureAwait(false);