2020-06-09 02:04:51 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Tests
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class RequestParamsTest
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void CacheDoesNotInterfereQuery()
|
|
|
|
{
|
|
|
|
var first = new FirstRequestModel { First = true };
|
|
|
|
var firstParams = first.BuildQueryParams();
|
|
|
|
|
|
|
|
var second = new SecondRequestModel { Second = false };
|
|
|
|
var secondParams = second.BuildQueryParams();
|
|
|
|
|
2024-02-10 10:41:47 +00:00
|
|
|
Assert.That(1, Is.EqualTo(firstParams.Keys.Count));
|
|
|
|
Assert.That("true", Is.EqualTo(firstParams["first"]));
|
2020-06-09 02:04:51 +01:00
|
|
|
|
2024-02-10 10:41:47 +00:00
|
|
|
Assert.That(1, Is.EqualTo(secondParams.Keys.Count));
|
|
|
|
Assert.That("false", Is.EqualTo(secondParams["second"]));
|
2020-06-09 02:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void CacheDoesNotInterfereBody()
|
|
|
|
{
|
|
|
|
var first = new FirstRequestModel { First = true };
|
|
|
|
var firstParams = first.BuildBodyParams();
|
|
|
|
|
|
|
|
var second = new SecondRequestModel { Second = false };
|
|
|
|
var secondParams = second.BuildBodyParams();
|
|
|
|
|
2024-02-10 10:41:47 +00:00
|
|
|
Assert.That("{\"first\":true}", Is.EqualTo(firstParams.ToString(Formatting.None)));
|
|
|
|
Assert.That("{\"second\":false}", Is.EqualTo(secondParams.ToString(Formatting.None)));
|
2020-06-09 02:04:51 +01:00
|
|
|
}
|
2020-06-29 21:42:35 +01:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void EmptyListIsSkippedInQueryParams()
|
|
|
|
{
|
|
|
|
var first = new EmptyListExampleRequestModel();
|
2024-02-10 10:41:47 +00:00
|
|
|
Assert.That(new Dictionary<string, string> { }, Is.EqualTo(first.BuildQueryParams()));
|
2020-06-29 21:42:35 +01:00
|
|
|
first.List.Add("hello_world");
|
2024-02-10 10:41:47 +00:00
|
|
|
Assert.That(new Dictionary<string, string> { { "list", "hello_world" } }, Is.EqualTo(first.BuildQueryParams()));
|
2020-06-29 21:42:35 +01:00
|
|
|
}
|
2020-07-13 14:49:23 +01:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void EnumWithoutFlagsDoesNotHaveMultipleValues()
|
|
|
|
{
|
|
|
|
var enumModel = new EnumWithoutFlagsRequestModel
|
|
|
|
{
|
|
|
|
AnEnumParam = EnumWithoutFlagsRequestModel.AnEnum.Two
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = enumModel.BuildQueryParams();
|
2024-02-10 10:41:47 +00:00
|
|
|
Assert.That(1, Is.EqualTo(result.Keys.Count));
|
|
|
|
Assert.That("two", Is.EqualTo(result["an_enum"]));
|
2020-07-13 14:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void EnumWithFlagsDoesHaveMultipleValues()
|
|
|
|
{
|
|
|
|
var enumModel = new EnumWitFlagsRequestModel
|
|
|
|
{
|
|
|
|
AnEnumParam = EnumWitFlagsRequestModel.AnEnum.Two | EnumWitFlagsRequestModel.AnEnum.One
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = enumModel.BuildQueryParams();
|
2024-02-10 10:41:47 +00:00
|
|
|
Assert.That(1, Is.EqualTo(result.Keys.Count));
|
|
|
|
Assert.That("one,two", Is.EqualTo(result["an_enum"]));
|
2020-07-13 14:49:23 +01:00
|
|
|
}
|
2020-06-09 02:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public class FirstRequestModel : RequestParams
|
|
|
|
{
|
|
|
|
[BodyParam("first")]
|
|
|
|
[QueryParam("first")]
|
|
|
|
public bool? First { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class SecondRequestModel : RequestParams
|
|
|
|
{
|
|
|
|
[BodyParam("second")]
|
|
|
|
[QueryParam("second")]
|
|
|
|
public bool? Second { get; set; }
|
|
|
|
}
|
2020-06-29 21:42:35 +01:00
|
|
|
|
|
|
|
public class EmptyListExampleRequestModel : RequestParams
|
|
|
|
{
|
|
|
|
[QueryParam("list")]
|
|
|
|
public IList<string> List { get; set; } = new List<string>();
|
|
|
|
}
|
2020-07-13 14:49:23 +01:00
|
|
|
|
|
|
|
public class EnumWithoutFlagsRequestModel : RequestParams
|
|
|
|
{
|
|
|
|
[QueryParam("an_enum")]
|
|
|
|
public AnEnum AnEnumParam { get; set; }
|
|
|
|
|
|
|
|
public enum AnEnum
|
|
|
|
{
|
|
|
|
[String("one")]
|
|
|
|
One,
|
|
|
|
|
|
|
|
[String("two")]
|
|
|
|
Two,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class EnumWitFlagsRequestModel : RequestParams
|
|
|
|
{
|
|
|
|
[QueryParam("an_enum")]
|
|
|
|
public AnEnum AnEnumParam { get; set; }
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
public enum AnEnum
|
|
|
|
{
|
|
|
|
[String("one")]
|
|
|
|
One = 1,
|
|
|
|
|
|
|
|
[String("two")]
|
|
|
|
Two = 2,
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 02:04:51 +01:00
|
|
|
}
|