/// <summary> /// Retrieves a contact with some user supplied identifier. /// </summary> /// <param name="identifier">The user-supplied identifier for the contact.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns> /// A task whose result is the contact, or null if no match is found. /// </returns> public override async Task<Endpoint> LookupAsync(string identifier, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(identifier)) { return null; } Verify.Operation(this.AddressBookLookupUrl != null, "AddressBookLookupUrl not initialized"); var queryString = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); if (HashedEmailRegEx.IsMatch(identifier)) { queryString["emailHash"] = identifier; } else if (EmailRegEx.IsMatch(identifier)) { queryString["email"] = identifier; } else { return null; } var builder = new UriBuilder(this.AddressBookLookupUrl); builder.Query = queryString.UrlEncode(); return await this.DownloadEndpointAsync(builder.Uri, cancellationToken).ConfigureAwait(false); }
public void UrlEncode() { var data = new Dictionary<string, string> { { "a", "b" }, { "a=b&c", "e=f&g" }, }; string urlEncoded = data.UrlEncode(); Assert.That(urlEncoded, Is.EqualTo("a=b&a%3Db%26c=e%3Df%26g")); var decoded = HttpUtility.ParseQueryString(urlEncoded); Assert.That(decoded.Count, Is.EqualTo(data.Count)); foreach (string key in decoded) { Assert.That(data[key], Is.EqualTo(decoded[key])); } }