public void IsNullOrEmpty_ReturnsTrueForEmptyValues()
 {
     // Arrange, act and assert
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(string.Empty)));
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(new JArray())));
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(new JObject())));
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(new JValue(string.Empty))));
 }
 public void IsNullOrEmpty_ReturnsTrueForNullValues()
 {
     // Arrange, act and assert
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter((bool?)null)));
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter((long?)null)));
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter((string)null)));
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter((JArray)null)));
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter((JObject)null)));
     Assert.True(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter((JValue)null)));
 }
 public void IsNullOrEmpty_ReturnsFalseForNonEmptyValues()
 {
     // Arrange, act and assert
     Assert.False(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(true)));
     Assert.False(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter((bool?)true)));
     Assert.False(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(42)));
     Assert.False(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter((long?)42)));
     Assert.False(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter("Fabrikam")));
     Assert.False(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(new JArray("Fabrikam"))));
     Assert.False(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(new JObject {
         ["property"] = "value"
     })));
     Assert.False(OpenIdConnectParameter.IsNullOrEmpty(new OpenIdConnectParameter(new JValue("Fabrikam"))));
 }
        private HttpRequestMessage CreateRequestMessage(OpenIdConnectRequest request, HttpMethod method, Uri uri)
        {
            // Note: a dictionary is deliberately not used here to allow multiple parameters with the
            // same name to be specified. While initially not allowed by the core OAuth2 specification,
            // this is required for derived drafts like the OAuth2 token exchange specification.
            var parameters = new List <KeyValuePair <string, string> >();

            foreach (var parameter in request.GetParameters())
            {
                // If the parameter is null or empty, send an empty value.
                if (OpenIdConnectParameter.IsNullOrEmpty(parameter.Value))
                {
                    parameters.Add(new KeyValuePair <string, string>(parameter.Key, string.Empty));

                    continue;
                }

                var values = (string[])parameter.Value;
                if (values == null || values.Length == 0)
                {
                    continue;
                }

                foreach (var value in values)
                {
                    parameters.Add(new KeyValuePair <string, string>(parameter.Key, value));
                }
            }

            if (method == HttpMethod.Get && parameters.Count != 0)
            {
                var builder = new StringBuilder();

                foreach (var parameter in parameters)
                {
                    if (builder.Length != 0)
                    {
                        builder.Append('&');
                    }

                    builder.Append(UrlEncoder.Default.Encode(parameter.Key));
                    builder.Append('=');
                    builder.Append(UrlEncoder.Default.Encode(parameter.Value));
                }

                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri(HttpClient.BaseAddress, uri);
                }

                uri = new UriBuilder(uri)
                {
                    Query = builder.ToString()
                }.Uri;
            }

            var message = new HttpRequestMessage(method, uri);

            if (method != HttpMethod.Get)
            {
                message.Content = new FormUrlEncodedContent(parameters);
            }

            return(message);
        }