Skip to content

Commit 6850758

Browse files
authored
Set the "Accept" request header to the specified type, rathern than a… (#290)
* Set the "Accept" request header to the specified type, rathern than always ApplicationJson * Add additional unit tests for checking for the expected Accept header
1 parent 956de25 commit 6850758

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/SwaggerProvider.DesignTime/v3/OperationCompiler.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ type OperationCompiler(schema: OpenApiDocument, defCompiler: DefinitionCompiler,
252252
[ if not(isNull payloadMime) then
253253
"Content-Type", payloadMime
254254
if not(isNull retMime) then
255-
"Accept", MediaTypes.ApplicationJson ]
255+
"Accept", retMime ]
256256
@>
257257

258258
// Locates parameters matching the arguments

tests/SwaggerProvider.ProviderTests/v3/Swashbuckle.ReturnTextControllers.Tests.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ let ``Return text/csv GET Test``() =
1919
[<Fact>]
2020
let ``Send & return text/plain POST Test``() =
2121
api.PostApiConsumesText("hello") |> asyncEqual "hello"
22+
23+
// Test for https://github.com/fsprojects/SwaggerProvider/pull/290 to check for expected 'Accept' header values
24+
[<Fact>]
25+
let ``Return text/plain Accept header Test``() =
26+
api.GetApiCheckAcceptsPlain() |> asyncEqual "Hello world"
27+
28+
[<Fact>]
29+
let ``Return text/csv Accept header Test``() =
30+
api.GetApiCheckAcceptsCsv() |> asyncEqual "Hello,world"

tests/Swashbuckle.WebApi.Server/Controllers/ReturnTextControllers.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ type ReturnCsvController() =
2121
member this.Get() =
2222
"Hello,world" |> ActionResult<string>
2323

24+
// CheckAcceptsPlainController and CheckAcceptsCsvController are tests for https://github.com/fsprojects/SwaggerProvider/pull/290
25+
// to test that the generated 'Accept' headers contain the expected values
26+
[<Route("api/[controller]")>]
27+
[<ApiController>]
28+
type CheckAcceptsPlainController() =
29+
inherit ControllerBase()
30+
31+
[<HttpGet; Produces("text/plain")>]
32+
member this.Get() =
33+
if this.Request.Headers.Accept.Equals("text/plain") then
34+
"Hello world" |> ActionResult<string>
35+
else
36+
ActionResult<string>(UnsupportedMediaTypeResult())
37+
38+
[<Route("api/[controller]")>]
39+
[<ApiController>]
40+
type CheckAcceptsCsvController() =
41+
inherit ControllerBase()
42+
43+
[<HttpGet; Produces("text/csv")>]
44+
member this.Get() =
45+
if this.Request.Headers.Accept.Equals("text/csv") then
46+
"Hello,world" |> ActionResult<string>
47+
else
48+
ActionResult<string>(UnsupportedMediaTypeResult())
49+
2450
[<Route("api/[controller]")>]
2551
[<ApiController>]
2652
type ConsumesTextController() =

0 commit comments

Comments
 (0)