[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) | | Bash | @frol (2017/07) @bkryza (2017/08) @kenjones-cisco (2017/09) |
| C | @zhemant (2018/11) @ityuhui (2019/12) @michelealbano (2020/03) | | 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++ | @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 | | | 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) | | 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) | | Eiffel | @jvelilla (2017/09) |

View File

@ -339,11 +339,21 @@ namespace {{packageName}}.Client
Cookies = new List<Cookie>() Cookies = new List<Cookie>()
}; };
// process response headers, e.g. Access-Control-Allow-Methods
if (response.Headers != null) if (response.Headers != null)
{ {
foreach (var responseHeader in response.Headers) 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 #docs/*.md
# Then explicitly reverse the ignore rule for a single file: # Then explicitly reverse the ignore rule for a single file:
#!docs/README.md #!docs/README.md
src/Org.OpenAPITools.Test/Org.OpenAPITools.Test.csproj

View File

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

View File

@ -1,9 +1,10 @@
/* /*
* OpenAPI Petstore * OpenAPI Petstore
* *
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ * 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 * Generated by: https://github.com/openapitools/openapi-generator.git
*/ */
@ -17,13 +18,12 @@ using Xunit;
using Org.OpenAPITools.Client; using Org.OpenAPITools.Client;
using Org.OpenAPITools.Api; 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> /// <summary>
/// Class for testing PetApi /// Class for testing PetApi
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This file is automatically generated by OpenAPI Generator (https://openapi-generator.tech). /// This file is automatically generated by OpenAPI Generator (https://openapi-generator.tech).
@ -33,85 +33,215 @@ namespace Org.OpenAPITools.Test.Api
{ {
private PetApi instance; 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() public PetApiTests()
{ {
instance = new PetApi(); instance = new PetApi();
}
public void Dispose() // create pet
{ Pet p = createPet();
// Cleanup when everything is done.
}
// add pet before testing
PetApi petApi = new PetApi("http://petstore.swagger.io/v2");
petApi.AddPet(p);
}
/// <summary> /// <summary>
/// Test an instance of PetApi /// Test an instance of PetApi
/// </summary> /// </summary>
[Fact] [Fact]
public void InstanceTest() public void InstanceTest()
{ {
// TODO uncomment below to test 'IsType' PetApi Assert.IsType<PetApi>(instance);
//Assert.IsType<PetApi>(instance);
} }
/// <summary> /// <summary>
/// Test AddPet /// Test AddPet
/// </summary> /// </summary>
[Fact] [Fact]
public void AddPetTest() public void AddPetTest()
{ {
// TODO uncomment below to test the method and replace null with proper value // create pet
//Pet pet = null; Pet p = createPet();
//instance.AddPet(pet); instance.AddPet(p);
} }
/// <summary> /// <summary>
/// Test DeletePet /// Test DeletePet
/// </summary> /// </summary>
[Fact] [Fact]
public void DeletePetTest() public void DeletePetTest()
{ {
// TODO uncomment below to test the method and replace null with proper value // no need to test as it'c covered by Cleanup() already
//long petId = null;
//string apiKey = null;
//instance.DeletePet(petId, apiKey);
} }
/// <summary> /// <summary>
/// Test FindPetsByStatus /// Test FindPetsByStatus
/// </summary> /// </summary>
[Fact] [Fact]
public void FindPetsByStatusTest() public void FindPetsByStatusTest()
{ {
// TODO uncomment below to test the method and replace null with proper value PetApi petApi = new PetApi();
//List<string> status = null; List<String> tagsList = new List<String>(new String[] { "available" });
//var response = instance.FindPetsByStatus(status);
//Assert.IsType<List<Pet>>(response);
}
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> /// <summary>
/// Test FindPetsByTags /// Test FindPetsByTags
/// </summary> /// </summary>
[Fact] [Fact]
public void FindPetsByTagsTest() public void FindPetsByTagsTest()
{ {
// TODO uncomment below to test the method and replace null with proper value List<string> tags = new List<String>(new String[] { "pet" });
//List<string> tags = null; var response = instance.FindPetsByTags(tags);
//var response = instance.FindPetsByTags(tags); Assert.IsType<List<Pet>>(response);
//Assert.IsType<List<Pet>>(response);
} }
/// <summary> /// <summary>
/// Test GetPetById /// Test GetPetById
/// </summary> /// </summary>
[Fact] [Fact]
public void GetPetByIdTest() public void GetPetByIdTest()
{ {
// TODO uncomment below to test the method and replace null with proper value // set timeout to 10 seconds
//long petId = null; Configuration c1 = new Configuration();
//var response = instance.GetPetById(petId); c1.Timeout = 10000;
//Assert.IsType<Pet>(response); 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> /// <summary>
/// Test UpdatePet /// Test UpdatePet
/// </summary> /// </summary>
@ -119,37 +249,55 @@ namespace Org.OpenAPITools.Test.Api
public void UpdatePetTest() public void UpdatePetTest()
{ {
// TODO uncomment below to test the method and replace null with proper value // TODO uncomment below to test the method and replace null with proper value
//Pet pet = null; // create pet
//instance.UpdatePet(pet); Pet p = createPet();
instance.UpdatePet(p);
} }
/// <summary> /// <summary>
/// Test UpdatePetWithForm /// Test UpdatePetWithForm
/// </summary> /// </summary>
[Fact] [Fact]
public void UpdatePetWithFormTest() public void UpdatePetWithFormTest()
{ {
// TODO uncomment below to test the method and replace null with proper value PetApi petApi = new PetApi();
//long petId = null; petApi.UpdatePetWithForm(petId, "new form name", "pending");
//string name = null;
//string status = null;
//instance.UpdatePetWithForm(petId, name, status);
}
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> /// <summary>
/// Test UploadFile /// Test UploadFile
/// </summary> /// </summary>
[Fact] [Fact (Skip = "file upload not working for httpclient yet")]
public void UploadFileTest() public void UploadFileTest()
{ {
// TODO uncomment below to test the method and replace null with proper value Assembly _assembly = Assembly.GetExecutingAssembly();
//long petId = null; Stream _imageStream = _assembly.GetManifestResourceStream("Org.OpenAPITools.Test.linux-logo.png");
//string additionalMetadata = null; PetApi petApi = new PetApi();
//System.IO.Stream file = null; // test file upload with form parameters
//var response = instance.UploadFile(petId, additionalMetadata, file); petApi.UploadFile(petId, "new form name", _imageStream);
//Assert.IsType<ApiResponse>(response);
}
// test file upload without any form parameters
// using optional parameter syntax introduced at .net 4.0
petApi.UploadFile(petId: petId, file: _imageStream);
}
/// <summary> /// <summary>
/// Test UploadFileWithRequiredFile /// Test UploadFileWithRequiredFile
/// </summary> /// </summary>
@ -157,11 +305,47 @@ namespace Org.OpenAPITools.Test.Api
public void UploadFileWithRequiredFileTest() public void UploadFileWithRequiredFileTest()
{ {
// TODO uncomment below to test the method and replace null with proper value // TODO uncomment below to test the method and replace null with proper value
//long petId = null; //long? petId = null;
//System.IO.Stream requiredFile = null; //System.IO.Stream requiredFile = null;
//string additionalMetadata = null; //string additionalMetadata = null;
//var response = instance.UploadFileWithRequiredFile(petId, requiredFile, additionalMetadata); //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" /> <ProjectReference Include="..\Org.OpenAPITools\Org.OpenAPITools.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Remove="linux-logo.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="**/*" />
</ItemGroup>
</Project> </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>() Cookies = new List<Cookie>()
}; };
// process response headers, e.g. Access-Control-Allow-Methods
if (response.Headers != null) if (response.Headers != null)
{ {
foreach (var responseHeader in response.Headers) 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));
} }
} }