public string SignUri(string url, DateTime now) { url = AddQuestionMarkAtTheEndIfNoQuerystring(url); var sig = _signatureConstructor.BuildSignature(url, now); var hash = _signatureHasher.Hash(sig); var queryStringDictionary = HttpUtility.ParseQueryString(GetQueryString(url)); queryStringDictionary.Add("apikey", _apiKey); queryStringDictionary.Add("sig", hash); var queryStringWithSignature = ToQueryString(queryStringDictionary); queryStringWithSignature = AddLeadingAmpercentWhenThefirstParameterIsApiKey(queryStringWithSignature); var returnUri = string.Format("{0}{1}", GetAbsolutePath(url), queryStringWithSignature); returnUri = CorrectUrlEncodingWithUpperCaseAfterThepercentage(returnUri); Debug.WriteLine("SignedUri------------------------------------"); Debug.WriteLine(returnUri); Debug.WriteLine("---------------------------------------------"); return(returnUri); }
public void Hash_WhenStringProvided_ShouldHashProperSignature() { //arrange var key = "helloworld"; var sut = new SignatureHasher(key); //act var input = "Mon, 01 Jan 2015 12:00:00+0100\n\nhttps://api.almostinsided.com/thread?filter%5Bforumid%5D=123&order=title+DESC&pageSize=5\napplication/json; version=1"; var actual = sut.Hash(input); //assert actual.Should(Be.EqualTo(@"UxOzeeCrGIcVSnnKdCeOEt1ulj5umyXoEpGMHFv/7Z0=")); }
public void Hash_WhenStringProvided_ShouldHashProperSignature() { //arrange var key = "helloworld"; var sut = new SignatureHasher(key); //act var input = "Mon, 01 Jan 2015 12:00:00+0100\n\nhttps://api.almostinsided.com/thread?filter%5Bforumid%5D=123&order=title+DESC&pageSize=5\napplication/json; version=1"; var actual = sut.Hash(input); //assert actual.Should(Be.EqualTo(@"UxOzeeCrGIcVSnnKdCeOEt1ulj5umyXoEpGMHFv/7Z0=")); }
public void SignUri_WhenCalled_ShouldReturnUriWithSignature() { //arrange var hasher = new SignatureHasher(_secretKey); var signatureConstructor = new SignatureConstructor(); var sut = new UriSignedConstructor(signatureConstructor, hasher, _apiKey); //act var actual = sut.SignUri(_uri, _now).ToString(); //assert var sigValue = WebUtility.UrlEncode(hasher.Hash(signatureConstructor.BuildSignature(_uri, _now))); actual.Should(Be.StringContaining("sig=" + sigValue)); }