diff --git a/Solutions/Menes.Hosting.AspNetCore/Menes/Internal/OpenApiHttpResponseResult.cs b/Solutions/Menes.Hosting.AspNetCore/Menes/Internal/OpenApiHttpResponseResult.cs index a1d8d1cd..5aaab5e1 100644 --- a/Solutions/Menes.Hosting.AspNetCore/Menes/Internal/OpenApiHttpResponseResult.cs +++ b/Solutions/Menes.Hosting.AspNetCore/Menes/Internal/OpenApiHttpResponseResult.cs @@ -315,6 +315,14 @@ private void BuildHeaders(HttpResponse httpResponse, OpenApiResponse response) convertedValue = this.ConvertValue(header.Value.Schema, value); } + if (header.Value.Schema.Type == "string") + { + // When IOpenApiConverter produce a JSON string, they always include the surround + // double quotes, because those are necessary for the result to be valid JSON. + // But when we put string values in headers, we do not include the double quotes. + convertedValue = convertedValue[1..^1]; + } + httpResponse.Headers.Add(header.Key, new Microsoft.Extensions.Primitives.StringValues(convertedValue)); if (this.logger.IsEnabled(LogLevel.Debug)) diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/BooleanOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/BooleanOutputParsing.feature index 4fc87775..c14d658a 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/BooleanOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/BooleanOutputParsing.feature @@ -6,12 +6,24 @@ Feature: Boolean Output Parsing I want to be able to specify boolean values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Body with valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'boolean', and format '' - When I try to build a response body from the value '' of type 'System.Boolean' + When I try to build a response from the value '' of type 'System.Boolean' Then the response body should be '' Examples: | Value | ExpectedResult | | true | true | | false | false | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'boolean', and format '' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.Boolean | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | true | true | + | false | false | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/ByteArrayOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/ByteArrayOutputParsing.feature index 990d0321..bb6875fe 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/ByteArrayOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/ByteArrayOutputParsing.feature @@ -6,12 +6,24 @@ Feature: Byte Array Output Parsing I want to be able to specify byte-formatted string values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'string', and format 'byte' - When I try to build a response body from the value '' of type 'ByteArrayFromBase64String' + When I try to build a response from the value '' of type 'ByteArrayFromBase64String' Then the response body should be '' Examples: | Value | ExpectedResult | | U3dhZ2dlciByb2Nrcw== | "U3dhZ2dlciByb2Nrcw==" | | | "" | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format 'byte' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | ByteArrayFromBase64String | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | U3dhZ2dlciByb2Nrcw== | U3dhZ2dlciByb2Nrcw== | + | | | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/DateOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/DateOutputParsing.feature index f778a202..e1972e9c 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/DateOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/DateOutputParsing.feature @@ -6,11 +6,18 @@ Feature: Date Output Parsing I want to be able to specify date values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'string', and format 'date' - When I try to build a response body from the value '' of type 'System.DateTimeOffset' + When I try to build a response from the value '' of type 'System.DateTimeOffset' Then the response body should be '' Examples: | Value | ExpectedResult | | 2017-07-21T00:00:00Z | "2017-07-21" | + +Scenario: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format 'date' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.DateTimeOffset | 2017-07-21T00:00:00Z | + Then the response header called 'X-Test' should be '2017-07-21' diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/DateTimeOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/DateTimeOutputParsing.feature index 845a2f6d..2a67feae 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/DateTimeOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/DateTimeOutputParsing.feature @@ -8,9 +8,16 @@ Feature: DateTime Output Parsing Scenario Outline: Valid values for simple types Given I have constructed the OpenAPI specification with a response body of type 'string', and format 'date-time' - When I try to build a response body from the value '' of type 'System.String' + When I try to build a response from the value '' of type 'System.String' Then the response body should be '' Examples: - | Value | ExpectedResult | - | 2017-07-21T00:00:00Z | "2017-07-21T00:00:00Z" | + | Value | ExpectedResult | + | 2017-07-21T00:00:00Z | "2017-07-21T00:00:00Z" | + +Scenario: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format 'date-time' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.String | 2017-07-21T00:00:00Z | + Then the response header called 'X-Test' should be '2017-07-21T00:00:00Z' diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/DoubleOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/DoubleOutputParsing.feature index ccbe8dd3..3820c121 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/DoubleOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/DoubleOutputParsing.feature @@ -6,9 +6,9 @@ Feature: Double Output Parsing I want to be able to specify double values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'number', and format 'double' - When I try to build a response body from the value '' of type 'System.Double' + When I try to build a response from the value '' of type 'System.Double' Then the response body should be '' Examples: @@ -19,4 +19,21 @@ Scenario Outline: Valid values for simple types | -1234 | -1234 | | 1234.5 | 1234.5 | | -1234.567 | -1234.567 | - | -1234.5678987 | -1234.5678987 | \ No newline at end of file + | -1234.5678987 | -1234.5678987 | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format 'double' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.Double | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | 0 | 0 | + | 0.1 | 0.1 | + | 1234 | 1234 | + | -1234 | -1234 | + | 1234.5 | 1234.5 | + | -1234.567 | -1234.567 | + | -1234.5678987 | -1234.5678987 | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/FloatOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/FloatOutputParsing.feature index dffa8175..3ea8f445 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/FloatOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/FloatOutputParsing.feature @@ -6,9 +6,9 @@ Feature: Float Output Parsing I want to be able to specify float values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'number', and format 'float' - When I try to build a response body from the value '' of type 'System.Single' + When I try to build a response from the value '' of type 'System.Single' Then the response body should be '' Examples: @@ -18,4 +18,20 @@ Scenario Outline: Valid values for simple types | 1234 | 1234 | | -1234 | -1234 | | 1234.5 | 1234.5 | - | -1234.567 | -1234.567 | \ No newline at end of file + | -1234.567 | -1234.567 | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format 'float' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.Single | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | 0 | 0 | + | 0.1 | 0.1 | + | 1234 | 1234 | + | -1234 | -1234 | + | 1234.5 | 1234.5 | + | -1234.567 | -1234.567 | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/Integer32OutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/Integer32OutputParsing.feature index 1fdecca9..3338c44b 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/Integer32OutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/Integer32OutputParsing.feature @@ -6,9 +6,9 @@ Feature: Integer32 Output Parsing I want to be able to specify integer32 values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'integer', and format '' - When I try to build a response body from the value '' of type 'System.Int32' + When I try to build a response from the value '' of type 'System.Int32' Then the response body should be '' Examples: @@ -18,3 +18,18 @@ Scenario Outline: Valid values for simple types | -1234 | -1234 | | 2147483647 | 2147483647 | | -2147483648 | -2147483648 | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'integer', and format '' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.Int32 | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | 0 | 0 | + | 1234 | 1234 | + | -1234 | -1234 | + | 2147483647 | 2147483647 | + | -2147483648 | -2147483648 | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/Integer64OutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/Integer64OutputParsing.feature index 484823cb..0613274f 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/Integer64OutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/Integer64OutputParsing.feature @@ -6,9 +6,9 @@ Feature: Integer64 Output Parsing I want to be able to specify integer64 values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'integer', and format 'int64' - When I try to build a response body from the value '' of type 'System.Int64' + When I try to build a response from the value '' of type 'System.Int64' Then the response body should be '' Examples: @@ -20,3 +20,20 @@ Scenario Outline: Valid values for simple types | -2147483648 | -2147483648 | | 9223372036854775807 | 9223372036854775807 | | -9223372036854775808 | -9223372036854775808 | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'integer', and format 'int64' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.Int64 | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | 0 | 0 | + | 1234 | 1234 | + | -1234 | -1234 | + | 2147483647 | 2147483647 | + | -2147483648 | -2147483648 | + | 9223372036854775807 | 9223372036854775807 | + | -9223372036854775808 | -9223372036854775808 | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/ObjectOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/ObjectOutputParsing.feature index e5d895ea..6835de4b 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/ObjectOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/ObjectOutputParsing.feature @@ -8,5 +8,5 @@ Feature: Oubject Output Parsing Scenario: Object with properties of simple types Given I have constructed the OpenAPI specification with a response body of type object, containing properties in the structure '{ "id": { "type": "integer" }, "name": {"type": "string"} }' - When I try to build a response body from the value '{"id":123,"name":"Ed"}' of type 'ObjectWithIdAndName' + When I try to build a response from the value '{"id":123,"name":"Ed"}' of type 'ObjectWithIdAndName' Then the response body should be '{"id":123,"name":"Ed"}' diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/PasswordOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/PasswordOutputParsing.feature index aa4a6185..58575cd2 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/PasswordOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/PasswordOutputParsing.feature @@ -6,11 +6,23 @@ Feature: Password Output Parsing I want to be able to specify password-formatted string values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'string', and format 'password' - When I try to build a response body from the value '' of type 'System.String' + When I try to build a response from the value '' of type 'System.String' Then the response body should be '' Examples: | Value | ExpectedResult | | myVErySeCurePAsSworD123 | "myVErySeCurePAsSworD123" | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format 'password' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.String | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | Foo | Foo | + | myVErySeCurePAsSworD123 | myVErySeCurePAsSworD123 | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/StringOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/StringOutputParsing.feature new file mode 100644 index 00000000..e9e03b2d --- /dev/null +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/StringOutputParsing.feature @@ -0,0 +1,31 @@ +@perScenarioContainer + +Feature: String Output Parsing + In order to implement a web API + As a developer + I want to be able to specify string values as or in response bodies or headers within the OpenAPI specification and have corresponding response bodies deserialized and validated + + +Scenario Outline: Body with valid values + Given I have constructed the OpenAPI specification with a response body of type 'string', and format '' + When I try to build a response from the value '' of type 'System.String' + Then the response body should be '' + + Examples: + | Value | ExpectedResult | + | Foo | "Foo" | + | /1234/abc | "/1234/abc" | + | | "" | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format '' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.String | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | Foo | Foo | + | /1234/abc | /1234/abc | + | | | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/UriOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/UriOutputParsing.feature index 31aef604..c84a821b 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/UriOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/UriOutputParsing.feature @@ -6,15 +6,24 @@ Feature: Uri Output Parsing I want to be able to specify uri-formatted string values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'string', and format 'uri' - When I try to build a response body from the value '' of type 'System.Uri' + When I try to build a response from the value '' of type 'System.Uri' Then the response body should be '' Examples: | Value | ExpectedResult | | https://myuri.com/ | "https://myuri.com/" | - # Note: although Menes has always supported relative URIs as inputs, it does not allow them - # as outputs. It seems likely that one of these facts is a bug, but it's not currently clear - # which. We will most likely resolve this when we move over to Corvus.JsonSchema. - #| relativeuri | "relativeuri" | + | relativeuri | "relativeuri" | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format 'uri' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.Uri | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | https://myuri.com/ | https://myuri.com/ | + | relativeuri | relativeuri | diff --git a/Solutions/Menes.Specs/Features/JsonTypeConversion/UuidOutputParsing.feature b/Solutions/Menes.Specs/Features/JsonTypeConversion/UuidOutputParsing.feature index 6379d59a..9535aa17 100644 --- a/Solutions/Menes.Specs/Features/JsonTypeConversion/UuidOutputParsing.feature +++ b/Solutions/Menes.Specs/Features/JsonTypeConversion/UuidOutputParsing.feature @@ -6,11 +6,22 @@ Feature: Uuid Output Parsing I want to be able to specify uuid-formatted string values as or in response bodies within the OpenAPI specification and have corresponding response bodies deserialized and validated -Scenario Outline: Valid values for simple types +Scenario Outline: Body with valid values Given I have constructed the OpenAPI specification with a response body of type 'string', and format 'uuid' - When I try to build a response body from the value '' of type 'System.Uri' + When I try to build a response from the value '' of type 'System.Guid' Then the response body should be '' Examples: | Value | ExpectedResult | | 9b7d63fb-1689-4697-9571-00d10b873d78 | "9b7d63fb-1689-4697-9571-00d10b873d78" | + +Scenario Outline: Header with valid values + Given I have constructed the OpenAPI specification with a response header called 'X-Test' of type 'string', and format 'uuid' + When I try to build a response from an OpenAPI result with these values + | Name | Type | Value | + | X-Test | System.Guid | | + Then the response header called 'X-Test' should be '' + + Examples: + | Value | ExpectedResult | + | 9b7d63fb-1689-4697-9571-00d10b873d78 | 9b7d63fb-1689-4697-9571-00d10b873d78 | diff --git a/Solutions/Menes.Specs/Menes.Specs.csproj b/Solutions/Menes.Specs/Menes.Specs.csproj index 0aeaecd8..df087c8c 100644 --- a/Solutions/Menes.Specs/Menes.Specs.csproj +++ b/Solutions/Menes.Specs/Menes.Specs.csproj @@ -66,14 +66,28 @@ + + + Always + + + StringOutputParsing.feature + + PreserveNewest + + + $(UsingMicrosoftNETSdk) + %(RelativeDir)%(Filename).feature$(DefaultLanguageSourceExtension) + + \ No newline at end of file diff --git a/Solutions/Menes.Specs/Steps/OpenApiParameterParsingSteps.cs b/Solutions/Menes.Specs/Steps/OpenApiParameterParsingSteps.cs index 5c35b9bd..249c06b0 100644 --- a/Solutions/Menes.Specs/Steps/OpenApiParameterParsingSteps.cs +++ b/Solutions/Menes.Specs/Steps/OpenApiParameterParsingSteps.cs @@ -27,6 +27,7 @@ namespace Menes.Specs.Steps using NUnit.Framework; using TechTalk.SpecFlow; + using TechTalk.SpecFlow.Assist; [Binding] public class OpenApiParameterParsingSteps @@ -36,6 +37,7 @@ public class OpenApiParameterParsingSteps private IDictionary? parameters; private Exception? exception; private string? responseBody; + private IHeaderDictionary? responseHeaders; public OpenApiParameterParsingSteps(ScenarioContext scenarioContext) { @@ -121,6 +123,45 @@ public void GivenIHaveConstructedTheOpenAPISpecificationWithAResponseBodyOfTypeA this.InitializeDocumentProviderAndPathMatcher(openApiSpec); } + [Given("I have constructed the OpenAPI specification with a response header called '([^']*)' of type '([^']*)', and format '([^']*)'")] + public void GivenIHaveConstructedTheOpenAPISpecificationWithAResponseHeaderOfTypeAndFormat( + string headerName, string headerType, string headerFormat) + { + string openApiSpec = $$""" + { + "openapi": "3.0.1", + "info": { + "title": "Swagger Petstore (Simple)", + "version": "1.0.0" + }, + "servers": [ { "url": "http://petstore.swagger.io/api" } ], + "paths": { + "/pets": { + "get": { + "summary": "Get a pet", + "operationId": "getPet", + "responses": { + "200": { + "description": "A pet", + "headers": { + "{{headerName}}": { + "schema": { + "type": "{{headerType}}", + "format": "{{headerFormat}}" + } + } + } + } + } + } + } + } + } + """; + + this.InitializeDocumentProviderAndPathMatcher(openApiSpec); + } + [Given("I have constructed the OpenAPI specification with a response body of type object, containing properties in the structure '([^']*)'")] public void GivenIHaveConstructedTheOpenAPISpecificationWithAResponseBodyOfTypeObjectContainingPropertiesInTheStructure( string objectProperties) @@ -1009,34 +1050,30 @@ public async Task WhenITryToParseTheCookieValueAndExpectAnErrorAsync( } } - [When("I try to build a response body from the value '([^']*)' of type '([^']*)'")] - public void WhenITryToBuildAResponseBodyFromTheValueOfTypeSystem_Boolean( + [When("I try to build a response from the value '([^']*)' of type '([^']*)'")] + public void WhenITryToBuildAResponseFromTheValueOfType( string valueAsString, string valueType) { object value = GetResultFromStringAndType(valueAsString, valueType); + this.BuildAndExecuteResult(value); + } - IEnumerable> builders = ContainerBindings.GetServiceProvider(this.scenarioContext). - GetServices>(); - - this.Matcher.FindOperationPathTemplate("/pets", "GET", out OpenApiOperationPathTemplate? operationPathTemplate); - OpenApiOperation operation = operationPathTemplate!.Operation; + [When("I try to build a response from an OpenAPI result with these values")] + public void WhenITryToBuildAResponseFromAnOpenApiResultWith(Table resultValues) + { + var openApiResult = new OpenApiResult + { + StatusCode = 200, + }; - IHttpResponseResult? result = null; - foreach (IResponseOutputBuilder builder in builders) + foreach ((string name, string type, string value) in resultValues.CreateSet<(string Name, string Type, string Value)>()) { - if (builder.CanBuildOutput(value, operation)) - { - result = builder.BuildOutput(value, operation); - break; - } + openApiResult.Results.Add( + name, + GetResultFromStringAndType(value, type)); } - var context = new DefaultHttpContext(); - context.Response.Body = new MemoryStream(); - result!.ExecuteResultAsync(context.Response); - context.Response.Body.Position = 0; - using StreamReader sr = new(context.Response.Body); - this.responseBody = sr.ReadToEnd(); + this.BuildAndExecuteResult(openApiResult); } [Then("the response body should be '([^']*)'")] @@ -1045,6 +1082,12 @@ public void ThenTheResponseBodyShouldBeTrue(string expectedBody) Assert.AreEqual(expectedBody, this.responseBody); } + [Then("the response header called '([^']*)' should be '([^']*)'")] + public void ThenTheResponseHeaderCalledShouldBe(string headerName, string expectedHeaderValue) + { + Assert.AreEqual(expectedHeaderValue, this.responseHeaders![headerName][0]); + } + [Then("the parameter (.*?) should be (.*?) of type (.*)")] public void ThenTheParameterShouldBe(string parameterName, string expectedResultAsString, string expectedType) { @@ -1093,6 +1136,33 @@ private void InitializeDocumentProviderAndPathMatcher(string openApiSpec) this.matcher = new PathMatcher(documentProvider); } + private void BuildAndExecuteResult(object value) + { + IEnumerable> builders = ContainerBindings.GetServiceProvider(this.scenarioContext). + GetServices>(); + + this.Matcher.FindOperationPathTemplate("/pets", "GET", out OpenApiOperationPathTemplate? operationPathTemplate); + OpenApiOperation operation = operationPathTemplate!.Operation; + + IHttpResponseResult? result = null; + foreach (IResponseOutputBuilder builder in builders) + { + if (builder.CanBuildOutput(value, operation)) + { + result = builder.BuildOutput(value, operation); + break; + } + } + + var context = new DefaultHttpContext(); + context.Response.Body = new MemoryStream(); + result!.ExecuteResultAsync(context.Response); + context.Response.Body.Position = 0; + using StreamReader sr = new(context.Response.Body); + this.responseBody = sr.ReadToEnd(); + this.responseHeaders = context.Response.Headers; + } + private class FakeCookieCollection : IRequestCookieCollection { private readonly Dictionary cookies = new();