commit
ad46fa6ad6
55
Test/Flurl.Test/Http/DefaultUrlEncodedSerializerTests.cs
Normal file
55
Test/Flurl.Test/Http/DefaultUrlEncodedSerializerTests.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using Flurl.Http.Configuration;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Flurl.Test.Http
|
||||
{
|
||||
[TestFixture, Parallelizable]
|
||||
public class DefaultUrlEncodedSerializerTests
|
||||
{
|
||||
public class Grant
|
||||
{
|
||||
public string grant_type { get; set; }
|
||||
public string username { get; set; }
|
||||
public string password { get; set; }
|
||||
public string refresh_token { get; set; }
|
||||
public string client_id { get; set; }
|
||||
public string client_secret { get; set; }
|
||||
public string scope { get; set; }
|
||||
}
|
||||
|
||||
public class Grant_Types
|
||||
{
|
||||
public const string password = "password";
|
||||
public const string refresh_token = "refresh_token";
|
||||
public const string client_credentials = "client_credentials";
|
||||
}
|
||||
|
||||
public class Grant_Scopes
|
||||
{
|
||||
public const string password = "openid offline_access";
|
||||
public const string client_credentials = "openid";
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void can_serialize_with_grant_class()
|
||||
{
|
||||
var serializer = new DefaultUrlEncodedSerializer();
|
||||
var result = serializer.Serialize(new Grant
|
||||
{
|
||||
grant_type = Grant_Types.password,
|
||||
username = "user",
|
||||
password = "password",
|
||||
client_id = "Client_Id",
|
||||
client_secret = "Client_Secret",
|
||||
scope = Grant_Scopes.password
|
||||
});
|
||||
|
||||
Assert.AreEqual("grant_type=password&username=user&password=password&client_id=Client_Id&client_secret=Client_Secret&scope=openid+offline_access", result.ToString());
|
||||
}
|
||||
}
|
||||
}
|
@ -19,9 +19,13 @@ namespace Flurl.Http.Configuration
|
||||
return null;
|
||||
|
||||
var qp = new QueryParamCollection();
|
||||
foreach (var kv in obj.ToKeyValuePairs())
|
||||
qp[kv.Key] = new QueryParameter(kv.Key, kv.Value);
|
||||
return qp.ToString(true);
|
||||
foreach (var kv in obj.ToKeyValuePairs())
|
||||
{
|
||||
// if value is null, the serializer shouldn't add this key-value pair.
|
||||
if (kv.Value == null) continue;
|
||||
qp[kv.Key] = new QueryParameter(kv.Key, kv.Value);
|
||||
}
|
||||
return qp.ToString(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user