diff --git a/Directory.Build.props b/Directory.Build.props index 26a67e70b..60154296c 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -5,7 +5,6 @@ 1.4.41 - See CHANGELOG.md WireMock.Net-Logo.png https://github.com/WireMock-Net/WireMock.Net Apache-2.0 diff --git a/WireMock.Net Solution.sln.DotSettings b/WireMock.Net Solution.sln.DotSettings index dadfb7f50..68011e7b1 100644 --- a/WireMock.Net Solution.sln.DotSettings +++ b/WireMock.Net Solution.sln.DotSettings @@ -24,4 +24,5 @@ True True True + True \ No newline at end of file diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/DynamicDataGeneration.cs b/examples/WireMock.Net.OpenApiParser.ConsoleApp/DynamicDataGeneration.cs index e2fe4afd5..7d51dc739 100644 --- a/examples/WireMock.Net.OpenApiParser.ConsoleApp/DynamicDataGeneration.cs +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/DynamicDataGeneration.cs @@ -1,26 +1,23 @@ -using System; -using Microsoft.OpenApi.Models; using RandomDataGenerator.FieldOptions; using RandomDataGenerator.Randomizers; using WireMock.Net.OpenApiParser.Settings; -namespace WireMock.Net.OpenApiParser.ConsoleApp +namespace WireMock.Net.OpenApiParser.ConsoleApp; + +public class DynamicDataGeneration : WireMockOpenApiParserDynamicExampleValues { - public class DynamicDataGeneration : WireMockOpenApiParserDynamicExampleValues + public override string String { - public override string String + get { - get - { - //Since you have your Schema, you can get if max-lenght is set. You can generate accurate examples with this settings - var maxLength = this.Schema.MaxLength ?? 9; + // Since you have your Schema, you can get if max-length is set. You can generate accurate examples with this settings + var maxLength = Schema.MaxLength ?? 9; - return RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex - { - Pattern = $"[0-9A-Z]{{{maxLength}}}" - }).Generate() ?? "example-string"; - } - set { } + return RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex + { + Pattern = $"[0-9A-Z]{{{maxLength}}}" + }).Generate() ?? "example-string"; } + set { } } -} +} \ No newline at end of file diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/file_error.yaml b/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/file_error.yaml new file mode 100644 index 000000000..5301c43ef --- /dev/null +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/file_error.yaml @@ -0,0 +1,130 @@ +openapi: 3.0.1 +info: + title: Basic-String-Test + description: Basic string test + version: "4.5.2" +servers: + - url: https://localhost/examples +paths: + /string/basic: + get: + tags: + - basic-string + description: Basic string test + operationId: getBasicString1 + responses: + 200: + description: successful operation + content: + application/json: + schema: + type: string + /string/maxlenght/minlenght: + get: + tags: + - basic-string + description: Basic string test with maxlength and minlength properties + operationId: getBasicString2 + responses: + 200: + description: successful operation + content: + application/json: + schema: + type: string + maxLength: 8 + minLength: 8 + /string/maxlenght: + get: + tags: + - basic-string + description: Basic string test with maxlength property + operationId: getBasicString3 + + responses: + 200: + description: successful operation + content: + application/json: + schema: + type: string + maxLength: 5 + /string/minlenght: + get: + tags: + - basic-string + description: Basic string test with minlength property + operationId: getBasicString + responses: + 200: + description: successful operation + content: + application/json: + schema: + type: string + minLength: 10 + /string/enum: + get: + tags: + - basic-string + description: Basic string test with enum property + operationId: getBasicString4 + responses: + 200: + description: successful operation + content: + application/json: + schema: + type: string + enum: + - response1 + - response2 + /string/pattern/uri: + get: + tags: + - basic-string + description: Basic string test with uri pattern property + operationId: getBasicString5 + responses: + 200: + description: successful operation + content: + application/json: + schema: + type: string + pattern: '^(http|https|ftp|sftp)://((([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]))\.){3}([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]))|((www\.|())[a-z0-9]{2,5}\.([a-z]{2,3}((\.[a-z]{2})|()))))(()|(:((102[5-9])|(1[0-9][3-9][0-9])|(1[1-9][0-9]{2})|([2-9][0-9]{3})|([2-5][0-9]{4})|(1[0-9]{4})|(60000))))$' + /string/pattern/ipv4: + get: + tags: + - basic-string + description: Basic string test with ipv4 pattern property + operationId: getBasicString6 + responses: + 200: + description: successful operation + content: + application/json: + schema: + type: string + pattern: '^(([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]))\.){3}([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]))$' + /string/header/ipv4: + get: + tags: + - basic-string + description: Basic string test with ipv4 pattern property + operationId: getBasicString7 + parameters: + - name: Header-Sample + in: header + required: true + schema: + type: string + pattern: "ipv4 pattern" + responses: + 200: + description: successful operation + content: + application/json: + schema: + type: string + pattern: '^(([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]))\.){3}([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]))$' \ No newline at end of file diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/pet.json b/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/pet.json new file mode 100644 index 000000000..af4555485 --- /dev/null +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/pet.json @@ -0,0 +1,157 @@ +{ + "openapi": "3.0.2", + "info": { + "title": "Swagger Petstore - OpenAPI 3.0", + "description": "This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about\nSwagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!\nYou can now help us improve the API whether it's by making changes to the definition itself or to the code.\nThat way, with time, we can improve the API in general, and expose some of the new features in OAS3.\n\nSome useful links:\n- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)\n- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)", + "termsOfService": "http://swagger.io/terms/", + "contact": { "email": "apiteam@swagger.io" }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "1.0.11" + }, + "externalDocs": { + "description": "Find out more about Swagger", + "url": "http://swagger.io" + }, + "servers": [ { "url": "/api/v3" } ], + "tags": [ + { + "name": "pet", + "description": "Everything about your Pets", + "externalDocs": { + "description": "Find out more", + "url": "http://swagger.io" + } + }, + { + "name": "store", + "description": "Access to Petstore orders", + "externalDocs": { + "description": "Find out more about our store", + "url": "http://swagger.io" + } + }, + { + "name": "user", + "description": "Operations about user" + } + ], + "paths": { + "/pet": { + "put": { + "tags": [ "pet" ], + "summary": "Update an existing pet", + "description": "Update an existing pet by Id", + "operationId": "updatePet", + "parameters": [ + { + "name": "api_key", + "in": "header", + "description": "", + "required": true, + "schema": { "type": "string" } + } + ], + "requestBody": { + "description": "Update an existent pet in the store", + "content": { + "application/json": { + "schema": { "$ref": "#/components/schemas/Pet" } + }, + "application/xml": { + "schema": { "$ref": "#/components/schemas/Pet" } + }, + "application/x-www-form-urlencoded": { + "schema": { "$ref": "#/components/schemas/Pet" } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful operation", + "content": { + "application/xml": { + "schema": { "$ref": "#/components/schemas/Pet" } + }, + "application/json": { + "schema": { "$ref": "#/components/schemas/Pet" } + } + } + }, + "400": { "description": "Invalid ID supplied" }, + "404": { "description": "Pet not found" }, + "405": { "description": "Validation exception" } + }, + "security": [ { "petstore_auth": [ "write:pets", "read:pets" ] } ] + } + } + }, + "components": { + "schemas": { + "Category": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64", + "example": 1 + }, + "name": { + "type": "string", + "example": "Dogs" + } + }, + "xml": { "name": "category" } + }, + "Tag": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { "type": "string" } + }, + "xml": { "name": "tag" } + }, + "Pet": { + "required": [ "name", "photoUrls" ], + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64", + "example": 10 + }, + "name": { + "type": "string", + "example": "doggie" + }, + "category": { "$ref": "#/components/schemas/Category" }, + "photoUrls": { + "type": "array", + "xml": { "wrapped": true }, + "items": { + "type": "string", + "xml": { "name": "photoUrl" } + } + }, + "tags": { + "type": "array", + "xml": { "wrapped": true }, + "items": { "$ref": "#/components/schemas/Tag" } + }, + "status": { + "type": "string", + "description": "pet status in the store", + "enum": [ "available", "pending", "sold" ] + } + }, + "xml": { "name": "pet" } + } + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/refs.yaml b/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/refs.yaml new file mode 100644 index 000000000..2da199d62 --- /dev/null +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/refs.yaml @@ -0,0 +1,178 @@ +swagger: "2.0" +info: + description: "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters." + version: "1.0.0" + title: "Swagger Petstore" + termsOfService: "http://swagger.io/terms/" + contact: + email: "apiteam@swagger.io" + license: + name: "Apache 2.0" + url: "http://www.apache.org/licenses/LICENSE-2.0.html" +host: "petstore.swagger.io" +basePath: "/v2" +tags: +- name: "pet" + description: "Everything about your Pets" + externalDocs: + description: "Find out more" + url: "http://swagger.io" +- name: "store" + description: "Access to Petstore orders" +- name: "user" + description: "Operations about user" + externalDocs: + description: "Find out more about our store" + url: "http://swagger.io" +schemes: +- "https" +- "http" +paths: + /user/createWithList: + post: + tags: + - "user" + summary: "Creates list of users with given input array" + description: "" + operationId: "createUsersWithListInput" + produces: + - "application/xml" + - "application/json" + parameters: + - in: "body" + name: "body" + description: "List of user object" + required: true + schema: + type: "array" + items: + $ref: "#/definitions/User" + responses: + "200": + description: "successful operation" + schema: + $ref: "#/definitions/Order" + default: + description: "successful operation" +definitions: + Order: + type: "object" + properties: + id: + type: "integer" + format: "int64" + petId: + type: "integer" + format: "int64" + quantity: + type: "integer" + format: "int32" + shipDate: + type: "string" + format: "date-time" + status: + type: "string" + description: "Order Status" + enum: + - "placed" + - "approved" + - "delivered" + complete: + type: "boolean" + default: false + xml: + name: "Order" + Category: + type: "object" + properties: + id: + type: "integer" + format: "int64" + name: + type: "string" + xml: + name: "Category" + User: + type: "object" + properties: + id: + type: "integer" + format: "int64" + username: + type: "string" + firstName: + type: "string" + lastName: + type: "string" + email: + type: "string" + password: + type: "string" + phone: + type: "string" + userStatus: + type: "integer" + format: "int32" + description: "User Status" + xml: + name: "User" + Tag: + type: "object" + properties: + id: + type: "integer" + format: "int64" + name: + type: "string" + xml: + name: "Tag" + Pet: + type: "object" + required: + - "name" + - "photoUrls" + properties: + id: + type: "integer" + format: "int64" + category: + $ref: "#/definitions/Category" + name: + type: "string" + example: "doggie" + photoUrls: + type: "array" + xml: + name: "photoUrl" + wrapped: true + items: + type: "string" + tags: + type: "array" + xml: + name: "tag" + wrapped: true + items: + $ref: "#/definitions/Tag" + status: + type: "string" + description: "pet status in the store" + enum: + - "available" + - "pending" + - "sold" + xml: + name: "Pet" + ApiResponse: + type: "object" + properties: + code: + type: "integer" + format: "int32" + type: + type: "string" + message: + type: "string" +externalDocs: + description: "Find out more about Swagger" + url: "http://swagger.io" \ No newline at end of file diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/testopenapifile.json b/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/testopenapifile.json new file mode 100644 index 000000000..b3dd15bad --- /dev/null +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/OpenApiFiles/testopenapifile.json @@ -0,0 +1,150 @@ +{ + "openapi": "3.0.2", + "info": { + "title": "Swagger Petstore - OpenAPI 3.0", + "description": "This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about\nSwagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!\nYou can now help us improve the API whether it's by making changes to the definition itself or to the code.\nThat way, with time, we can improve the API in general, and expose some of the new features in OAS3.\n\nSome useful links:\n- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)\n- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml) ", + "termsOfService": "http://swagger.io/terms/", + "contact": { "email": "apiteam@swagger.io" }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "1.0.4" + }, + "externalDocs": { + "description": "Find out more about Swagger", + "url": "http://swagger.io" + }, + "servers": [ { "url": "/api/v3" } ], + "tags": [ + { + "name": "pet", + "description": "Everything about your Pets", + "externalDocs": { + "description": "Find out more", + "url": "http://swagger.io" + } + }, + { + "name": "store", + "description": "Operations about user" + }, + { + "name": "user", + "description": "Access to Petstore orders", + "externalDocs": { + "description": "Find out more about our store", + "url": "http://swagger.io" + } + } + ], + "paths": { + "/pet": { + "put": { + "tags": [ "pet" ], + "summary": "Update an existing pet", + "description": "Update an existing pet by Id", + "operationId": "updatePet", + "requestBody": { + "description": "Update an existent pet in the store", + "content": { + "application/json": { "schema": { "$ref": "#/components/schemas/Pet" } }, + "application/xml": { "schema": { "$ref": "#/components/schemas/Pet" } }, + "application/x-www-form-urlencoded": { "schema": { "$ref": "#/components/schemas/Pet" } } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful operation", + "content": { + "application/xml": { "schema": { "$ref": "#/components/schemas/Pet" } }, + "application/json": { "schema": { "$ref": "#/components/schemas/Pet" } } + } + }, + "400": { "description": "Invalid ID supplied" }, + "404": { "description": "Pet not found" }, + "405": { "description": "Validation exception" } + }, + "security": [ { "petstore_auth": [ "write:pets", "read:pets" ] } ] + } + } + }, + "components": { + "schemas": { + "Category": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64", + "example": 1 + }, + "name": { + "type": "string", + "example": "Dogs" + } + }, + "xml": { "name": "category" } + }, + "Tag": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { "type": "string" } + }, + "xml": { "name": "tag" } + }, + "Pet": { + "required": [ "name", "photoUrls" ], + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64", + "example": 10 + }, + "name": { + "type": "string", + "example": "doggie" + }, + "category": { "$ref": "#/components/schemas/Category" }, + "photoUrls": { + "type": "array", + "xml": { "wrapped": true }, + "items": { + "type": "string", + "xml": { "name": "photoUrl" } + } + }, + "tags": { + "type": "array", + "xml": { "wrapped": true }, + "items": { "$ref": "#/components/schemas/Tag" } + }, + "status": { + "type": "string", + "description": "pet status in the store", + "enum": [ "available", "pending", "sold" ] + } + }, + "xml": { "name": "pet" } + }, + "ApiResponse": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "format": "int32" + }, + "type": { "type": "string" }, + "message": { "type": "string" } + }, + "xml": { "name": "##default" } + } + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/Program.cs b/examples/WireMock.Net.OpenApiParser.ConsoleApp/Program.cs index 37e183b4b..c959d548c 100644 --- a/examples/WireMock.Net.OpenApiParser.ConsoleApp/Program.cs +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/Program.cs @@ -1,59 +1,90 @@ -using System; -using System.IO; -using Microsoft.OpenApi.Readers; -using Newtonsoft.Json; - -namespace WireMock.Net.OpenApiParser.ConsoleApp -{ - class Program - { - private const string Folder = "OpenApiFiles"; - static void Main(string[] args) - { - //RunOthersOpenApiParserExample(); - - RunMockServerWithDynamicExampleGeneration(); - } - - private static void RunMockServerWithDynamicExampleGeneration() { - //Run your mocking framework specifieing youur Example Values generator class. - var serverCustomer_V2_json = Run.RunServer(Path.Combine(Folder, "Swagger_Customer_V2.0.json"), "http://localhost:8090/", true, new DynamicDataGeneration(), Types.ExampleValueType.Value, Types.ExampleValueType.Value); - Console.WriteLine("Press any key to stop the servers"); - - Console.ReadKey(); - serverCustomer_V2_json.Stop(); - } - - private static void RunOthersOpenApiParserExample() - { - var serverOpenAPIExamples = Run.RunServer(Path.Combine(Folder, "openAPIExamples.yaml"), "https://localhost:9091/"); - var serverPetstore_V2_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.json"), "https://localhost:9092/"); - var serverPetstore_V2_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.yaml"), "https://localhost:9093/"); - var serverPetstore_V300_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.0.yaml"), "https://localhost:9094/"); - var serverPetstore_V302_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.2.json"), "https://localhost:9095/"); - - Console.WriteLine("Press any key to stop the servers"); - Console.ReadKey(); - - serverOpenAPIExamples.Stop(); - serverPetstore_V2_json.Stop(); - serverPetstore_V2_yaml.Stop(); - serverPetstore_V300_yaml.Stop(); - serverPetstore_V302_json.Stop(); - - //IWireMockOpenApiParser parser = new WireMockOpenApiParser(); - - //var petStoreModels = parser.FromStream(File.OpenRead("petstore-openapi3.json"), out OpenApiDiagnostic diagnostic1); - //string petStoreJson = JsonConvert.SerializeObject(petStoreModels, Settings); - // File.WriteAllText("../../../wiremock-petstore-openapi3.json", petStoreJson); - - //Run.RunServer(petStoreModels); - - //var mappingModels2 = parser.FromStream(File.OpenRead("infura.yaml"), out OpenApiDiagnostic diagnostic2); - //Console.WriteLine(JsonConvert.SerializeObject(diagnostic2, Settings)); - - //string json2 = JsonConvert.SerializeObject(mappingModels2, Settings); - //Console.WriteLine(json2); - } - } +using System; +using System.IO; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; + +namespace WireMock.Net.OpenApiParser.ConsoleApp; + +class Program +{ + private const string Folder = "OpenApiFiles"; + static void Main(string[] args) + { + RunOthersOpenApiParserExample(); + + //RunMockServerWithDynamicExampleGeneration(); + } + + private static void RunMockServerWithDynamicExampleGeneration() + { + //Run your mocking framework specifieing youur Example Values generator class. + var serverCustomer_V2_json = Run.RunServer(Path.Combine(Folder, "Swagger_Customer_V2.0.json"), "http://localhost:8090/", true, new DynamicDataGeneration(), Types.ExampleValueType.Value, Types.ExampleValueType.Value); + Console.WriteLine("Press any key to stop the servers"); + + Console.ReadKey(); + serverCustomer_V2_json.Stop(); + } + + private static void RunOthersOpenApiParserExample() + { + var serverOpenAPIExamples = Run.RunServer(Path.Combine(Folder, "openAPIExamples.yaml"), "https://localhost:9091/"); + var serverPetstore_V2_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.json"), "https://localhost:9092/"); + var serverPetstore_V2_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.yaml"), "https://localhost:9093/"); + var serverPetstore_V300_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.0.yaml"), "https://localhost:9094/"); + var serverPetstore_V302_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.2.json"), "https://localhost:9095/"); + var testopenapifile_json = Run.RunServer(Path.Combine(Folder, "testopenapifile.json"), "https://localhost:9096/"); + var file_errorYaml = Run.RunServer(Path.Combine(Folder, "file_error.yaml"), "https://localhost:9097/"); + var file_petJson = Run.RunServer(Path.Combine(Folder, "pet.json"), "https://localhost:9098/"); + var refsYaml = Run.RunServer(Path.Combine(Folder, "refs.yaml"), "https://localhost:9099/"); + + testopenapifile_json + .Given(Request.Create().WithPath("/x").UsingGet()) + .WithTitle("t") + .WithDescription("d") + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new + { + result = "ok" + }) + ); + + testopenapifile_json + .Given(Request.Create().WithPath("/y").UsingGet()) + .WithTitle("t2") + .WithDescription("d2") + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson(new[] { "string-value"}) + ); + + Console.WriteLine("Press any key to stop the servers"); + Console.ReadKey(); + + serverOpenAPIExamples.Stop(); + serverPetstore_V2_json.Stop(); + serverPetstore_V2_yaml.Stop(); + serverPetstore_V300_yaml.Stop(); + serverPetstore_V302_json.Stop(); + testopenapifile_json.Stop(); + file_errorYaml.Stop(); + file_petJson.Stop(); + refsYaml.Stop(); + + //IWireMockOpenApiParser parser = new WireMockOpenApiParser(); + + //var petStoreModels = parser.FromStream(File.OpenRead("petstore-openapi3.json"), out OpenApiDiagnostic diagnostic1); + //string petStoreJson = JsonConvert.SerializeObject(petStoreModels, Settings); + // File.WriteAllText("../../../wiremock-petstore-openapi3.json", petStoreJson); + + //Run.RunServer(petStoreModels); + + //var mappingModels2 = parser.FromStream(File.OpenRead("infura.yaml"), out OpenApiDiagnostic diagnostic2); + //Console.WriteLine(JsonConvert.SerializeObject(diagnostic2, Settings)); + + //string json2 = JsonConvert.SerializeObject(mappingModels2, Settings); + //Console.WriteLine(json2); + } } \ No newline at end of file diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/WireMock.Net.OpenApiParser.ConsoleApp.csproj b/examples/WireMock.Net.OpenApiParser.ConsoleApp/WireMock.Net.OpenApiParser.ConsoleApp.csproj index a6ea51670..b63d33433 100644 --- a/examples/WireMock.Net.OpenApiParser.ConsoleApp/WireMock.Net.OpenApiParser.ConsoleApp.csproj +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/WireMock.Net.OpenApiParser.ConsoleApp.csproj @@ -1,44 +1,29 @@ - - Exe - net5.0 - + + Exe + net6.0 + - - - - - + + + + + - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Admin/Mappings/BodyModel.cs b/src/WireMock.Net.Abstractions/Admin/Mappings/BodyModel.cs index a3ebfcad5..52d2f7f00 100644 --- a/src/WireMock.Net.Abstractions/Admin/Mappings/BodyModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Mappings/BodyModel.cs @@ -1,4 +1,4 @@ -namespace WireMock.Admin.Mappings +namespace WireMock.Admin.Mappings { /// /// Body Model @@ -9,11 +9,11 @@ public class BodyModel /// /// Gets or sets the matcher. /// - public MatcherModel Matcher { get; set; } + public MatcherModel? Matcher { get; set; } /// /// Gets or sets the matchers. /// - public MatcherModel[] Matchers { get; set; } + public MatcherModel[]? Matchers { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Admin/Mappings/CookieModel.cs b/src/WireMock.Net.Abstractions/Admin/Mappings/CookieModel.cs index 420d95be9..5b88bb426 100644 --- a/src/WireMock.Net.Abstractions/Admin/Mappings/CookieModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Mappings/CookieModel.cs @@ -1,30 +1,31 @@ using System.Collections.Generic; -namespace WireMock.Admin.Mappings; - -/// -/// Cookie Model -/// -[FluentBuilder.AutoGenerateBuilder] -public class CookieModel +namespace WireMock.Admin.Mappings { /// - /// Gets or sets the name. + /// Cookie Model /// - public string Name { get; set; } = null!; + [FluentBuilder.AutoGenerateBuilder] + public class CookieModel + { + /// + /// Gets or sets the name. + /// + public string Name { get; set; } = null!; - /// - /// Gets or sets the matchers. - /// - public IList? Matchers { get; set; } + /// + /// Gets or sets the matchers. + /// + public IList? Matchers { get; set; } - /// - /// Gets or sets the ignore case. - /// - public bool? IgnoreCase { get; set; } + /// + /// Gets or sets the ignore case. + /// + public bool? IgnoreCase { get; set; } - /// - /// Reject on match. - /// - public bool? RejectOnMatch { get; set; } + /// + /// Reject on match. + /// + public bool? RejectOnMatch { get; set; } + } } \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Admin/Mappings/MatcherModel.cs b/src/WireMock.Net.Abstractions/Admin/Mappings/MatcherModel.cs index 38bf9948e..1c1cda319 100644 --- a/src/WireMock.Net.Abstractions/Admin/Mappings/MatcherModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Mappings/MatcherModel.cs @@ -14,17 +14,17 @@ public class MatcherModel /// /// Gets or sets the pattern. Can be a string (default) or an object. /// - public object Pattern { get; set; } + public object? Pattern { get; set; } /// /// Gets or sets the patterns. Can be array of strings (default) or an array of objects. /// - public object[] Patterns { get; set; } + public object[]? Patterns { get; set; } /// /// Gets or sets the pattern as a file. /// - public string PatternAsFile { get; set; } + public string? PatternAsFile { get; set; } /// /// Gets or sets the ignore case. @@ -36,4 +36,4 @@ public class MatcherModel /// public bool? RejectOnMatch { get; set; } } -} +} \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Admin/Mappings/PathModel.cs b/src/WireMock.Net.Abstractions/Admin/Mappings/PathModel.cs index 5f7e06e1b..8122c8989 100644 --- a/src/WireMock.Net.Abstractions/Admin/Mappings/PathModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Mappings/PathModel.cs @@ -1,4 +1,4 @@ -namespace WireMock.Admin.Mappings +namespace WireMock.Admin.Mappings { /// /// PathModel @@ -9,6 +9,6 @@ public class PathModel /// /// Gets or sets the matchers. /// - public MatcherModel[] Matchers { get; set; } + public MatcherModel[]? Matchers { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Extensions/RequestModelExtensions.cs b/src/WireMock.Net.Abstractions/Extensions/RequestModelExtensions.cs new file mode 100644 index 000000000..3566d3b49 --- /dev/null +++ b/src/WireMock.Net.Abstractions/Extensions/RequestModelExtensions.cs @@ -0,0 +1,29 @@ +using System.Linq; +using WireMock.Admin.Mappings; + +namespace WireMock.Extensions; + +public static class RequestModelExtensions +{ + public static string? GetPathAsString(this RequestModel request) + { + var path = request.Path switch + { + string pathAsString => pathAsString, + PathModel pathModel => pathModel.Matchers?.FirstOrDefault()?.Pattern as string, + _ => null + }; + + return FixPath(path); + } + + private static string? FixPath(string? path) + { + if (string.IsNullOrEmpty(path)) + { + return path; + } + + return path!.StartsWith("/") ? path : $"/{path}"; + } +} \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Extensions/ResponseModelExtensions.cs b/src/WireMock.Net.Abstractions/Extensions/ResponseModelExtensions.cs new file mode 100644 index 000000000..31fc4b12f --- /dev/null +++ b/src/WireMock.Net.Abstractions/Extensions/ResponseModelExtensions.cs @@ -0,0 +1,20 @@ +using WireMock.Admin.Mappings; + +namespace WireMock.Extensions; + +public static class ResponseModelExtensions +{ + private const string DefaultStatusCode = "200"; + + public static string GetStatusCodeAsString(this ResponseModel response) + { + return response.StatusCode switch + { + string statusCodeAsString => statusCodeAsString, + + int statusCodeAsInt => statusCodeAsInt.ToString(), + + _ => response.StatusCode?.ToString() ?? DefaultStatusCode + }; + } +} \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/IResponseMessage.cs b/src/WireMock.Net.Abstractions/IResponseMessage.cs index 416c339ab..b249171de 100644 --- a/src/WireMock.Net.Abstractions/IResponseMessage.cs +++ b/src/WireMock.Net.Abstractions/IResponseMessage.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using WireMock.ResponseBuilders; using WireMock.Types; using WireMock.Util; @@ -13,7 +13,7 @@ public interface IResponseMessage /// /// The Body. /// - IBodyData BodyData { get; } + IBodyData? BodyData { get; } /// /// Gets the body destination (SameAsSource, String or Bytes). diff --git a/src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.cs b/src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.cs index bd6e9aa97..53f9298c0 100644 --- a/src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.cs +++ b/src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.cs @@ -1,130 +1,132 @@ +#pragma warning disable CS1591 using System.Linq; using FluentAssertions; using FluentAssertions.Execution; using WireMock.Server; // ReSharper disable once CheckNamespace -namespace WireMock.FluentAssertions +namespace WireMock.FluentAssertions; + +public class WireMockAssertions { - public class WireMockAssertions + private readonly IWireMockServer _subject; + private readonly int? _callsCount; + + public WireMockAssertions(IWireMockServer subject, int? callsCount) { - private readonly IWireMockServer _subject; + _subject = subject; + _callsCount = callsCount; + } - public WireMockAssertions(IWireMockServer subject, int? callsCount) - { - _subject = subject; - } + [CustomAssertion] + public AndConstraint AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs) + { + Execute.Assertion + .BecauseOf(because, becauseArgs) + .Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList()) + .ForCondition(requests => requests.Any()) + .FailWith( + "Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but no calls were made.", + absoluteUrl) + .Then + .ForCondition(x => _callsCount == null && x.Any(y => y.AbsoluteUrl == absoluteUrl) || _callsCount == x.Count(y => y.AbsoluteUrl == absoluteUrl)) + .FailWith( + "Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but didn't find it among the calls to {1}.", + _ => absoluteUrl, requests => requests.Select(request => request.AbsoluteUrl)); - [CustomAssertion] - public AndConstraint AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs) - { - Execute.Assertion - .BecauseOf(because, becauseArgs) - .Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList()) - .ForCondition(requests => requests.Any()) - .FailWith( - "Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but no calls were made.", - absoluteUrl) - .Then - .ForCondition(x => x.Any(y => y.AbsoluteUrl == absoluteUrl)) - .FailWith( - "Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but didn't find it among the calls to {1}.", - _ => absoluteUrl, requests => requests.Select(request => request.AbsoluteUrl)); - - return new AndConstraint(this); - } + return new AndConstraint(this); + } - [CustomAssertion] - public AndConstraint WithHeader(string expectedKey, string value, string because = "", params object[] becauseArgs) - => WithHeader(expectedKey, new[] { value }, because, becauseArgs); + [CustomAssertion] + public AndConstraint AtUrl(string url, string because = "", params object[] becauseArgs) + { + Execute.Assertion + .BecauseOf(because, becauseArgs) + .Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList()) + .ForCondition(requests => requests.Any()) + .FailWith( + "Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but no calls were made.", + url) + .Then + .ForCondition(x => _callsCount == null && x.Any(y => y.Url == url) || _callsCount == x.Count(y => y.Url == url)) + .FailWith( + "Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but didn't find it among the calls to {1}.", + _ => url, requests => requests.Select(request => request.Url)); - [CustomAssertion] - public AndConstraint WithHeader(string expectedKey, string[] expectedValues, string because = "", params object[] becauseArgs) - { - var headersDictionary = _subject.LogEntries.SelectMany(x => x.RequestMessage.Headers) - .ToDictionary(x => x.Key, x => x.Value); + return new AndConstraint(this); + } - using (new AssertionScope("headers from requests sent")) - { - headersDictionary.Should().ContainKey(expectedKey, because, becauseArgs); - } + [CustomAssertion] + public AndConstraint WithProxyUrl(string proxyUrl, string because = "", params object[] becauseArgs) + { + Execute.Assertion + .BecauseOf(because, becauseArgs) + .Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList()) + .ForCondition(requests => requests.Any()) + .FailWith( + "Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but no calls were made.", + proxyUrl) + .Then + .ForCondition(x => _callsCount == null && x.Any(y => y.ProxyUrl == proxyUrl) || _callsCount == x.Count(y => y.ProxyUrl == proxyUrl)) + .FailWith( + "Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but didn't find it among the calls with {1}.", + _ => proxyUrl, requests => requests.Select(request => request.ProxyUrl)); - using (new AssertionScope($"header \"{expectedKey}\" from requests sent with value(s)")) - { - if (expectedValues.Length == 1) - { - headersDictionary[expectedKey].Should().Contain(expectedValues.First()); - } - else - { - var trimmedHeaderValues = string.Join(",", headersDictionary[expectedKey].Select(x => x)).Split(',') - .Select(x => x.Trim()) - .ToList(); - foreach (var expectedValue in expectedValues) - { - trimmedHeaderValues.Should().Contain(expectedValue); - } - } - } + return new AndConstraint(this); + } - return new AndConstraint(this); - } + [CustomAssertion] + public AndConstraint FromClientIP(string clientIP, string because = "", params object[] becauseArgs) + { + Execute.Assertion + .BecauseOf(because, becauseArgs) + .Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList()) + .ForCondition(requests => requests.Any()) + .FailWith( + "Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but no calls were made.", + clientIP) + .Then + .ForCondition(x => _callsCount == null && x.Any(y => y.ClientIP == clientIP) || _callsCount == x.Count(y => y.ClientIP == clientIP)) + .FailWith( + "Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but didn't find it among the calls from IP(s) {1}.", + _ => clientIP, requests => requests.Select(request => request.ClientIP)); - [CustomAssertion] - public AndConstraint AtUrl(string url, string because = "", params object[] becauseArgs) - { - Execute.Assertion - .BecauseOf(because, becauseArgs) - .Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList()) - .ForCondition(requests => requests.Any()) - .FailWith( - "Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but no calls were made.", - url) - .Then - .ForCondition(x => x.Any(y => y.Url == url)) - .FailWith( - "Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but didn't find it among the calls to {1}.", - _ => url, requests => requests.Select(request => request.Url)); - - return new AndConstraint(this); - } + return new AndConstraint(this); + } + + [CustomAssertion] + public AndConstraint WithHeader(string expectedKey, string value, string because = "", params object[] becauseArgs) + => WithHeader(expectedKey, new[] { value }, because, becauseArgs); - [CustomAssertion] - public AndConstraint WithProxyUrl(string proxyUrl, string because = "", params object[] becauseArgs) + [CustomAssertion] + public AndConstraint WithHeader(string expectedKey, string[] expectedValues, string because = "", params object[] becauseArgs) + { + var headersDictionary = _subject.LogEntries.SelectMany(x => x.RequestMessage.Headers) + .ToDictionary(x => x.Key, x => x.Value); + + using (new AssertionScope("headers from requests sent")) { - Execute.Assertion - .BecauseOf(because, becauseArgs) - .Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList()) - .ForCondition(requests => requests.Any()) - .FailWith( - "Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but no calls were made.", - proxyUrl) - .Then - .ForCondition(x => x.Any(y => y.ProxyUrl == proxyUrl)) - .FailWith( - "Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but didn't find it among the calls with {1}.", - _ => proxyUrl, requests => requests.Select(request => request.ProxyUrl)); - - return new AndConstraint(this); + headersDictionary.Should().ContainKey(expectedKey, because, becauseArgs); } - [CustomAssertion] - public AndConstraint FromClientIP(string clientIP, string because = "", params object[] becauseArgs) + using (new AssertionScope($"header \"{expectedKey}\" from requests sent with value(s)")) { - Execute.Assertion - .BecauseOf(because, becauseArgs) - .Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList()) - .ForCondition(requests => requests.Any()) - .FailWith( - "Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but no calls were made.", - clientIP) - .Then - .ForCondition(x => x.Any(y => y.ClientIP == clientIP)) - .FailWith( - "Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but didn't find it among the calls from IP(s) {1}.", - _ => clientIP, requests => requests.Select(request => request.ClientIP)); - - return new AndConstraint(this); + if (expectedValues.Length == 1) + { + headersDictionary[expectedKey].Should().Contain(expectedValues.First()); + } + else + { + var trimmedHeaderValues = string.Join(",", headersDictionary[expectedKey].Select(x => x)).Split(',') + .Select(x => x.Trim()) + .ToList(); + foreach (var expectedValue in expectedValues) + { + trimmedHeaderValues.Should().Contain(expectedValue); + } + } } + + return new AndConstraint(this); } } \ No newline at end of file diff --git a/src/WireMock.Net.FluentAssertions/Assertions/WireMockReceivedAssertions.cs b/src/WireMock.Net.FluentAssertions/Assertions/WireMockReceivedAssertions.cs index 3e26dbedb..eba8fd90e 100644 --- a/src/WireMock.Net.FluentAssertions/Assertions/WireMockReceivedAssertions.cs +++ b/src/WireMock.Net.FluentAssertions/Assertions/WireMockReceivedAssertions.cs @@ -17,6 +17,15 @@ public WireMockReceivedAssertions(IWireMockServer server) : base(server) { } + /// + /// Asserts if has received no calls. + /// + /// + public WireMockAssertions HaveReceivedNoCalls() + { + return new WireMockAssertions(Subject, 0); + } + /// /// Asserts if has received a call. /// diff --git a/src/WireMock.Net/Constants/WireMockConstants.cs b/src/WireMock.Net/Constants/WireMockConstants.cs index f4b3bae81..04294a671 100644 --- a/src/WireMock.Net/Constants/WireMockConstants.cs +++ b/src/WireMock.Net/Constants/WireMockConstants.cs @@ -1,9 +1,10 @@ -namespace WireMock.Constants +namespace WireMock.Constants; + +internal static class WireMockConstants { - internal static class WireMockConstants - { - public const int AdminPriority = int.MinValue; - public const int MinPriority = -1_000_000; - public const int ProxyPriority = -2_000_000; - } + public const int AdminPriority = int.MinValue; + public const int MinPriority = -1_000_000; + public const int ProxyPriority = -2_000_000; + + public const string ContentTypeJson = "application/json"; } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/JsonMatcher.cs b/src/WireMock.Net/Matchers/JsonMatcher.cs index e3d6eed97..eed4a542f 100644 --- a/src/WireMock.Net/Matchers/JsonMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonMatcher.cs @@ -1,11 +1,10 @@ -using System; +using System; using System.Collections; using System.Linq; -using JetBrains.Annotations; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using WireMock.Util; using Stef.Validation; +using WireMock.Util; namespace WireMock.Matchers { @@ -38,7 +37,7 @@ public class JsonMatcher : IValueMatcher, IIgnoreCaseMatcher /// The string value to check for equality. /// Ignore the case from the PropertyName and PropertyValue (string only). /// Throw an exception when the internal matching fails because of invalid input. - public JsonMatcher([NotNull] string value, bool ignoreCase = false, bool throwException = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase, throwException) + public JsonMatcher(string value, bool ignoreCase = false, bool throwException = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase, throwException) { } @@ -48,7 +47,7 @@ public JsonMatcher([NotNull] string value, bool ignoreCase = false, bool throwEx /// The object value to check for equality. /// Ignore the case from the PropertyName and PropertyValue (string only). /// Throw an exception when the internal matching fails because of invalid input. - public JsonMatcher([NotNull] object value, bool ignoreCase = false, bool throwException = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase, throwException) + public JsonMatcher(object value, bool ignoreCase = false, bool throwException = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase, throwException) { } @@ -59,7 +58,7 @@ public JsonMatcher([NotNull] object value, bool ignoreCase = false, bool throwEx /// The value to check for equality. /// Ignore the case from the PropertyName and PropertyValue (string only). /// Throw an exception when the internal matching fails because of invalid input. - public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] object value, bool ignoreCase = false, bool throwException = false) + public JsonMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool throwException = false) { Guard.NotNull(value, nameof(value)); @@ -75,12 +74,12 @@ public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] object value, bool i } /// - public double IsMatch(object input) + public double IsMatch(object? input) { bool match = false; // When input is null or byte[], return Mismatch. - if (input != null && !(input is byte[])) + if (input != null && input is not byte[]) { try { @@ -132,7 +131,7 @@ private static JToken ConvertValueToJToken(object value) } } - private static string ToUpper(string input) + private static string? ToUpper(string? input) { return input?.ToUpperInvariant(); } diff --git a/src/WireMock.Net/Matchers/RegexMatcher.cs b/src/WireMock.Net/Matchers/RegexMatcher.cs index f85fd582c..03a775519 100644 --- a/src/WireMock.Net/Matchers/RegexMatcher.cs +++ b/src/WireMock.Net/Matchers/RegexMatcher.cs @@ -8,108 +8,107 @@ using WireMock.RegularExpressions; using Stef.Validation; -namespace WireMock.Matchers +namespace WireMock.Matchers; + +/// +/// Regular Expression Matcher +/// +/// +/// +public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher { + private readonly AnyOf[] _patterns; + private readonly Regex[] _expressions; + + /// + public MatchBehaviour MatchBehaviour { get; } + + /// + public bool ThrowException { get; } + /// - /// Regular Expression Matcher + /// Initializes a new instance of the class. /// - /// - /// - public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher + /// The pattern. + /// Ignore the case from the pattern. + /// Throw an exception when the internal matching fails because of invalid input. + /// Use RegexExtended (default = true). + public RegexMatcher([NotNull, RegexPattern] AnyOf pattern, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true) : + this(MatchBehaviour.AcceptOnMatch, new[] { pattern }, ignoreCase, throwException, useRegexExtended) { - private readonly AnyOf[] _patterns; - private readonly Regex[] _expressions; - - /// - public MatchBehaviour MatchBehaviour { get; } - - /// - public bool ThrowException { get; } - - /// - /// Initializes a new instance of the class. - /// - /// The pattern. - /// Ignore the case from the pattern. - /// Throw an exception when the internal matching fails because of invalid input. - /// Use RegexExtended (default = true). - public RegexMatcher([NotNull, RegexPattern] AnyOf pattern, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true) : - this(MatchBehaviour.AcceptOnMatch, new[] { pattern }, ignoreCase, throwException, useRegexExtended) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The match behaviour. - /// The pattern. - /// Ignore the case from the pattern. - /// Throw an exception when the internal matching fails because of invalid input. - /// Use RegexExtended (default = true). - public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf pattern, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true) : - this(matchBehaviour, new[] { pattern }, ignoreCase, throwException, useRegexExtended) - { - } + } - /// - /// Initializes a new instance of the class. - /// - /// The match behaviour. - /// The patterns. - /// Ignore the case from the pattern. - /// Throw an exception when the internal matching fails because of invalid input. - /// Use RegexExtended (default = true). - public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf[] patterns, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true) - { - Guard.NotNull(patterns, nameof(patterns)); + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The pattern. + /// Ignore the case from the pattern. + /// Throw an exception when the internal matching fails because of invalid input. + /// Use RegexExtended (default = true). + public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf pattern, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true) : + this(matchBehaviour, new[] { pattern }, ignoreCase, throwException, useRegexExtended) + { + } - _patterns = patterns; - IgnoreCase = ignoreCase; - MatchBehaviour = matchBehaviour; - ThrowException = throwException; + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The patterns. + /// Ignore the case from the pattern. + /// Throw an exception when the internal matching fails because of invalid input. + /// Use RegexExtended (default = true). + public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf[] patterns, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true) + { + Guard.NotNull(patterns, nameof(patterns)); - RegexOptions options = RegexOptions.Compiled | RegexOptions.Multiline; + _patterns = patterns; + IgnoreCase = ignoreCase; + MatchBehaviour = matchBehaviour; + ThrowException = throwException; - if (ignoreCase) - { - options |= RegexOptions.IgnoreCase; - } + RegexOptions options = RegexOptions.Compiled | RegexOptions.Multiline; - _expressions = patterns.Select(p => useRegexExtended ? new RegexExtended(p.GetPattern(), options) : new Regex(p.GetPattern(), options)).ToArray(); + if (ignoreCase) + { + options |= RegexOptions.IgnoreCase; } - /// - public virtual double IsMatch(string input) + _expressions = patterns.Select(p => useRegexExtended ? new RegexExtended(p.GetPattern(), options) : new Regex(p.GetPattern(), options)).ToArray(); + } + + /// + public virtual double IsMatch(string input) + { + double match = MatchScores.Mismatch; + if (input != null) { - double match = MatchScores.Mismatch; - if (input != null) + try { - try - { - match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input))); - } - catch (Exception) + match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input))); + } + catch (Exception) + { + if (ThrowException) { - if (ThrowException) - { - throw; - } + throw; } } - - return MatchBehaviourHelper.Convert(MatchBehaviour, match); - } - - /// - public virtual AnyOf[] GetPatterns() - { - return _patterns; } - /// - public virtual string Name => "RegexMatcher"; + return MatchBehaviourHelper.Convert(MatchBehaviour, match); + } - /// - public bool IgnoreCase { get; } + /// + public virtual AnyOf[] GetPatterns() + { + return _patterns; } + + /// + public virtual string Name => nameof(RegexMatcher); + + /// + public bool IgnoreCase { get; } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/WildcardMatcher.cs b/src/WireMock.Net/Matchers/WildcardMatcher.cs index 99423d636..2ebea047c 100644 --- a/src/WireMock.Net/Matchers/WildcardMatcher.cs +++ b/src/WireMock.Net/Matchers/WildcardMatcher.cs @@ -5,75 +5,74 @@ using WireMock.Extensions; using WireMock.Models; -namespace WireMock.Matchers +namespace WireMock.Matchers; + +/// +/// WildcardMatcher +/// +/// +public class WildcardMatcher : RegexMatcher { + private readonly AnyOf[] _patterns; + /// - /// WildcardMatcher + /// Initializes a new instance of the class. /// - /// - public class WildcardMatcher : RegexMatcher + /// The pattern. + /// IgnoreCase + public WildcardMatcher([NotNull] AnyOf pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase) { - private readonly AnyOf[] _patterns; - - /// - /// Initializes a new instance of the class. - /// - /// The pattern. - /// IgnoreCase - public WildcardMatcher([NotNull] AnyOf pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase) - { - } + } - /// - /// Initializes a new instance of the class. - /// - /// The match behaviour. - /// The pattern. - /// IgnoreCase - public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase) - { - } + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The pattern. + /// IgnoreCase + public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase) + { + } - /// - /// Initializes a new instance of the class. - /// - /// The patterns. - /// IgnoreCase - public WildcardMatcher([NotNull] AnyOf[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase) - { - } + /// + /// Initializes a new instance of the class. + /// + /// The patterns. + /// IgnoreCase + public WildcardMatcher([NotNull] AnyOf[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase) + { + } - /// - /// Initializes a new instance of the class. - /// - /// The match behaviour. - /// The patterns. - /// IgnoreCase - /// Throw an exception when the internal matching fails because of invalid input. - public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf[] patterns, bool ignoreCase = false, bool throwException = false) : - base(matchBehaviour, CreateArray(patterns), ignoreCase, throwException) - { - _patterns = patterns; - } + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The patterns. + /// IgnoreCase + /// Throw an exception when the internal matching fails because of invalid input. + public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf[] patterns, bool ignoreCase = false, bool throwException = false) : + base(matchBehaviour, CreateArray(patterns), ignoreCase, throwException) + { + _patterns = patterns; + } - /// - public override AnyOf[] GetPatterns() - { - return _patterns; - } + /// + public override AnyOf[] GetPatterns() + { + return _patterns; + } - /// - public override string Name => "WildcardMatcher"; + /// + public override string Name => nameof(WildcardMatcher); - private static AnyOf[] CreateArray(AnyOf[] patterns) - { - return patterns.Select(pattern => new AnyOf( + private static AnyOf[] CreateArray(AnyOf[] patterns) + { + return patterns.Select(pattern => new AnyOf( new StringPattern { Pattern = "^" + Regex.Escape(pattern.GetPattern()).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", PatternAsFile = pattern.IsSecond ? pattern.Second.PatternAsFile : null })) - .ToArray(); - } + .ToArray(); } } \ No newline at end of file diff --git a/src/WireMock.Net/Owin/Mappers/IOwinResponseMapper.cs b/src/WireMock.Net/Owin/Mappers/IOwinResponseMapper.cs index 461e5c395..34c5954c1 100644 --- a/src/WireMock.Net/Owin/Mappers/IOwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/IOwinResponseMapper.cs @@ -17,6 +17,6 @@ internal interface IOwinResponseMapper /// /// The ResponseMessage /// The OwinResponse/HttpResponse - Task MapAsync(IResponseMessage responseMessage, IResponse response); + Task MapAsync(IResponseMessage? responseMessage, IResponse response); } } \ No newline at end of file diff --git a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs index ff2649a9f..476c232e6 100644 --- a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs @@ -47,20 +47,18 @@ internal class OwinResponseMapper : IOwinResponseMapper /// The IWireMockMiddlewareOptions. public OwinResponseMapper(IWireMockMiddlewareOptions options) { - Guard.NotNull(options, nameof(options)); - - _options = options; + _options = Guard.NotNull(options); } /// - public async Task MapAsync(IResponseMessage responseMessage, IResponse response) + public async Task MapAsync(IResponseMessage? responseMessage, IResponse response) { if (responseMessage == null) { return; } - byte[] bytes; + byte[]? bytes; switch (responseMessage.FaultType) { case FaultType.EMPTY_RESPONSE: @@ -122,33 +120,28 @@ private bool IsFault(IResponseMessage responseMessage) return responseMessage.FaultPercentage == null || _randomizerDouble.Generate() <= responseMessage.FaultPercentage; } - private byte[] GetNormalBody(IResponseMessage responseMessage) + private byte[]? GetNormalBody(IResponseMessage responseMessage) { - byte[] bytes = null; switch (responseMessage.BodyData?.DetectedBodyType) { case BodyType.String: - bytes = (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(responseMessage.BodyData.BodyAsString); - break; + return (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(responseMessage.BodyData.BodyAsString); case BodyType.Json: - Formatting formatting = responseMessage.BodyData.BodyAsJsonIndented == true + var formatting = responseMessage.BodyData.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None; string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore }); - bytes = (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody); - break; + return (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody); case BodyType.Bytes: - bytes = responseMessage.BodyData.BodyAsBytes; - break; + return responseMessage.BodyData.BodyAsBytes; case BodyType.File: - bytes = _options.FileSystemHandler.ReadResponseBodyAsFile(responseMessage.BodyData.BodyAsFile); - break; + return _options.FileSystemHandler.ReadResponseBodyAsFile(responseMessage.BodyData.BodyAsFile); } - return bytes; + return null; } private static void SetResponseHeaders(IResponseMessage responseMessage, IResponse response) diff --git a/src/WireMock.Net/Owin/WireMockMiddleware.cs b/src/WireMock.Net/Owin/WireMockMiddleware.cs index 80dc8d125..5c79eecf6 100644 --- a/src/WireMock.Net/Owin/WireMockMiddleware.cs +++ b/src/WireMock.Net/Owin/WireMockMiddleware.cs @@ -72,8 +72,8 @@ private async Task InvokeInternalAsync(IContext ctx) var request = await _requestMapper.MapAsync(ctx.Request, _options).ConfigureAwait(false); var logRequest = false; - IResponseMessage response = null; - (MappingMatcherResult Match, MappingMatcherResult Partial) result = (null, null); + IResponseMessage? response = null; + (MappingMatcherResult? Match, MappingMatcherResult? Partial) result = (null, null); try { foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null)) diff --git a/src/WireMock.Net/ResponseMessage.cs b/src/WireMock.Net/ResponseMessage.cs index 9420aee8a..c4300b3e3 100644 --- a/src/WireMock.Net/ResponseMessage.cs +++ b/src/WireMock.Net/ResponseMessage.cs @@ -1,4 +1,4 @@ -// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License. +// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License. // For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root. using System.Collections.Generic; using System.Linq; @@ -27,7 +27,7 @@ public class ResponseMessage : IResponseMessage public string BodyDestination { get; set; } /// - public IBodyData BodyData { get; set; } + public IBodyData? BodyData { get; set; } /// public FaultType FaultType { get; set; } diff --git a/src/WireMock.Net/ResponseMessageBuilder.cs b/src/WireMock.Net/ResponseMessageBuilder.cs index b5d1e2e1a..1b353c0dc 100644 --- a/src/WireMock.Net/ResponseMessageBuilder.cs +++ b/src/WireMock.Net/ResponseMessageBuilder.cs @@ -1,50 +1,49 @@ -using System; +using System; using System.Collections.Generic; using WireMock.Admin.Mappings; +using WireMock.Constants; using WireMock.Http; using WireMock.Types; using WireMock.Util; -namespace WireMock +namespace WireMock; + +internal static class ResponseMessageBuilder { - internal static class ResponseMessageBuilder + private static readonly IDictionary> ContentTypeJsonHeaders = new Dictionary> + { + { HttpKnownHeaderNames.ContentType, new WireMockList { WireMockConstants.ContentTypeJson } } + }; + + internal static ResponseMessage Create(string? message, int statusCode = 200, Guid? guid = null) { - private static string ContentTypeJson = "application/json"; - private static readonly IDictionary> ContentTypeJsonHeaders = new Dictionary> + var response = new ResponseMessage { - { HttpKnownHeaderNames.ContentType, new WireMockList { ContentTypeJson } } + StatusCode = statusCode, + Headers = ContentTypeJsonHeaders }; - internal static ResponseMessage Create(string message, int statusCode = 200, Guid? guid = null) + if (message != null) { - var response = new ResponseMessage - { - StatusCode = statusCode, - Headers = ContentTypeJsonHeaders - }; - - if (message != null) + response.BodyData = new BodyData { - response.BodyData = new BodyData + DetectedBodyType = BodyType.Json, + BodyAsJson = new StatusModel { - DetectedBodyType = BodyType.Json, - BodyAsJson = new StatusModel - { - Guid = guid, - Status = message - } - }; - } - - return response; + Guid = guid, + Status = message + } + }; } - internal static ResponseMessage Create(int statusCode) + return response; + } + + internal static ResponseMessage Create(int statusCode) + { + return new ResponseMessage { - return new ResponseMessage - { - StatusCode = statusCode - }; - } + StatusCode = statusCode + }; } } \ No newline at end of file diff --git a/src/WireMock.Net/Serialization/JsonSerializationConstants.cs b/src/WireMock.Net/Serialization/JsonSerializationConstants.cs index 848bd6af2..a73f1ee3f 100644 --- a/src/WireMock.Net/Serialization/JsonSerializationConstants.cs +++ b/src/WireMock.Net/Serialization/JsonSerializationConstants.cs @@ -1,30 +1,34 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; -namespace WireMock.Serialization +namespace WireMock.Serialization; + +internal static class JsonSerializationConstants { - internal static class JsonSerializationConstants + public static readonly JsonSerializerSettings JsonSerializerSettingsDefault = new() { - public static readonly JsonSerializerSettings JsonSerializerSettingsDefault = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - NullValueHandling = NullValueHandling.Ignore - }; + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore + }; - public static readonly JsonSerializerSettings JsonSerializerSettingsIncludeNullValues = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - NullValueHandling = NullValueHandling.Include - }; + public static readonly JsonSerializerSettings JsonSerializerSettingsIncludeNullValues = new() + { + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Include + }; - public static readonly JsonSerializerSettings JsonDeserializerSettingsWithDateParsingNone = new JsonSerializerSettings - { - DateParseHandling = DateParseHandling.None - }; + public static readonly JsonSerializerSettings JsonDeserializerSettingsWithDateParsingNone = new() + { + DateParseHandling = DateParseHandling.None + }; - public static readonly JsonSerializerSettings JsonSerializerSettingsPact = new JsonSerializerSettings + public static readonly JsonSerializerSettings JsonSerializerSettingsPact = new() + { + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore, + ContractResolver = new DefaultContractResolver { - Formatting = Formatting.Indented, - NullValueHandling = NullValueHandling.Ignore - }; - } + NamingStrategy = new CamelCaseNamingStrategy() + } + }; } \ No newline at end of file diff --git a/src/WireMock.Net/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs index 621d4d54d..87b860fc3 100644 --- a/src/WireMock.Net/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net/Serialization/MatcherMapper.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using AnyOfTypes; -using JetBrains.Annotations; using SimMetrics.Net; using WireMock.Admin.Mappings; using WireMock.Extensions; @@ -22,12 +21,12 @@ public MatcherMapper(WireMockServerSettings settings) _settings = settings ?? throw new ArgumentNullException(nameof(settings)); } - public IMatcher[] Map([CanBeNull] IEnumerable matchers) + public IMatcher[] Map(IEnumerable? matchers) { return matchers?.Select(Map).Where(m => m != null).ToArray(); } - public IMatcher Map([CanBeNull] MatcherModel matcher) + public IMatcher? Map(MatcherModel? matcher) { if (matcher == null) { @@ -36,7 +35,7 @@ public IMatcher Map([CanBeNull] MatcherModel matcher) string[] parts = matcher.Name.Split('.'); string matcherName = parts[0]; - string matcherType = parts.Length > 1 ? parts[1] : null; + string? matcherType = parts.Length > 1 ? parts[1] : null; var stringPatterns = ParseStringPatterns(matcher); var matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch; bool ignoreCase = matcher.IgnoreCase == true; @@ -69,16 +68,16 @@ public IMatcher Map([CanBeNull] MatcherModel matcher) return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails, useRegexExtended); case nameof(JsonMatcher): - object valueForJsonMatcher = matcher.Pattern ?? matcher.Patterns; - return new JsonMatcher(matchBehaviour, valueForJsonMatcher, ignoreCase, throwExceptionWhenMatcherFails); + var valueForJsonMatcher = matcher.Pattern ?? matcher.Patterns; + return new JsonMatcher(matchBehaviour, valueForJsonMatcher!, ignoreCase, throwExceptionWhenMatcherFails); case nameof(JsonPartialMatcher): - object valueForJsonPartialMatcher = matcher.Pattern ?? matcher.Patterns; - return new JsonPartialMatcher(matchBehaviour, valueForJsonPartialMatcher, ignoreCase, throwExceptionWhenMatcherFails); + var valueForJsonPartialMatcher = matcher.Pattern ?? matcher.Patterns; + return new JsonPartialMatcher(matchBehaviour, valueForJsonPartialMatcher!, ignoreCase, throwExceptionWhenMatcherFails); case nameof(JsonPartialWildcardMatcher): - object valueForJsonPartialWildcardMatcher = matcher.Pattern ?? matcher.Patterns; - return new JsonPartialWildcardMatcher(matchBehaviour, valueForJsonPartialWildcardMatcher, ignoreCase, throwExceptionWhenMatcherFails); + var valueForJsonPartialWildcardMatcher = matcher.Pattern ?? matcher.Patterns; + return new JsonPartialWildcardMatcher(matchBehaviour, valueForJsonPartialWildcardMatcher!, ignoreCase, throwExceptionWhenMatcherFails); case nameof(JsonPathMatcher): return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns); @@ -114,12 +113,12 @@ public IMatcher Map([CanBeNull] MatcherModel matcher) } } - public MatcherModel[] Map([CanBeNull] IEnumerable matchers) + public MatcherModel[] Map(IEnumerable? matchers) { return matchers?.Select(Map).Where(m => m != null).ToArray(); } - public MatcherModel Map([CanBeNull] IMatcher matcher) + public MatcherModel? Map(IMatcher? matcher) { if (matcher == null) { diff --git a/src/WireMock.Net/Server/WireMockServer.Pact.cs b/src/WireMock.Net/Serialization/PactMapper.cs similarity index 60% rename from src/WireMock.Net/Server/WireMockServer.Pact.cs rename to src/WireMock.Net/Serialization/PactMapper.cs index 197ed0574..44e2cf7eb 100644 --- a/src/WireMock.Net/Server/WireMockServer.Pact.cs +++ b/src/WireMock.Net/Serialization/PactMapper.cs @@ -2,28 +2,25 @@ using System.Collections.Generic; using System.Linq; using WireMock.Admin.Mappings; +using WireMock.Extensions; +using WireMock.Matchers; using WireMock.Pact.Models.V2; +using WireMock.Server; using WireMock.Util; -namespace WireMock.Server; +namespace WireMock.Serialization; -public partial class WireMockServer +internal static class PactMapper { - private const string DefaultPath = "/"; private const string DefaultMethod = "GET"; - private const int DefaultStatus = 200; + private const int DefaultStatusCode = 200; private const string DefaultConsumer = "Default Consumer"; private const string DefaultProvider = "Default Provider"; - /// - /// Save the mappings as a Pact Json file V2. - /// - /// The folder to save the pact file. - /// The filename for the .json file [optional]. - public void SavePact(string folder, string? filename = null) + public static (string FileName, byte[] Bytes) ToPact(WireMockServer server, string? filename = null) { - var consumer = Consumer ?? DefaultConsumer; - var provider = Provider ?? DefaultProvider; + var consumer = server.Consumer ?? DefaultConsumer; + var provider = server.Provider ?? DefaultProvider; filename ??= $"{consumer} - {provider}.json"; @@ -33,41 +30,31 @@ public void SavePact(string folder, string? filename = null) Provider = new Pacticipant { Name = provider } }; - foreach (var mapping in MappingModels) + foreach (var mapping in server.MappingModels.OrderBy(m => m.Guid)) { + var path = mapping.Request.GetPathAsString(); + if (path == null) + { + // Path is null (probably a Func<>), skip this. + continue; + } + var interaction = new Interaction { Description = mapping.Description, ProviderState = mapping.Title, - Request = MapRequest(mapping.Request), + Request = MapRequest(mapping.Request, path), Response = MapResponse(mapping.Response) }; pact.Interactions.Add(interaction); } - var bytes = JsonUtils.SerializeAsPactFile(pact); - _settings.FileSystemHandler.WriteFile(folder, filename, bytes); + return (filename, JsonUtils.SerializeAsPactFile(pact)); } - private static Request MapRequest(RequestModel request) + private static Request MapRequest(RequestModel request, string path) { - string path; - switch (request.Path) - { - case string pathAsString: - path = pathAsString; - break; - - case PathModel pathModel: - path = GetPatternAsStringFromMatchers(pathModel.Matchers, DefaultPath); - break; - - default: - path = DefaultPath; - break; - } - return new Request { Method = request.Methods?.FirstOrDefault() ?? DefaultMethod, @@ -97,7 +84,7 @@ private static int MapStatusCode(object? statusCode) { if (statusCode is string statusCodeAsString) { - return int.TryParse(statusCodeAsString, out var statusCodeAsInt) ? statusCodeAsInt : DefaultStatus; + return int.TryParse(statusCodeAsString, out var statusCodeAsInt) ? statusCodeAsInt : DefaultStatusCode; } if (statusCode != null) @@ -106,7 +93,7 @@ private static int MapStatusCode(object? statusCode) return Convert.ToInt32(statusCode); } - return DefaultStatus; + return DefaultStatusCode; } private static string? MapQueryParameters(IList? queryParameters) @@ -118,41 +105,37 @@ private static int MapStatusCode(object? statusCode) var values = queryParameters .Where(qp => qp.Matchers != null && qp.Matchers.Any() && qp.Matchers[0].Pattern is string) - .Select(param => $"{Uri.EscapeDataString(param.Name)}={Uri.EscapeDataString((string)param.Matchers![0].Pattern)}"); + .Select(param => $"{Uri.EscapeDataString(param.Name)}={Uri.EscapeDataString((string)param.Matchers![0].Pattern!)}"); return string.Join("&", values); } private static IDictionary? MapRequestHeaders(IList? headers) { - if (headers == null) - { - return null; - } - - var validHeaders = headers.Where(h => h.Matchers != null && h.Matchers.Any() && h.Matchers[0].Pattern is string); - return validHeaders.ToDictionary(x => x.Name, y => (string)y.Matchers![0].Pattern); + var validHeaders = headers?.Where(h => h.Matchers != null && h.Matchers.Any() && h.Matchers[0].Pattern is string); + return validHeaders?.ToDictionary(x => x.Name, y => (string)y.Matchers![0].Pattern!); } private static IDictionary? MapResponseHeaders(IDictionary? headers) { - if (headers == null) - { - return null; - } - - var validHeaders = headers.Where(h => h.Value is string); - return validHeaders.ToDictionary(x => x.Key, y => (string)y.Value); + var validHeaders = headers?.Where(h => h.Value is string); + return validHeaders?.ToDictionary(x => x.Key, y => (string)y.Value); } private static object? MapBody(BodyModel? body) { - if (body == null || body.Matcher.Name != "JsonMatcher") + if (body?.Matcher == null || body.Matchers == null) { return null; } - return body.Matcher.Pattern; + if (body.Matcher is { Name: nameof(JsonMatcher) }) + { + return body.Matcher.Pattern; + } + + var jsonMatcher = body.Matchers.FirstOrDefault(m => m.Name == nameof(JsonMatcher)); + return jsonMatcher?.Pattern; } private static string GetPatternAsStringFromMatchers(MatcherModel[]? matchers, string defaultValue) diff --git a/src/WireMock.Net/Serialization/SwaggerMapper.cs b/src/WireMock.Net/Serialization/SwaggerMapper.cs new file mode 100644 index 000000000..9439974d2 --- /dev/null +++ b/src/WireMock.Net/Serialization/SwaggerMapper.cs @@ -0,0 +1,325 @@ +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json; +using NJsonSchema; +using NJsonSchema.Extensions; +using NSwag; +using WireMock.Admin.Mappings; +using WireMock.Constants; +using WireMock.Extensions; +using WireMock.Matchers; +using WireMock.Server; +using WireMock.Util; + +namespace WireMock.Serialization; + +internal static class SwaggerMapper +{ + private const string DefaultMethod = "GET"; + private const string Generator = "WireMock.Net"; + + private static readonly JsonSchema JsonSchemaString = new() { Type = JsonObjectType.String }; + + public static string ToSwagger(WireMockServer server) + { + var openApiDocument = new OpenApiDocument + { + Generator = Generator, + Info = new OpenApiInfo + { + Title = $"{Generator} Mappings Swagger specification", + Version = SystemUtils.Version + }, + }; + + foreach (var url in server.Urls) + { + openApiDocument.Servers.Add(new OpenApiServer + { + Url = url + }); + } + + foreach (var mapping in server.MappingModels) + { + var path = mapping.Request.GetPathAsString(); + if (path == null) + { + // Path is null (probably a Func<>), skip this. + continue; + } + + var operation = new OpenApiOperation(); + foreach (var openApiParameter in MapRequestQueryParameters(mapping.Request.Params)) + { + operation.Parameters.Add(openApiParameter); + } + foreach (var openApiParameter in MapRequestHeaders(mapping.Request.Headers)) + { + operation.Parameters.Add(openApiParameter); + } + foreach (var openApiParameter in MapRequestCookies(mapping.Request.Cookies)) + { + operation.Parameters.Add(openApiParameter); + } + + operation.RequestBody = MapRequestBody(mapping.Request); + + var response = MapResponse(mapping.Response); + if (response != null) + { + operation.Responses.Add(mapping.Response.GetStatusCodeAsString(), response); + } + + var method = mapping.Request.Methods?.FirstOrDefault() ?? DefaultMethod; + if (!openApiDocument.Paths.ContainsKey(path)) + { + var openApiPathItem = new OpenApiPathItem + { + { method, operation } + }; + + openApiDocument.Paths.Add(path, openApiPathItem); + } + else + { + // The combination of path+method uniquely identify an operation. Duplicates are not allowed. + if (!openApiDocument.Paths[path].ContainsKey(method)) + { + openApiDocument.Paths[path].Add(method, operation); + } + } + } + + return openApiDocument.ToJson(SchemaType.OpenApi3, Formatting.Indented); + } + + private static IEnumerable MapRequestQueryParameters(IList? queryParameters) + { + if (queryParameters == null) + { + return new List(); + } + + return queryParameters + .Where(x => x.Matchers != null && x.Matchers.Any()) + .Select(x => new + { + x.Name, + Details = GetDetailsFromMatcher(x.Matchers![0]) + }) + .Select(x => new OpenApiParameter + { + Name = x.Name, + Example = x.Details.Example, + Description = x.Details.Description, + Kind = OpenApiParameterKind.Query, + Schema = x.Details.JsonSchemaRegex, + IsRequired = !x.Details.Reject + }) + .ToList(); + } + + private static IEnumerable MapRequestHeaders(IList? headers) + { + if (headers == null) + { + return new List(); + } + + return headers + .Where(x => x.Matchers != null && x.Matchers.Any()) + .Select(x => new + { + x.Name, + Details = GetDetailsFromMatcher(x.Matchers![0]) + }) + .Select(x => new OpenApiHeader + { + Name = x.Name, + Example = x.Details.Example, + Description = x.Details.Description, + Kind = OpenApiParameterKind.Header, + Schema = x.Details.JsonSchemaRegex, + IsRequired = !x.Details.Reject + }) + .ToList(); + } + + private static IEnumerable MapRequestCookies(IList? cookies) + { + if (cookies == null) + { + return new List(); + } + + return cookies + .Where(x => x.Matchers != null && x.Matchers.Any()) + .Select(x => new + { + x.Name, + Details = GetDetailsFromMatcher(x.Matchers![0]) + }) + .Select(x => new OpenApiParameter + { + Name = x.Name, + Example = x.Details.Example, + Description = x.Details.Description, + Kind = OpenApiParameterKind.Cookie, + Schema = x.Details.JsonSchemaRegex, + IsRequired = !x.Details.Reject + }) + .ToList(); + } + + private static (JsonSchema JsonSchemaRegex, string? Example, string? Description, bool Reject) GetDetailsFromMatcher(MatcherModel matcher) + { + var pattern = GetPatternAsStringFromMatcher(matcher); + var reject = matcher.RejectOnMatch == true; + var description = $"{matcher.Name} with RejectOnMatch = '{reject}' and Pattern = '{pattern}'"; + + return matcher.Name is nameof(RegexMatcher) ? + (new JsonSchema { Type = JsonObjectType.String, Format = "regex", Pattern = pattern }, pattern, description, reject) : + (JsonSchemaString, pattern, description, reject); + } + + private static OpenApiRequestBody? MapRequestBody(RequestModel request) + { + var body = MapRequestBody(request.Body); + if (body == null) + { + return null; + } + + var openApiMediaType = new OpenApiMediaType + { + Schema = GetJsonSchema(body) + }; + + var requestBodyPost = new OpenApiRequestBody(); + requestBodyPost.Content.Add(GetContentType(request), openApiMediaType); + + return requestBodyPost; + } + + private static OpenApiResponse? MapResponse(ResponseModel response) + { + if (response.Body != null) + { + return new OpenApiResponse + { + Schema = new JsonSchemaProperty + { + Type = JsonObjectType.String, + Example = response.Body + } + }; + } + + if (response.BodyAsBytes != null) + { + // https://stackoverflow.com/questions/62794949/how-to-define-byte-array-in-openapi-3-0 + return new OpenApiResponse + { + Schema = new JsonSchemaProperty + { + Type = JsonObjectType.Array, + Items = + { + new JsonSchema + { + Type = JsonObjectType.String, + Format = JsonFormatStrings.Byte + } + } + } + }; + } + + if (response.BodyAsJson == null) + { + return null; + } + + return new OpenApiResponse + { + Schema = GetJsonSchema(response.BodyAsJson) + }; + } + + private static JsonSchema GetJsonSchema(object instance) + { + switch (instance) + { + case string instanceAsString: + try + { + var value = JsonConvert.DeserializeObject(instanceAsString); + return GetJsonSchema(value!); + } + catch + { + return JsonSchemaString; + } + + default: + return instance.ToJsonSchema(); + } + } + + private static object? MapRequestBody(BodyModel? body) + { + if (body == null) + { + return null; + } + + var matcher = GetMatcher(body.Matcher, body.Matchers); + if (matcher is { Name: nameof(JsonMatcher) }) + { + var pattern = GetPatternAsStringFromMatcher(matcher); + if (JsonUtils.TryParseAsJObject(pattern, out var jObject)) + { + return jObject; + } + + return pattern; + } + + return null; + } + + private static string GetContentType(RequestModel request) + { + var contentType = request.Headers?.FirstOrDefault(h => h.Name == "Content-Type"); + + return contentType == null ? + WireMockConstants.ContentTypeJson : + GetPatternAsStringFromMatchers(contentType.Matchers, WireMockConstants.ContentTypeJson); + } + + private static string GetPatternAsStringFromMatchers(IList? matchers, string defaultValue) + { + if (matchers == null || !matchers.Any()) + { + return defaultValue; + } + + return GetPatternAsStringFromMatcher(matchers.First()) ?? defaultValue; + } + + private static string? GetPatternAsStringFromMatcher(MatcherModel matcher) + { + if (matcher.Pattern is string patternAsString) + { + return patternAsString; + } + + return matcher.Patterns?.FirstOrDefault() as string; + } + + private static MatcherModel? GetMatcher(MatcherModel? matcher, MatcherModel[]? matchers) + { + return matcher ?? matchers?.FirstOrDefault(); + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Server/WireMockServer.Admin.cs b/src/WireMock.Net/Server/WireMockServer.Admin.cs index 298746197..898845847 100644 --- a/src/WireMock.Net/Server/WireMockServer.Admin.cs +++ b/src/WireMock.Net/Server/WireMockServer.Admin.cs @@ -35,7 +35,6 @@ namespace WireMock.Server; public partial class WireMockServer { private const int EnhancedFileSystemWatcherTimeoutMs = 1000; - private const string ContentTypeJson = "application/json"; private const string AdminFiles = "/__admin/files"; private const string AdminMappings = "/__admin/mappings"; private const string AdminMappingsWireMockOrg = "/__admin/mappings/wiremock.org"; @@ -45,7 +44,7 @@ public partial class WireMockServer private const string QueryParamReloadStaticMappings = "reloadStaticMappings"; private readonly Guid _proxyMappingGuid = new("e59914fd-782e-428e-91c1-4810ffb86567"); - private readonly RegexMatcher _adminRequestContentTypeJson = new ContentTypeMatcher(ContentTypeJson, true); + private readonly RegexMatcher _adminRequestContentTypeJson = new ContentTypeMatcher(WireMockConstants.ContentTypeJson, true); private readonly RegexMatcher _adminMappingsGuidPathMatcher = new(@"^\/__admin\/mappings\/([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})$"); private readonly RegexMatcher _adminRequestsGuidPathMatcher = new(@"^\/__admin\/requests\/([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})$"); @@ -73,7 +72,10 @@ private void InitAdmin() Given(Request.Create().WithPath(_adminMappingsGuidPathMatcher).UsingDelete()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingDelete)); // __admin/mappings/save - Given(Request.Create().WithPath(AdminMappings + "/save").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingsSave)); + Given(Request.Create().WithPath($"{AdminMappings}/save").UsingPost()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(MappingsSave)); + + // __admin/mappings/swagger + Given(Request.Create().WithPath($"{AdminMappings}/swagger").UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(SwaggerGet)); // __admin/requests Given(Request.Create().WithPath(AdminRequests).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(RequestsGet)); @@ -148,7 +150,7 @@ public void ReadStaticMappings(string? folder = null) /// [PublicAPI] - public void WatchStaticMappings([CanBeNull] string folder = null) + public void WatchStaticMappings(string? folder = null) { if (folder == null) { @@ -379,6 +381,20 @@ private Guid ParseGuidFromRequestMessage(IRequestMessage requestMessage) #endregion Mapping/{guid} #region Mappings + private IResponseMessage SwaggerGet(IRequestMessage requestMessage) + { + return new ResponseMessage + { + BodyData = new BodyData + { + DetectedBodyType = BodyType.String, + BodyAsString = SwaggerMapper.ToSwagger(this) + }, + StatusCode = (int)HttpStatusCode.OK, + Headers = new Dictionary> { { HttpKnownHeaderNames.ContentType, new WireMockList(WireMockConstants.ContentTypeJson) } } + }; + } + private IResponseMessage MappingsSave(IRequestMessage requestMessage) { SaveStaticMappings(); @@ -667,6 +683,19 @@ private IResponseMessage ScenariosReset(IRequestMessage requestMessage) } #endregion + #region Pact + /// + /// Save the mappings as a Pact Json file V2. + /// + /// The folder to save the pact file. + /// The filename for the .json file [optional]. + [PublicAPI] + public void SavePact(string folder, string? filename = null) + { + var (filenameUpdated, bytes) = PactMapper.ToPact(this, filename); + _settings.FileSystemHandler.WriteFile(folder, filenameUpdated, bytes); + } + /// /// This stores details about the consumer of the interaction. /// @@ -688,7 +717,7 @@ public WireMockServer WithProvider(string provider) Provider = provider; return this; } - + #endregion private IRequestBuilder? InitRequestBuilder(RequestModel requestModel, bool pathOrUrlRequired) { IRequestBuilder requestBuilder = Request.Create(); @@ -904,68 +933,6 @@ private IResponseBuilder InitResponseBuilder(ResponseModel responseModel) return responseBuilder; } - private ResponseMessage ToJson(T result, bool keepNullValues = false) - { - return new ResponseMessage - { - BodyData = new BodyData - { - DetectedBodyType = BodyType.String, - BodyAsString = JsonConvert.SerializeObject(result, keepNullValues ? JsonSerializationConstants.JsonSerializerSettingsIncludeNullValues : JsonSerializationConstants.JsonSerializerSettingsDefault) - }, - StatusCode = (int)HttpStatusCode.OK, - Headers = new Dictionary> { { HttpKnownHeaderNames.ContentType, new WireMockList(ContentTypeJson) } } - }; - } - - private Encoding? ToEncoding(EncodingModel? encodingModel) - { - return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null; - } - - private T? DeserializeObject(IRequestMessage requestMessage) - { - if (requestMessage?.BodyData?.DetectedBodyType == BodyType.String) - { - return JsonUtils.DeserializeObject(requestMessage.BodyData.BodyAsString); - } - - if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json) - { - return ((JObject)requestMessage.BodyData.BodyAsJson).ToObject(); - } - - return default(T); - } - - private T[] DeserializeRequestMessageToArray(IRequestMessage requestMessage) - { - if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json) - { - var bodyAsJson = requestMessage.BodyData.BodyAsJson; - - return DeserializeObjectToArray(bodyAsJson); - } - - return default(T[]); - } - - private T[] DeserializeObjectToArray(object value) - { - if (value is JArray jArray) - { - return jArray.ToObject(); - } - - var singleResult = ((JObject)value).ToObject(); - return new[] { singleResult }; - } - - private T[] DeserializeJsonToArray(string value) - { - return DeserializeObjectToArray(JsonUtils.DeserializeObject(value)); - } - private void DisposeEnhancedFileSystemWatcher() { if (_enhancedFileSystemWatcher != null) @@ -1012,4 +979,66 @@ private void EnhancedFileSystemWatcherDeleted(object sender, FileSystemEventArgs DeleteMapping(args.FullPath); } } + + private static Encoding? ToEncoding(EncodingModel? encodingModel) + { + return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null; + } + + private static ResponseMessage ToJson(T result, bool keepNullValues = false) + { + return new ResponseMessage + { + BodyData = new BodyData + { + DetectedBodyType = BodyType.String, + BodyAsString = JsonConvert.SerializeObject(result, keepNullValues ? JsonSerializationConstants.JsonSerializerSettingsIncludeNullValues : JsonSerializationConstants.JsonSerializerSettingsDefault) + }, + StatusCode = (int)HttpStatusCode.OK, + Headers = new Dictionary> { { HttpKnownHeaderNames.ContentType, new WireMockList(WireMockConstants.ContentTypeJson) } } + }; + } + + private static T? DeserializeObject(IRequestMessage requestMessage) + { + if (requestMessage?.BodyData?.DetectedBodyType == BodyType.String) + { + return JsonUtils.DeserializeObject(requestMessage.BodyData.BodyAsString); + } + + if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json) + { + return ((JObject)requestMessage.BodyData.BodyAsJson).ToObject(); + } + + return default(T); + } + + private static T[] DeserializeRequestMessageToArray(IRequestMessage requestMessage) + { + if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json) + { + var bodyAsJson = requestMessage.BodyData.BodyAsJson; + + return DeserializeObjectToArray(bodyAsJson); + } + + return default(T[]); + } + + private static T[] DeserializeJsonToArray(string value) + { + return DeserializeObjectToArray(JsonUtils.DeserializeObject(value)); + } + + private static T[] DeserializeObjectToArray(object value) + { + if (value is JArray jArray) + { + return jArray.ToObject(); + } + + var singleResult = ((JObject)value).ToObject(); + return new[] { singleResult }; + } } \ No newline at end of file diff --git a/src/WireMock.Net/Server/WireMockServer.LogEntries.cs b/src/WireMock.Net/Server/WireMockServer.LogEntries.cs index e79c8d572..5030c0713 100644 --- a/src/WireMock.Net/Server/WireMockServer.LogEntries.cs +++ b/src/WireMock.Net/Server/WireMockServer.LogEntries.cs @@ -5,87 +5,89 @@ using System.Collections.Specialized; using System.Linq; using JetBrains.Annotations; +using Stef.Validation; using WireMock.Logging; using WireMock.Matchers; using WireMock.Matchers.Request; -namespace WireMock.Server +namespace WireMock.Server; + +public partial class WireMockServer { - public partial class WireMockServer + /// + [PublicAPI] + public event NotifyCollectionChangedEventHandler LogEntriesChanged { - /// - [PublicAPI] - public event NotifyCollectionChangedEventHandler LogEntriesChanged + add { - add + _options.LogEntries.CollectionChanged += (sender, eventRecordArgs) => { - _options.LogEntries.CollectionChanged += (sender, eventRecordArgs) => + try { - try - { - value(sender, eventRecordArgs); - } - catch (Exception exception) - { - _options.Logger.Error("Error calling the LogEntriesChanged event handler: {0}", exception.Message); - } - }; - } - - remove => _options.LogEntries.CollectionChanged -= value; + value(sender, eventRecordArgs); + } + catch (Exception exception) + { + _options.Logger.Error("Error calling the LogEntriesChanged event handler: {0}", exception.Message); + } + }; } - /// - [PublicAPI] - public IEnumerable LogEntries => new ReadOnlyCollection(_options.LogEntries.ToList()); - - /// - /// The search log-entries based on matchers. - /// - /// The matchers. - /// The . - [PublicAPI] - public IEnumerable FindLogEntries([NotNull] params IRequestMatcher[] matchers) - { - var results = new Dictionary(); + remove => _options.LogEntries.CollectionChanged -= value; + } - foreach (var log in _options.LogEntries.ToList()) - { - var requestMatchResult = new RequestMatchResult(); - foreach (var matcher in matchers) - { - matcher.GetMatchingScore(log.RequestMessage, requestMatchResult); - } + /// + [PublicAPI] + public IEnumerable LogEntries => new ReadOnlyCollection(_options.LogEntries.ToList()); - if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect) - { - results.Add(log, requestMatchResult); - } - } + /// + /// The search log-entries based on matchers. + /// + /// The matchers. + /// The . + [PublicAPI] + public IEnumerable FindLogEntries(params IRequestMatcher[] matchers) + { + Guard.NotNull(matchers); - return new ReadOnlyCollection(results.OrderBy(x => x.Value).Select(x => x.Key).ToList()); - } + var results = new Dictionary(); - /// - [PublicAPI] - public void ResetLogEntries() + foreach (var log in _options.LogEntries.ToList()) { - _options.LogEntries.Clear(); - } + var requestMatchResult = new RequestMatchResult(); + foreach (var matcher in matchers) + { + matcher.GetMatchingScore(log.RequestMessage, requestMatchResult); + } - /// - [PublicAPI] - public bool DeleteLogEntry(Guid guid) - { - // Check a LogEntry exists with the same GUID, if so, remove it. - var existing = _options.LogEntries.ToList().FirstOrDefault(m => m.Guid == guid); - if (existing != null) + if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect) { - _options.LogEntries.Remove(existing); - return true; + results.Add(log, requestMatchResult); } + } - return false; + return new ReadOnlyCollection(results.OrderBy(x => x.Value).Select(x => x.Key).ToList()); + } + + /// + [PublicAPI] + public void ResetLogEntries() + { + _options.LogEntries.Clear(); + } + + /// + [PublicAPI] + public bool DeleteLogEntry(Guid guid) + { + // Check a LogEntry exists with the same GUID, if so, remove it. + var existing = _options.LogEntries.ToList().FirstOrDefault(m => m.Guid == guid); + if (existing != null) + { + _options.LogEntries.Remove(existing); + return true; } + + return false; } } \ No newline at end of file diff --git a/src/WireMock.Net/Util/JsonUtils.cs b/src/WireMock.Net/Util/JsonUtils.cs index 6af5b20b4..b464826eb 100644 --- a/src/WireMock.Net/Util/JsonUtils.cs +++ b/src/WireMock.Net/Util/JsonUtils.cs @@ -5,18 +5,49 @@ using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using WireMock.Pact.Models.V2; using WireMock.Serialization; namespace WireMock.Util; internal static class JsonUtils { - public static bool TryParseAsComplexObject(string strInput, [NotNullWhen(true)] out JToken? token) + public static Type CreateTypeFromJObject(JObject instance, string? fullName = null) { - token = null; + static Type ConvertType(JToken value, string? propertyName = null) + { + var type = value.Type; + return type switch + { + JTokenType.Array => value.HasValues ? ConvertType(value.First!, propertyName).MakeArrayType() : typeof(object).MakeArrayType(), + JTokenType.Boolean => typeof(bool), + JTokenType.Bytes => typeof(byte[]), + JTokenType.Date => typeof(DateTime), + JTokenType.Guid => typeof(Guid), + JTokenType.Float => typeof(float), + JTokenType.Integer => typeof(long), + JTokenType.Null => typeof(object), + JTokenType.Object => CreateTypeFromJObject((JObject)value, propertyName), + JTokenType.String => typeof(string), + JTokenType.TimeSpan => typeof(TimeSpan), + JTokenType.Uri => typeof(string), + _ => typeof(object) + }; + } - if (string.IsNullOrWhiteSpace(strInput)) + var properties = new Dictionary(); + foreach (var item in instance.Properties()) + { + properties.Add(item.Name, ConvertType(item.Value, item.Name)); + } + + return TypeBuilderUtils.BuildType(properties, fullName) ?? throw new InvalidOperationException(); + } + + public static bool TryParseAsJObject(string? strInput, [NotNullWhen(true)] out JObject? value) + { + value = null; + + if (strInput == null || string.IsNullOrWhiteSpace(strInput)) { return false; } @@ -30,7 +61,7 @@ public static bool TryParseAsComplexObject(string strInput, [NotNullWhen(true)] try { // Try to convert this string into a JToken - token = JToken.Parse(strInput); + value = JObject.Parse(strInput); return true; } catch @@ -105,17 +136,19 @@ public static string GenerateDynamicLinqStatement(JToken jsonObject) private static void WalkNode(JToken node, string? path, string? propertyName, List lines) { - if (node.Type == JTokenType.Object) - { - ProcessObject(node, propertyName, lines); - } - else if (node.Type == JTokenType.Array) - { - ProcessArray(node, propertyName, lines); - } - else + switch (node.Type) { - ProcessItem(node, path ?? "it", propertyName, lines); + case JTokenType.Object: + ProcessObject(node, propertyName, lines); + break; + + case JTokenType.Array: + ProcessArray(node, propertyName, lines); + break; + + default: + ProcessItem(node, path ?? "it", propertyName, lines); + break; } } @@ -125,7 +158,7 @@ private static void ProcessObject(JToken node, string? propertyName, List().ToArray()) + foreach (var child in node.Children().ToArray()) { WalkNode(child.Value, child.Path, child.Name, items); } @@ -147,8 +180,8 @@ private static void ProcessArray(JToken node, string? propertyName, List var text = new StringBuilder("(new [] { "); // In case of Array, loop all items. Do a ToArray() to avoid `Collection was modified` exceptions. - int idx = 0; - foreach (JToken child in node.Children().ToArray()) + var idx = 0; + foreach (var child in node.Children().ToArray()) { WalkNode(child, $"{node.Path}[{idx}]", null, items); idx++; @@ -165,50 +198,21 @@ private static void ProcessArray(JToken node, string? propertyName, List lines.Add(text.ToString()); } - private static void ProcessItem(JToken node, string path, string propertyName, List lines) + private static void ProcessItem(JToken node, string path, string? propertyName, List lines) { - string castText; - switch (node.Type) + var castText = node.Type switch { - case JTokenType.Boolean: - castText = $"bool({path})"; - break; - - case JTokenType.Date: - castText = $"DateTime({path})"; - break; - - case JTokenType.Float: - castText = $"double({path})"; - break; - - case JTokenType.Guid: - castText = $"Guid({path})"; - break; - - case JTokenType.Integer: - castText = $"long({path})"; - break; - - case JTokenType.Null: - castText = "null"; - break; - - case JTokenType.String: - castText = $"string({path})"; - break; - - case JTokenType.TimeSpan: - castText = $"TimeSpan({path})"; - break; - - case JTokenType.Uri: - castText = $"Uri({path})"; - break; - - default: - throw new NotSupportedException($"JTokenType '{node.Type}' cannot be converted to a Dynamic Linq cast operator."); - } + JTokenType.Boolean => $"bool({path})", + JTokenType.Date => $"DateTime({path})", + JTokenType.Float => $"double({path})", + JTokenType.Guid => $"Guid({path})", + JTokenType.Integer => $"long({path})", + JTokenType.Null => "null", + JTokenType.String => $"string({path})", + JTokenType.TimeSpan => $"TimeSpan({path})", + JTokenType.Uri => $"Uri({path})", + _ => throw new NotSupportedException($"JTokenType '{node.Type}' cannot be converted to a Dynamic Linq cast operator.") + }; if (!string.IsNullOrEmpty(propertyName)) { diff --git a/src/WireMock.Net/Util/SystemUtils.cs b/src/WireMock.Net/Util/SystemUtils.cs new file mode 100644 index 000000000..85182d9f1 --- /dev/null +++ b/src/WireMock.Net/Util/SystemUtils.cs @@ -0,0 +1,8 @@ +using System.Reflection; + +namespace WireMock.Util; + +internal static class SystemUtils +{ + public static readonly string Version = typeof(SystemUtils).GetTypeInfo().Assembly.GetName().Version.ToString(); +} \ No newline at end of file diff --git a/src/WireMock.Net/Util/TypeBuilderUtils.cs b/src/WireMock.Net/Util/TypeBuilderUtils.cs new file mode 100644 index 000000000..30fe9fbf2 --- /dev/null +++ b/src/WireMock.Net/Util/TypeBuilderUtils.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Reflection.Emit; + +namespace WireMock.Util; + +/// +/// Code based on https://stackoverflow.com/questions/40507909/convert-jobject-to-anonymous-object +/// +internal static class TypeBuilderUtils +{ + private static readonly ConcurrentDictionary, Type> Types = new(); + + private static readonly ModuleBuilder ModuleBuilder = + AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("WireMock.Net.Reflection"), AssemblyBuilderAccess.Run) + .DefineDynamicModule("WireMock.Net.Reflection.Module"); + + public static Type BuildType(IDictionary properties, string? name = null) + { + var keyExists = Types.Keys.FirstOrDefault(k => Compare(k, properties)); + if (keyExists != null) + { + return Types[keyExists]; + } + + var typeBuilder = GetTypeBuilder(name ?? Guid.NewGuid().ToString()); + foreach (var property in properties) + { + CreateGetSetMethods(typeBuilder, property.Key, property.Value); + } + + var type = typeBuilder.CreateTypeInfo().AsType(); + + Types.TryAdd(properties, type); + + return type; + } + + /// + /// https://stackoverflow.com/questions/3804367/testing-for-equality-between-dictionaries-in-c-sharp + /// + private static bool Compare(IDictionary dict1, IDictionary dict2) + { + if (dict1 == dict2) + { + return true; + } + + if (dict1.Count != dict2.Count) + { + return false; + } + + var valueComparer = EqualityComparer.Default; + + foreach (var kvp in dict1) + { + if (!dict2.TryGetValue(kvp.Key, out var value2)) + { + return false; + } + + if (!valueComparer.Equals(kvp.Value, value2)) + { + return false; + } + } + + return true; + } + + private static TypeBuilder GetTypeBuilder(string name) + { + return ModuleBuilder.DefineType(name, + TypeAttributes.Public | + TypeAttributes.Class | + TypeAttributes.AutoClass | + TypeAttributes.AnsiClass | + TypeAttributes.BeforeFieldInit | + TypeAttributes.AutoLayout, + null); + } + + private static void CreateGetSetMethods(TypeBuilder typeBuilder, string propertyName, Type propertyType) + { + var fieldBuilder = typeBuilder.DefineField("_" + propertyName, propertyType, FieldAttributes.Private); + + var propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null); + + var getPropertyMethodBuilder = typeBuilder.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType, Type.EmptyTypes); + var getIl = getPropertyMethodBuilder.GetILGenerator(); + + getIl.Emit(OpCodes.Ldarg_0); + getIl.Emit(OpCodes.Ldfld, fieldBuilder); + getIl.Emit(OpCodes.Ret); + + var setPropertyMethodBuilder = typeBuilder.DefineMethod("set_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, null, new[] { propertyType }); + var setIl = setPropertyMethodBuilder.GetILGenerator(); + var modifyProperty = setIl.DefineLabel(); + + var exitSet = setIl.DefineLabel(); + + setIl.MarkLabel(modifyProperty); + setIl.Emit(OpCodes.Ldarg_0); + setIl.Emit(OpCodes.Ldarg_1); + setIl.Emit(OpCodes.Stfld, fieldBuilder); + + setIl.Emit(OpCodes.Nop); + setIl.MarkLabel(exitSet); + setIl.Emit(OpCodes.Ret); + + propertyBuilder.SetGetMethod(getPropertyMethodBuilder); + propertyBuilder.SetSetMethod(setPropertyMethodBuilder); + } +} \ No newline at end of file diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj index 29a93bd6b..c114d79e1 100644 --- a/src/WireMock.Net/WireMock.Net.csproj +++ b/src/WireMock.Net/WireMock.Net.csproj @@ -58,11 +58,12 @@ - + + + - diff --git a/test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs b/test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs index deb31564d..9dc597801 100644 --- a/test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs +++ b/test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs @@ -12,277 +12,318 @@ using Xunit; using static System.Environment; -namespace WireMock.Net.Tests.FluentAssertions +namespace WireMock.Net.Tests.FluentAssertions; + +public class WireMockAssertionsTests : IDisposable { - public class WireMockAssertionsTests : IDisposable + private readonly WireMockServer _server; + private readonly HttpClient _httpClient; + private readonly int _portUsed; + + public WireMockAssertionsTests() + { + _server = WireMockServer.Start(); + _server.Given(Request.Create().UsingAnyMethod()) + .RespondWith(Response.Create().WithSuccess()); + _portUsed = _server.Ports.First(); + + _httpClient = new HttpClient { BaseAddress = new Uri(_server.Urls[0]) }; + } + + [Fact] + public async Task HaveReceivedNoCalls_AtAbsoluteUrl_WhenACallWasNotMadeToAbsoluteUrl_Should_BeOK() + { + await _httpClient.GetAsync("xxx").ConfigureAwait(false); + + _server.Should() + .HaveReceivedNoCalls() + .AtAbsoluteUrl($"http://localhost:{_portUsed}/anyurl"); + } + + [Fact] + public async Task HaveReceived0Calls_AtAbsoluteUrl_WhenACallWasNotMadeToAbsoluteUrl_Should_BeOK() + { + await _httpClient.GetAsync("xxx").ConfigureAwait(false); + + _server.Should() + .HaveReceived(0).Calls() + .AtAbsoluteUrl($"http://localhost:{_portUsed}/anyurl"); + } + + [Fact] + public async Task HaveReceived1Calls_AtAbsoluteUrl_WhenACallWasMadeToAbsoluteUrl_Should_BeOK() + { + await _httpClient.GetAsync("anyurl").ConfigureAwait(false); + + _server.Should() + .HaveReceived(1).Calls() + .AtAbsoluteUrl($"http://localhost:{_portUsed}/anyurl"); + } + + [Fact] + public async Task HaveReceived2Calls_AtAbsoluteUrl_WhenACallWasMadeToAbsoluteUrl_Should_BeOK() + { + await _httpClient.GetAsync("anyurl").ConfigureAwait(false); + + await _httpClient.GetAsync("anyurl").ConfigureAwait(false); + + _server.Should() + .HaveReceived(2).Calls() + .AtAbsoluteUrl($"http://localhost:{_portUsed}/anyurl"); + } + + [Fact] + public async Task HaveReceivedACall_AtAbsoluteUrl_WhenACallWasMadeToAbsoluteUrl_Should_BeOK() + { + await _httpClient.GetAsync("anyurl").ConfigureAwait(false); + + _server.Should() + .HaveReceivedACall() + .AtAbsoluteUrl($"http://localhost:{_portUsed}/anyurl"); + } + + [Fact] + public void HaveReceivedACall_AtAbsoluteUrl_Should_ThrowWhenNoCallsWereMade() + { + Action act = () => _server.Should() + .HaveReceivedACall() + .AtAbsoluteUrl("anyurl"); + + act.Should().Throw() + .And.Message.Should() + .Be( + "Expected _server to have been called at address matching the absolute url \"anyurl\", but no calls were made."); + } + + [Fact] + public async Task HaveReceivedACall_AtAbsoluteUrl_Should_ThrowWhenNoCallsMatchingTheAbsoluteUrlWereMade() + { + await _httpClient.GetAsync("").ConfigureAwait(false); + + Action act = () => _server.Should() + .HaveReceivedACall() + .AtAbsoluteUrl("anyurl"); + + act.Should().Throw() + .And.Message.Should() + .Be( + $"Expected _server to have been called at address matching the absolute url \"anyurl\", but didn't find it among the calls to {{\"http://localhost:{_portUsed}/\"}}."); + } + + [Fact] + public async Task HaveReceivedACall_WithHeader_WhenACallWasMadeWithExpectedHeader_Should_BeOK() + { + _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer a"); + await _httpClient.GetAsync("").ConfigureAwait(false); + + _server.Should() + .HaveReceivedACall() + .WithHeader("Authorization", "Bearer a"); + } + + [Fact] + public async Task HaveReceivedACall_WithHeader_WhenACallWasMadeWithExpectedHeaderAmongMultipleHeaderValues_Should_BeOK() + { + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + await _httpClient.GetAsync("").ConfigureAwait(false); + + _server.Should() + .HaveReceivedACall() + .WithHeader("Accept", new[] { "application/xml", "application/json" }); + } + + [Fact] + public async Task HaveReceivedACall_WithHeader_Should_ThrowWhenNoCallsMatchingTheHeaderNameWereMade() + { + await _httpClient.GetAsync("").ConfigureAwait(false); + + Action act = () => _server.Should() + .HaveReceivedACall() + .WithHeader("Authorization", "value"); + + act.Should().Throw() + .And.Message.Should() + .Contain("to contain key \"Authorization\"."); + } + + [Fact] + public async Task HaveReceivedACall_WithHeader_Should_ThrowWhenNoCallsMatchingTheHeaderValuesWereMade() + { + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + await _httpClient.GetAsync("").ConfigureAwait(false); + + Action act = () => _server.Should() + .HaveReceivedACall() + .WithHeader("Accept", "missing-value"); + + var sentHeaders = _server.LogEntries.SelectMany(x => x.RequestMessage.Headers) + .ToDictionary(x => x.Key, x => x.Value)["Accept"] + .Select(x => $"\"{x}\"") + .ToList(); + + var sentHeaderString = "{" + string.Join(", ", sentHeaders) + "}"; + + act.Should().Throw() + .And.Message.Should() + .Be( + $"Expected header \"Accept\" from requests sent with value(s) {sentHeaderString} to contain \"missing-value\".{NewLine}"); + } + + [Fact] + public async Task HaveReceivedACall_WithHeader_Should_ThrowWhenNoCallsMatchingTheHeaderWithMultipleValuesWereMade() + { + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + await _httpClient.GetAsync("").ConfigureAwait(false); + + Action act = () => _server.Should() + .HaveReceivedACall() + .WithHeader("Accept", new[] { "missing-value1", "missing-value2" }); + + const string missingValue1Message = + "Expected header \"Accept\" from requests sent with value(s) {\"application/xml\", \"application/json\"} to contain \"missing-value1\"."; + const string missingValue2Message = + "Expected header \"Accept\" from requests sent with value(s) {\"application/xml\", \"application/json\"} to contain \"missing-value2\"."; + + act.Should().Throw() + .And.Message.Should() + .Be($"{string.Join(NewLine, missingValue1Message, missingValue2Message)}{NewLine}"); + } + + [Fact] + public async Task HaveReceivedACall_AtUrl_WhenACallWasMadeToUrl_Should_BeOK() + { + await _httpClient.GetAsync("anyurl").ConfigureAwait(false); + + _server.Should() + .HaveReceivedACall() + .AtUrl($"http://localhost:{_portUsed}/anyurl"); + } + + [Fact] + public void HaveReceivedACall_AtUrl_Should_ThrowWhenNoCallsWereMade() + { + Action act = () => _server.Should() + .HaveReceivedACall() + .AtUrl("anyurl"); + + act.Should().Throw() + .And.Message.Should() + .Be( + "Expected _server to have been called at address matching the url \"anyurl\", but no calls were made."); + } + + [Fact] + public async Task HaveReceivedACall_AtUrl_Should_ThrowWhenNoCallsMatchingTheUrlWereMade() + { + await _httpClient.GetAsync("").ConfigureAwait(false); + + Action act = () => _server.Should() + .HaveReceivedACall() + .AtUrl("anyurl"); + + act.Should().Throw() + .And.Message.Should() + .Be( + $"Expected _server to have been called at address matching the url \"anyurl\", but didn't find it among the calls to {{\"http://localhost:{_portUsed}/\"}}."); + } + + [Fact] + public async Task HaveReceivedACall_WithProxyUrl_WhenACallWasMadeWithProxyUrl_Should_BeOK() + { + _server.ResetMappings(); + _server.Given(Request.Create().UsingAnyMethod()) + .RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999" })); + + await _httpClient.GetAsync("").ConfigureAwait(false); + + _server.Should() + .HaveReceivedACall() + .WithProxyUrl($"http://localhost:9999"); + } + + [Fact] + public void HaveReceivedACall_WithProxyUrl_Should_ThrowWhenNoCallsWereMade() + { + _server.ResetMappings(); + _server.Given(Request.Create().UsingAnyMethod()) + .RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999" })); + + Action act = () => _server.Should() + .HaveReceivedACall() + .WithProxyUrl("anyurl"); + + act.Should().Throw() + .And.Message.Should() + .Be( + "Expected _server to have been called with proxy url \"anyurl\", but no calls were made."); + } + + [Fact] + public async Task HaveReceivedACall_WithProxyUrl_Should_ThrowWhenNoCallsWithTheProxyUrlWereMade() + { + _server.ResetMappings(); + _server.Given(Request.Create().UsingAnyMethod()) + .RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999" })); + + await _httpClient.GetAsync("").ConfigureAwait(false); + + Action act = () => _server.Should() + .HaveReceivedACall() + .WithProxyUrl("anyurl"); + + act.Should().Throw() + .And.Message.Should() + .Be( + $"Expected _server to have been called with proxy url \"anyurl\", but didn't find it among the calls with {{\"http://localhost:9999\"}}."); + } + + [Fact] + public async Task HaveReceivedACall_FromClientIP_whenACallWasMadeFromClientIP_Should_BeOK() + { + await _httpClient.GetAsync("").ConfigureAwait(false); + var clientIP = _server.LogEntries.Last().RequestMessage.ClientIP; + + _server.Should() + .HaveReceivedACall() + .FromClientIP(clientIP); + } + + [Fact] + public void HaveReceivedACall_FromClientIP_Should_ThrowWhenNoCallsWereMade() + { + Action act = () => _server.Should() + .HaveReceivedACall() + .FromClientIP("different-ip"); + + act.Should().Throw() + .And.Message.Should() + .Be( + "Expected _server to have been called from client IP \"different-ip\", but no calls were made."); + } + + [Fact] + public async Task HaveReceivedACall_FromClientIP_Should_ThrowWhenNoCallsFromClientIPWereMade() + { + await _httpClient.GetAsync("").ConfigureAwait(false); + var clientIP = _server.LogEntries.Last().RequestMessage.ClientIP; + + Action act = () => _server.Should() + .HaveReceivedACall() + .FromClientIP("different-ip"); + + act.Should().Throw() + .And.Message.Should() + .Be( + $"Expected _server to have been called from client IP \"different-ip\", but didn't find it among the calls from IP(s) {{\"{clientIP}\"}}."); + } + + public void Dispose() { - private readonly WireMockServer _server; - private readonly HttpClient _httpClient; - private readonly int _portUsed; - - public WireMockAssertionsTests() - { - _server = WireMockServer.Start(); - _server.Given(Request.Create().UsingAnyMethod()) - .RespondWith(Response.Create().WithSuccess()); - _portUsed = _server.Ports.First(); - - _httpClient = new HttpClient { BaseAddress = new Uri(_server.Urls[0]) }; - } - - [Fact] - public async Task AtAbsoluteUrl_WhenACallWasMadeToAbsoluteUrl_Should_BeOK() - { - await _httpClient.GetAsync("anyurl").ConfigureAwait(false); - - _server.Should() - .HaveReceivedACall() - .AtAbsoluteUrl($"http://localhost:{_portUsed}/anyurl"); - } - - [Fact] - public void AtAbsoluteUrl_Should_ThrowWhenNoCallsWereMade() - { - Action act = () => _server.Should() - .HaveReceivedACall() - .AtAbsoluteUrl("anyurl"); - - act.Should().Throw() - .And.Message.Should() - .Be( - "Expected _server to have been called at address matching the absolute url \"anyurl\", but no calls were made."); - } - - [Fact] - public async Task AtAbsoluteUrl_Should_ThrowWhenNoCallsMatchingTheAbsoluteUrlWereMade() - { - await _httpClient.GetAsync("").ConfigureAwait(false); - - Action act = () => _server.Should() - .HaveReceivedACall() - .AtAbsoluteUrl("anyurl"); - - act.Should().Throw() - .And.Message.Should() - .Be( - $"Expected _server to have been called at address matching the absolute url \"anyurl\", but didn't find it among the calls to {{\"http://localhost:{_portUsed}/\"}}."); - } - - [Fact] - public async Task WithHeader_WhenACallWasMadeWithExpectedHeader_Should_BeOK() - { - _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer a"); - await _httpClient.GetAsync("").ConfigureAwait(false); - - _server.Should() - .HaveReceivedACall() - .WithHeader("Authorization", "Bearer a"); - } - - [Fact] - public async Task WithHeader_WhenACallWasMadeWithExpectedHeaderAmongMultipleHeaderValues_Should_BeOK() - { - _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); - _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - await _httpClient.GetAsync("").ConfigureAwait(false); - - _server.Should() - .HaveReceivedACall() - .WithHeader("Accept", new[] { "application/xml", "application/json" }); - } - - [Fact] - public async Task WithHeader_Should_ThrowWhenNoCallsMatchingTheHeaderNameWereMade() - { - await _httpClient.GetAsync("").ConfigureAwait(false); - - Action act = () => _server.Should() - .HaveReceivedACall() - .WithHeader("Authorization", "value"); - - act.Should().Throw() - .And.Message.Should() - .Contain("to contain key \"Authorization\"."); - } - - [Fact] - public async Task WithHeader_Should_ThrowWhenNoCallsMatchingTheHeaderValuesWereMade() - { - _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); - _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - await _httpClient.GetAsync("").ConfigureAwait(false); - - Action act = () => _server.Should() - .HaveReceivedACall() - .WithHeader("Accept", "missing-value"); - - var sentHeaders = _server.LogEntries.SelectMany(x => x.RequestMessage.Headers) - .ToDictionary(x => x.Key, x => x.Value)["Accept"] - .Select(x => $"\"{x}\"") - .ToList(); - - var sentHeaderString = "{" + string.Join(", ", sentHeaders) + "}"; - - act.Should().Throw() - .And.Message.Should() - .Be( - $"Expected header \"Accept\" from requests sent with value(s) {sentHeaderString} to contain \"missing-value\".{NewLine}"); - } - - [Fact] - public async Task WithHeader_Should_ThrowWhenNoCallsMatchingTheHeaderWithMultipleValuesWereMade() - { - _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); - _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - await _httpClient.GetAsync("").ConfigureAwait(false); - - Action act = () => _server.Should() - .HaveReceivedACall() - .WithHeader("Accept", new[] { "missing-value1", "missing-value2" }); - - const string missingValue1Message = - "Expected header \"Accept\" from requests sent with value(s) {\"application/xml\", \"application/json\"} to contain \"missing-value1\"."; - const string missingValue2Message = - "Expected header \"Accept\" from requests sent with value(s) {\"application/xml\", \"application/json\"} to contain \"missing-value2\"."; - - act.Should().Throw() - .And.Message.Should() - .Be($"{string.Join(NewLine, missingValue1Message, missingValue2Message)}{NewLine}"); - } - - [Fact] - public async Task AtUrl_WhenACallWasMadeToUrl_Should_BeOK() - { - await _httpClient.GetAsync("anyurl").ConfigureAwait(false); - - _server.Should() - .HaveReceivedACall() - .AtUrl($"http://localhost:{_portUsed}/anyurl"); - } - - [Fact] - public void AtUrl_Should_ThrowWhenNoCallsWereMade() - { - Action act = () => _server.Should() - .HaveReceivedACall() - .AtUrl("anyurl"); - - act.Should().Throw() - .And.Message.Should() - .Be( - "Expected _server to have been called at address matching the url \"anyurl\", but no calls were made."); - } - - [Fact] - public async Task AtUrl_Should_ThrowWhenNoCallsMatchingTheUrlWereMade() - { - await _httpClient.GetAsync("").ConfigureAwait(false); - - Action act = () => _server.Should() - .HaveReceivedACall() - .AtUrl("anyurl"); - - act.Should().Throw() - .And.Message.Should() - .Be( - $"Expected _server to have been called at address matching the url \"anyurl\", but didn't find it among the calls to {{\"http://localhost:{_portUsed}/\"}}."); - } - - [Fact] - public async Task WithProxyUrl_WhenACallWasMadeWithProxyUrl_Should_BeOK() - { - _server.ResetMappings(); - _server.Given(Request.Create().UsingAnyMethod()) - .RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999" })); - - await _httpClient.GetAsync("").ConfigureAwait(false); - - _server.Should() - .HaveReceivedACall() - .WithProxyUrl($"http://localhost:9999"); - } - - [Fact] - public void WithProxyUrl_Should_ThrowWhenNoCallsWereMade() - { - _server.ResetMappings(); - _server.Given(Request.Create().UsingAnyMethod()) - .RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999" })); - - Action act = () => _server.Should() - .HaveReceivedACall() - .WithProxyUrl("anyurl"); - - act.Should().Throw() - .And.Message.Should() - .Be( - "Expected _server to have been called with proxy url \"anyurl\", but no calls were made."); - } - - [Fact] - public async Task WithProxyUrl_Should_ThrowWhenNoCallsWithTheProxyUrlWereMade() - { - _server.ResetMappings(); - _server.Given(Request.Create().UsingAnyMethod()) - .RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999" })); - - await _httpClient.GetAsync("").ConfigureAwait(false); - - Action act = () => _server.Should() - .HaveReceivedACall() - .WithProxyUrl("anyurl"); - - act.Should().Throw() - .And.Message.Should() - .Be( - $"Expected _server to have been called with proxy url \"anyurl\", but didn't find it among the calls with {{\"http://localhost:9999\"}}."); - } - - [Fact] - public async Task FromClientIP_whenACallWasMadeFromClientIP_Should_BeOK() - { - await _httpClient.GetAsync("").ConfigureAwait(false); - var clientIP = _server.LogEntries.Last().RequestMessage.ClientIP; - - _server.Should() - .HaveReceivedACall() - .FromClientIP(clientIP); - } - - [Fact] - public void FromClientIP_Should_ThrowWhenNoCallsWereMade() - { - Action act = () => _server.Should() - .HaveReceivedACall() - .FromClientIP("different-ip"); - - act.Should().Throw() - .And.Message.Should() - .Be( - "Expected _server to have been called from client IP \"different-ip\", but no calls were made."); - } - - [Fact] - public async Task FromClientIP_Should_ThrowWhenNoCallsFromClientIPWereMade() - { - await _httpClient.GetAsync("").ConfigureAwait(false); - var clientIP = _server.LogEntries.Last().RequestMessage.ClientIP; - - Action act = () => _server.Should() - .HaveReceivedACall() - .FromClientIP("different-ip"); - - act.Should().Throw() - .And.Message.Should() - .Be( - $"Expected _server to have been called from client IP \"different-ip\", but didn't find it among the calls from IP(s) {{\"{clientIP}\"}}."); - } - - public void Dispose() - { - _server?.Stop(); - _server?.Dispose(); - _httpClient?.Dispose(); - } + _server?.Stop(); + _server?.Dispose(); + _httpClient?.Dispose(); } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Pact/PactTests.cs b/test/WireMock.Net.Tests/Pact/PactTests.cs index 5cce094c1..70234ed5a 100644 --- a/test/WireMock.Net.Tests/Pact/PactTests.cs +++ b/test/WireMock.Net.Tests/Pact/PactTests.cs @@ -57,6 +57,7 @@ public void SavePact_Multiple_Requests() .WithHeader("Accept", "application/json") ) .WithTitle("A GET request to retrieve the something") + .WithGuid("23e2aedb-166c-467b-b9f6-9b0817cb1636") .RespondWith( Response.Create() .WithStatusCode(HttpStatusCode.OK) @@ -77,6 +78,7 @@ public void SavePact_Multiple_Requests() .WithBody(new JsonMatcher("{ \"Id\" : \"1\", \"FirstName\" : \"Totally\" }")) ) .WithTitle("A Post request to add the something") + .WithGuid("f3f8abe7-7d1e-4518-afa1-d295ce7dadfd") .RespondWith( Response.Create() .WithStatusCode(HttpStatusCode.RedirectMethod) diff --git a/test/WireMock.Net.Tests/Pact/files/pact-get.json b/test/WireMock.Net.Tests/Pact/files/pact-get.json index e4099eb36..61a617f85 100644 --- a/test/WireMock.Net.Tests/Pact/files/pact-get.json +++ b/test/WireMock.Net.Tests/Pact/files/pact-get.json @@ -1,32 +1,32 @@ { - "Consumer": { - "Name": "Something API Consumer Get" + "consumer": { + "name": "Something API Consumer Get" }, - "Interactions": [ + "interactions": [ { - "ProviderState": "A GET request to retrieve the something", - "Request": { - "Headers": { + "providerState": "A GET request to retrieve the something", + "request": { + "headers": { "Accept": "application/json" }, - "Method": "GET", - "Path": "/tester", - "Query": "q1=test&q2=ok" + "method": "GET", + "path": "/tester", + "query": "q1=test&q2=ok" }, - "Response": { - "Body": { - "Id": "tester", - "FirstName": "Totally", - "LastName": "Awesome" + "response": { + "body": { + "id": "tester", + "firstName": "Totally", + "lastName": "Awesome" }, - "Headers": { + "headers": { "Content-Type": "application/json; charset=utf-8" }, - "Status": 200 + "status": 200 } } ], - "Provider": { - "Name": "Something API" + "provider": { + "name": "Something API" } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Pact/files/pact-multiple.json b/test/WireMock.Net.Tests/Pact/files/pact-multiple.json index ddf7d5945..e2231f5d8 100644 --- a/test/WireMock.Net.Tests/Pact/files/pact-multiple.json +++ b/test/WireMock.Net.Tests/Pact/files/pact-multiple.json @@ -1,50 +1,49 @@ { - "Consumer": { - "Name": "Something API Consumer Multiple" + "consumer": { + "name": "Something API Consumer Multiple" }, - "Interactions": [ + "interactions": [ { - "ProviderState": "A Post request to add the something", - "Request": { - "Headers": { + "providerState": "A GET request to retrieve the something", + "request": { + "headers": { "Accept": "application/json" }, - "Method": "POST", - "Path": "/add", - "Body": "{ \"Id\" : \"1\", \"FirstName\" : \"Totally\" }" + "method": "POST", + "path": "/tester", + "query": "q1=test&q2=ok" }, - "Response": { - "Body": { - "Id": "1", - "FirstName": "Totally" + "response": { + "body": { + "id": "tester", + "firstName": "Totally", + "lastName": "Awesome" }, - "Status": 303 + "headers": { + "Content-Type": "application/json; charset=utf-8" + }, + "status": 200 } }, { - "ProviderState": "A GET request to retrieve the something", - "Request": { - "Headers": { + "providerState": "A Post request to add the something", + "request": { + "headers": { "Accept": "application/json" }, - "Method": "POST", - "Path": "/tester", - "Query": "q1=test&q2=ok" + "method": "POST", + "path": "/add" }, - "Response": { - "Body": { - "Id": "tester", - "FirstName": "Totally", - "LastName": "Awesome" - }, - "Headers": { - "Content-Type": "application/json; charset=utf-8" + "response": { + "body": { + "id": "1", + "firstName": "Totally" }, - "Status": 200 + "status": 303 } } ], - "Provider": { - "Name": "Something API" + "provider": { + "name": "Something API" } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Util/JsonUtilsTests.cs b/test/WireMock.Net.Tests/Util/JsonUtilsTests.cs index a1ade3093..f34334964 100644 --- a/test/WireMock.Net.Tests/Util/JsonUtilsTests.cs +++ b/test/WireMock.Net.Tests/Util/JsonUtilsTests.cs @@ -1,115 +1,167 @@ using System; using System.Linq; using System.Linq.Dynamic.Core; +using System.Reflection; using FluentAssertions; using Newtonsoft.Json.Linq; using NFluent; using WireMock.Util; using Xunit; -namespace WireMock.Net.Tests.Util +namespace WireMock.Net.Tests.Util; + +public class JsonUtilsTests { - public class JsonUtilsTests + [Fact] + public void JsonUtils_ParseJTokenToObject() { - [Fact] - public void JsonUtils_ParseJTokenToObject() - { - // Assign - object value = "test"; + // Assign + object value = "test"; - // Act - string result = JsonUtils.ParseJTokenToObject(value); + // Act + string result = JsonUtils.ParseJTokenToObject(value); - // Assert - Check.That(result).IsEqualTo(default(string)); - } + // Assert + Check.That(result).IsEqualTo(default(string)); + } - [Fact] - public void JsonUtils_GenerateDynamicLinqStatement_JToken() - { - // Assign - JToken j = "Test"; + [Fact] + public void JsonUtils_GenerateDynamicLinqStatement_JToken() + { + // Assign + JToken instance = "Test"; - // Act - string line = JsonUtils.GenerateDynamicLinqStatement(j); + // Act + string line = JsonUtils.GenerateDynamicLinqStatement(instance); - // Assert - var queryable = new[] { j }.AsQueryable().Select(line); - bool result = queryable.Any("it == \"Test\""); - Check.That(result).IsTrue(); + // Assert + var queryable = new[] { instance }.AsQueryable().Select(line); + bool result = queryable.Any("it == \"Test\""); + Check.That(result).IsTrue(); - Check.That(line).IsEqualTo("string(it)"); - } + Check.That(line).IsEqualTo("string(it)"); + } - [Fact] - public void JsonUtils_GenerateDynamicLinqStatement_JArray_Indexer() + [Fact] + public void JsonUtils_GenerateDynamicLinqStatement_JArray_Indexer() + { + // Assign + var instance = new JObject { - // Assign - var j = new JObject - { - { "Items", new JArray(new[] { new JValue(4), new JValue(8) }) } - }; + { "Items", new JArray(new JValue(4), new JValue(8)) } + }; - // Act - string line = JsonUtils.GenerateDynamicLinqStatement(j); + // Act + string line = JsonUtils.GenerateDynamicLinqStatement(instance); - // Assert 1 - line.Should().Be("new ((new [] { long(Items[0]), long(Items[1])}) as Items)"); + // Assert 1 + line.Should().Be("new ((new [] { long(Items[0]), long(Items[1])}) as Items)"); - // Assert 2 - var queryable = new[] { j }.AsQueryable().Select(line); - bool result = queryable.Any("Items != null"); - result.Should().BeTrue(); - } + // Assert 2 + var queryable = new[] { instance }.AsQueryable().Select(line); + bool result = queryable.Any("Items != null"); + result.Should().BeTrue(); + } - [Fact] - public void JsonUtils_GenerateDynamicLinqStatement_JObject2() + [Fact] + public void JsonUtils_GenerateDynamicLinqStatement_JObject2() + { + // Assign + var instance = new JObject { - // Assign - var j = new JObject + {"U", new JValue(new Uri("http://localhost:80/abc?a=5"))}, + {"N", new JValue((object?) null)}, + {"G", new JValue(Guid.NewGuid())}, + {"Flt", new JValue(10.0f)}, + {"Dbl", new JValue(Math.PI)}, + {"Check", new JValue(true)}, { - {"U", new JValue(new Uri("http://localhost:80/abc?a=5"))}, - {"N", new JValue((object) null)}, - {"G", new JValue(Guid.NewGuid())}, - {"Flt", new JValue(10.0f)}, - {"Dbl", new JValue(Math.PI)}, - {"Check", new JValue(true)}, + "Child", new JObject { - "Child", new JObject - { - {"ChildId", new JValue(4)}, - {"ChildDateTime", new JValue(new DateTime(2018, 2, 17))}, - {"TS", new JValue(TimeSpan.FromMilliseconds(999))} - } - }, - {"I", new JValue(9)}, - {"L", new JValue(long.MaxValue)}, - {"Name", new JValue("Test")} - }; - - // Act - string line = JsonUtils.GenerateDynamicLinqStatement(j); - - // Assert 1 - line.Should().Be("new (Uri(U) as U, null as N, Guid(G) as G, double(Flt) as Flt, double(Dbl) as Dbl, bool(Check) as Check, new (long(Child.ChildId) as ChildId, DateTime(Child.ChildDateTime) as ChildDateTime, TimeSpan(Child.TS) as TS) as Child, long(I) as I, long(L) as L, string(Name) as Name)"); - - // Assert 2 - var queryable = new[] { j }.AsQueryable().Select(line); - bool result = queryable.Any("I > 1 && L > 1"); - result.Should().BeTrue(); - } - - [Fact] - public void JsonUtils_GenerateDynamicLinqStatement_Throws() + {"ChildId", new JValue(4)}, + {"ChildDateTime", new JValue(new DateTime(2018, 2, 17))}, + {"TS", new JValue(TimeSpan.FromMilliseconds(999))} + } + }, + {"I", new JValue(9)}, + {"L", new JValue(long.MaxValue)}, + {"Name", new JValue("Test")} + }; + + // Act + string line = JsonUtils.GenerateDynamicLinqStatement(instance); + + // Assert 1 + line.Should().Be("new (Uri(U) as U, null as N, Guid(G) as G, double(Flt) as Flt, double(Dbl) as Dbl, bool(Check) as Check, new (long(Child.ChildId) as ChildId, DateTime(Child.ChildDateTime) as ChildDateTime, TimeSpan(Child.TS) as TS) as Child, long(I) as I, long(L) as L, string(Name) as Name)"); + + // Assert 2 + var queryable = new[] { instance }.AsQueryable().Select(line); + bool result = queryable.Any("I > 1 && L > 1"); + result.Should().BeTrue(); + } + + [Fact] + public void JsonUtils_GenerateDynamicLinqStatement_Throws() + { + // Assign + var instance = new JObject { - // Assign - var j = new JObject - { - { "B", new JValue(new byte[] {48, 49}) } - }; + { "B", new JValue(new byte[] {48, 49}) } + }; + + // Act and Assert + Check.ThatCode(() => JsonUtils.GenerateDynamicLinqStatement(instance)).Throws(); + } - // Act and Assert - Check.ThatCode(() => JsonUtils.GenerateDynamicLinqStatement(j)).Throws(); - } + [Fact] + public void JsonUtils_CreateTypeFromJObject() + { + // Assign + var instance = new JObject + { + {"U", new JValue(new Uri("http://localhost:80/abc?a=5"))}, + {"N", new JValue((object?) null)}, + {"G", new JValue(Guid.NewGuid())}, + {"Flt", new JValue(10.0f)}, + {"Dbl", new JValue(Math.PI)}, + {"Check", new JValue(true)}, + { + "Child", new JObject + { + {"ChildId", new JValue(4)}, + {"ChildDateTime", new JValue(new DateTime(2018, 2, 17))}, + {"ChildTimeSpan", new JValue(TimeSpan.FromMilliseconds(999))} + } + }, + {"I", new JValue(9)}, + {"L", new JValue(long.MaxValue)}, + {"S", new JValue("Test")}, + {"C", new JValue('c')} + }; + + // Act + var type = JsonUtils.CreateTypeFromJObject(instance); + + // Assert + var setProperties = type + .GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(pi => pi.GetMethod != null).Select(pi => $"{pi.GetMethod}") + .ToArray(); + + setProperties.Should().HaveCount(11); + setProperties.Should().BeEquivalentTo(new[] + { + "System.String get_U()", + "System.Object get_N()", + "System.Guid get_G()", + "Single get_Flt()", + "Single get_Dbl()", + "Boolean get_Check()", + "Child get_Child()", + "Int64 get_I()", + "Int64 get_L()", + "System.String get_S()", + "System.String get_C()" + }); } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Util/SystemUtilsTests.cs b/test/WireMock.Net.Tests/Util/SystemUtilsTests.cs new file mode 100644 index 000000000..ba2c7cd48 --- /dev/null +++ b/test/WireMock.Net.Tests/Util/SystemUtilsTests.cs @@ -0,0 +1,14 @@ +using FluentAssertions; +using WireMock.Util; +using Xunit; + +namespace WireMock.Net.Tests.Util; + +public class SystemUtilsTests +{ + [Fact] + public void Version() + { + SystemUtils.Version.Should().NotBeEmpty(); + } +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index 82729577d..7cd64f1bb 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -83,28 +83,16 @@ PreserveNewest - + PreserveNewest - + PreserveNewest - + PreserveNewest - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - + PreserveNewest diff --git a/test/WireMock.Net.Tests/WireMockServer.Settings.cs b/test/WireMock.Net.Tests/WireMockServer.Settings.cs index d5b43cdab..07337ac43 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Settings.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Settings.cs @@ -81,7 +81,7 @@ public void WireMockServer_WireMockServerSettings_PriorityFromAllAdminMappingsIs // Assert server.Mappings.Should().NotBeNull(); - server.Mappings.Should().HaveCount(25); + server.Mappings.Should().HaveCount(26); server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue(); } @@ -100,9 +100,9 @@ public void WireMockServer_WireMockServerSettings_ProxyAndRecordSettings_ProxyPr // Assert server.Mappings.Should().NotBeNull(); - server.Mappings.Should().HaveCount(26); + server.Mappings.Should().HaveCount(27); - server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(25); + server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(26); server.Mappings.Count(m => m.Priority == WireMockConstants.ProxyPriority).Should().Be(1); }