[csharp-netcore][httpclient] add tests, bug fixes (#8885)

* add tests to csharp-httpclient petstore

* fix basepath in tests

* fix response headers

* skip file upload test in httpclient

* update samples

* update tech committee
This commit is contained in:
William Cheng 2021-03-04 16:02:31 +08:00 committed by GitHub
parent 1983dfc122
commit e18d4b97e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 268 additions and 58 deletions

View File

@ -1000,7 +1000,7 @@ If you want to join the committee, please kindly apply by sending an email to te
| Bash | @frol (2017/07) @bkryza (2017/08) @kenjones-cisco (2017/09) |
| C | @zhemant (2018/11) @ityuhui (2019/12) @michelealbano (2020/03) |
| C++ | @ravinikam (2017/07) @stkrwork (2017/07) @etherealjoy (2018/02) @martindelille (2018/03) @muttleyxd (2019/08) |
| C# | @mandrean (2017/08) @frankyjuang (2019/09) @shibayan (2020/02) |
| C# | @mandrean (2017/08) @frankyjuang (2019/09) @shibayan (2020/02) @Blackclaws (2021/03) |
| Clojure | |
| Dart | @swipesight (2018/09) @jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) |
| Eiffel | @jvelilla (2017/09) |

View File

@ -339,11 +339,21 @@ namespace {{packageName}}.Client
Cookies = new List<Cookie>()
};
// process response headers, e.g. Access-Control-Allow-Methods
if (response.Headers != null)
{
foreach (var responseHeader in response.Headers)
{
transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value));
}
}
// process response content headers, e.g. Content-Type
if (response.Content.Headers != null)
{
foreach (var responseHeader in response.Content.Headers)
{
transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value));
}
}

View File

@ -21,3 +21,4 @@
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj

View File

@ -84,7 +84,6 @@ docs/UserApi.md
docs/Whale.md
docs/Zebra.md
git_push.sh
src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj
src/Org.OpenAPITools/Api/AnotherFakeApi.cs
src/Org.OpenAPITools/Api/DefaultApi.cs
src/Org.OpenAPITools/Api/FakeApi.cs

View File

@ -3,7 +3,8 @@
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
* OpenAPI spec version: 1.0.0
*
* Generated by: https://github.com/openapitools/openapi-generator.git
*/
@ -17,10 +18,9 @@ using Xunit;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Api;
// uncomment below to import models
//using Org.OpenAPITools.Model;
using Org.OpenAPITools.Model;
namespace Org.OpenAPITools.Test.Api
namespace Org.OpenAPITools.Test
{
/// <summary>
/// Class for testing PetApi
@ -33,14 +33,55 @@ namespace Org.OpenAPITools.Test.Api
{
private PetApi instance;
private long petId = 11088;
/// <summary>
/// Create a Pet object
/// </summary>
private Pet createPet()
{
// create pet
Pet p = new Pet(name: "Csharp test", photoUrls: new List<string> { "http://petstore.com/csharp_test" });
p.Id = petId;
//p.Name = "Csharp test";
p.Status = Pet.StatusEnum.Available;
// create Category object
Category category = new Category();
category.Id = 56;
category.Name = "sample category name2";
List<String> photoUrls = new List<String>(new String[] { "sample photoUrls" });
// create Tag object
Tag tag = new Tag();
tag.Id = petId;
tag.Name = "csharp sample tag name1";
List<Tag> tags = new List<Tag>(new Tag[] { tag });
p.Tags = tags;
p.Category = category;
p.PhotoUrls = photoUrls;
return p;
}
/// <summary>
/// Convert string to byte array
/// </summary>
private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public PetApiTests()
{
instance = new PetApi();
}
public void Dispose()
{
// Cleanup when everything is done.
// create pet
Pet p = createPet();
// add pet before testing
PetApi petApi = new PetApi("http://petstore.swagger.io/v2");
petApi.AddPet(p);
}
/// <summary>
@ -49,19 +90,19 @@ namespace Org.OpenAPITools.Test.Api
[Fact]
public void InstanceTest()
{
// TODO uncomment below to test 'IsType' PetApi
//Assert.IsType<PetApi>(instance);
Assert.IsType<PetApi>(instance);
}
/// <summary>
/// Test AddPet
/// </summary>
[Fact]
public void AddPetTest()
{
// TODO uncomment below to test the method and replace null with proper value
//Pet pet = null;
//instance.AddPet(pet);
// create pet
Pet p = createPet();
instance.AddPet(p);
}
/// <summary>
@ -70,10 +111,7 @@ namespace Org.OpenAPITools.Test.Api
[Fact]
public void DeletePetTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long petId = null;
//string apiKey = null;
//instance.DeletePet(petId, apiKey);
// no need to test as it'c covered by Cleanup() already
}
/// <summary>
@ -82,10 +120,15 @@ namespace Org.OpenAPITools.Test.Api
[Fact]
public void FindPetsByStatusTest()
{
// TODO uncomment below to test the method and replace null with proper value
//List<string> status = null;
//var response = instance.FindPetsByStatus(status);
//Assert.IsType<List<Pet>>(response);
PetApi petApi = new PetApi();
List<String> tagsList = new List<String>(new String[] { "available" });
List<Pet> listPet = petApi.FindPetsByTags(tagsList);
foreach (Pet pet in listPet) // Loop through List with foreach.
{
Assert.IsType<Pet>(pet);
Assert.Equal("available", pet.Tags[0].Name);
}
}
/// <summary>
@ -94,10 +137,9 @@ namespace Org.OpenAPITools.Test.Api
[Fact]
public void FindPetsByTagsTest()
{
// TODO uncomment below to test the method and replace null with proper value
//List<string> tags = null;
//var response = instance.FindPetsByTags(tags);
//Assert.IsType<List<Pet>>(response);
List<string> tags = new List<String>(new String[] { "pet" });
var response = instance.FindPetsByTags(tags);
Assert.IsType<List<Pet>>(response);
}
/// <summary>
@ -106,10 +148,98 @@ namespace Org.OpenAPITools.Test.Api
[Fact]
public void GetPetByIdTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long petId = null;
//var response = instance.GetPetById(petId);
//Assert.IsType<Pet>(response);
// set timeout to 10 seconds
Configuration c1 = new Configuration();
c1.Timeout = 10000;
c1.UserAgent = "TEST_USER_AGENT";
PetApi petApi = new PetApi(c1);
Pet response = petApi.GetPetById(petId);
Assert.IsType<Pet>(response);
Assert.Equal("Csharp test", response.Name);
Assert.Equal(Pet.StatusEnum.Available, response.Status);
Assert.IsType<List<Tag>>(response.Tags);
Assert.Equal(petId, response.Tags[0].Id);
Assert.Equal("csharp sample tag name1", response.Tags[0].Name);
Assert.IsType<List<String>>(response.PhotoUrls);
Assert.Equal("sample photoUrls", response.PhotoUrls[0]);
Assert.IsType<Category>(response.Category);
Assert.Equal(56, response.Category.Id);
Assert.Equal("sample category name2", response.Category.Name);
}
/// <summary>
/// Test GetPetByIdAsync
/// </summary>
[Fact]
public void TestGetPetByIdAsync()
{
PetApi petApi = new PetApi();
var task = petApi.GetPetByIdAsync(petId);
Pet response = task.Result;
Assert.IsType<Pet>(response);
Assert.Equal("Csharp test", response.Name);
Assert.Equal(Pet.StatusEnum.Available, response.Status);
Assert.IsType<List<Tag>>(response.Tags);
Assert.Equal(petId, response.Tags[0].Id);
Assert.Equal("csharp sample tag name1", response.Tags[0].Name);
Assert.IsType<List<String>>(response.PhotoUrls);
Assert.Equal("sample photoUrls", response.PhotoUrls[0]);
Assert.IsType<Category>(response.Category);
Assert.Equal(56, response.Category.Id);
Assert.Equal("sample category name2", response.Category.Name);
}
/* a simple test for binary response. no longer in use.
[Fact]
public void TestGetByIdBinaryResponse()
{
PetApi petApi = new PetApi(c1);
Stream response = petApi.GetPetByIdBinaryResponse(petId);
Assert.IsType<System.IO.MemoryStream>(response);
StreamReader reader = new StreamReader(response);
// the following will fail for sure
//Assert.Equal("someting", reader.ReadToEnd());
}
*/
/// <summary>
/// Test GetPetByIdWithHttpInfoAsync
/// </summary>
[Fact]
public void TestGetPetByIdWithHttpInfoAsync()
{
PetApi petApi = new PetApi();
var task = petApi.GetPetByIdWithHttpInfoAsync(petId);
Assert.Equal(200, (int)task.Result.StatusCode);
Assert.True(task.Result.Headers.ContainsKey("Content-Type"));
Assert.Equal("application/json", task.Result.Headers["Content-Type"][0]);
Pet response = task.Result.Data;
Assert.IsType<Pet>(response);
Assert.Equal("Csharp test", response.Name);
Assert.Equal(Pet.StatusEnum.Available, response.Status);
Assert.IsType<List<Tag>>(response.Tags);
Assert.Equal(petId, response.Tags[0].Id);
Assert.Equal("csharp sample tag name1", response.Tags[0].Name);
Assert.IsType<List<String>>(response.PhotoUrls);
Assert.Equal("sample photoUrls", response.PhotoUrls[0]);
Assert.IsType<Category>(response.Category);
Assert.Equal(56, response.Category.Id);
Assert.Equal("sample category name2", response.Category.Name);
}
/// <summary>
@ -119,8 +249,10 @@ namespace Org.OpenAPITools.Test.Api
public void UpdatePetTest()
{
// TODO uncomment below to test the method and replace null with proper value
//Pet pet = null;
//instance.UpdatePet(pet);
// create pet
Pet p = createPet();
instance.UpdatePet(p);
}
/// <summary>
@ -129,25 +261,41 @@ namespace Org.OpenAPITools.Test.Api
[Fact]
public void UpdatePetWithFormTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long petId = null;
//string name = null;
//string status = null;
//instance.UpdatePetWithForm(petId, name, status);
PetApi petApi = new PetApi();
petApi.UpdatePetWithForm(petId, "new form name", "pending");
Pet response = petApi.GetPetById(petId);
Assert.IsType<Pet>(response);
Assert.IsType<Category>(response.Category);
Assert.IsType<List<Tag>>(response.Tags);
Assert.Equal("new form name", response.Name);
Assert.Equal(Pet.StatusEnum.Pending, response.Status);
Assert.Equal(petId, response.Tags[0].Id);
Assert.Equal(56, response.Category.Id);
// test optional parameter
petApi.UpdatePetWithForm(petId, "new form name2");
Pet response2 = petApi.GetPetById(petId);
Assert.Equal("new form name2", response2.Name);
}
/// <summary>
/// Test UploadFile
/// </summary>
[Fact]
[Fact (Skip = "file upload not working for httpclient yet")]
public void UploadFileTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long petId = null;
//string additionalMetadata = null;
//System.IO.Stream file = null;
//var response = instance.UploadFile(petId, additionalMetadata, file);
//Assert.IsType<ApiResponse>(response);
Assembly _assembly = Assembly.GetExecutingAssembly();
Stream _imageStream = _assembly.GetManifestResourceStream("Org.OpenAPITools.Test.linux-logo.png");
PetApi petApi = new PetApi();
// test file upload with form parameters
petApi.UploadFile(petId, "new form name", _imageStream);
// test file upload without any form parameters
// using optional parameter syntax introduced at .net 4.0
petApi.UploadFile(petId: petId, file: _imageStream);
}
/// <summary>
@ -157,11 +305,47 @@ namespace Org.OpenAPITools.Test.Api
public void UploadFileWithRequiredFileTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long petId = null;
//long? petId = null;
//System.IO.Stream requiredFile = null;
//string additionalMetadata = null;
//var response = instance.UploadFileWithRequiredFile(petId, requiredFile, additionalMetadata);
//Assert.IsType<ApiResponse>(response);
//Assert.IsType<ApiResponse> (response, "response is ApiResponse");
}
/// <summary>
/// Test status code
/// </summary>
[Fact]
public void TestStatusCodeAndHeader()
{
PetApi petApi = new PetApi();
var response = petApi.GetPetByIdWithHttpInfo(petId);
//Assert.Equal("OK", response.StatusCode);
Assert.Equal(200, (int)response.StatusCode);
Assert.True(response.Headers.ContainsKey("Content-Type"));
Assert.Equal("application/json", response.Headers["Content-Type"][0]);
}
/// <summary>
/// Test default header (should be deprecated)
/// </summary>
[Fact]
public void TestDefaultHeader()
{
//PetApi petApi = new PetApi();
// commented out the warning test below as it's confirmed the warning is working as expected
// there should be a warning for using AddDefaultHeader (deprecated) below
//petApi.AddDefaultHeader ("header_key", "header_value");
// the following should be used instead as suggested in the doc
//petApi.Configuration.AddDefaultHeader("header_key2", "header_value2");
}
public void Dispose()
{
// remove the pet after testing
PetApi petApi = new PetApi();
petApi.DeletePet(petId, "test key");
}
}
}

View File

@ -17,4 +17,10 @@
<ProjectReference Include="..\Org.OpenAPITools\Org.OpenAPITools.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="linux-logo.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="**/*" />
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 305 KiB

View File

@ -327,11 +327,21 @@ namespace Org.OpenAPITools.Client
Cookies = new List<Cookie>()
};
// process response headers, e.g. Access-Control-Allow-Methods
if (response.Headers != null)
{
foreach (var responseHeader in response.Headers)
{
transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value));
}
}
// process response content headers, e.g. Content-Type
if (response.Content.Headers != null)
{
foreach (var responseHeader in response.Content.Headers)
{
transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value));
}
}