-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from christianhelle/file-upload
Add support for multipart/form-data file uploads
- Loading branch information
Showing
6 changed files
with
131 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"dotnet.defaultSolution": "Refitter.sln" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using FluentAssertions; | ||
using Refitter.Core; | ||
using Refitter.Tests.Build; | ||
using Xunit; | ||
|
||
namespace Refitter.Tests; | ||
|
||
public class MultiPartFormDataTests | ||
{ | ||
private const string OpenApiSpec = @" | ||
{ | ||
""openapi"":""3.0.2"", | ||
""paths"":{ | ||
""/foo/{id}/files"":{ | ||
""post"":{ | ||
""summary"":""uploads a file"", | ||
""operationId"":""uploadFile"", | ||
""parameters"":[ | ||
{ | ||
""name"":""id"", | ||
""in"":""path"", | ||
""description"":""Id of the foo resource"", | ||
""required"":true, | ||
""schema"":{ | ||
""type"":""integer"", | ||
""format"":""int64"" | ||
} | ||
} | ||
], | ||
""requestBody"":{ | ||
""content"":{ | ||
""multipart/form-data"":{ | ||
""schema"":{ | ||
""type"":""object"", | ||
""properties"":{ | ||
""formFile"":{ | ||
""type"":""string"", | ||
""format"":""binary"" | ||
} | ||
} | ||
}, | ||
""encoding"":{ | ||
""formFile"":{ | ||
""style"":""form"" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
""responses"":{ | ||
""200"":{ | ||
""description"":""successful operation"" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
"; | ||
|
||
[Fact] | ||
public async Task Can_Generate_Code() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_Build_Generated_Code() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
BuildHelper | ||
.BuildCSharp(generateCode) | ||
.Should() | ||
.BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task Generated_Code_Contains_MultiPart_Attribute() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().Contain("[Multipart]"); | ||
} | ||
|
||
[Fact] | ||
public async Task Generated_Code_Contains_StreamPart_Parameter() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().Contain("StreamPart"); | ||
} | ||
|
||
private static async Task<string> GenerateCode() | ||
{ | ||
var swaggerFile = await CreateSwaggerFile(OpenApiSpec); | ||
var settings = new RefitGeneratorSettings { OpenApiPath = swaggerFile }; | ||
|
||
var sut = await RefitGenerator.CreateAsync(settings); | ||
var generateCode = sut.Generate(); | ||
return generateCode; | ||
} | ||
|
||
private static async Task<string> CreateSwaggerFile(string contents) | ||
{ | ||
var filename = $"{Guid.NewGuid()}.json"; | ||
var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
Directory.CreateDirectory(folder); | ||
var swaggerFile = Path.Combine(folder, filename); | ||
await File.WriteAllTextAsync(swaggerFile, contents); | ||
return swaggerFile; | ||
} | ||
} |