public void ShouldNotThrowWhenTrueReturnedFromResponse() { OkHttpResponse.Content = HttpContent.Create("true"); MockedHttpClientAdapter.Setup(hca => hca.GetHttpClient(It.IsAny <string>())).Returns(MockedHttpClient.Object); MockedHttpClient.Setup(hc => hc.Get(It.IsAny <string>())).Returns(OkHttpResponse); TestDelegate methodThatShouldThrow = () => PackageAuthenticator.EnsureKeyCanAccessPackage("key", "packageId", "packageVersion"); Assert.DoesNotThrow(methodThatShouldThrow, "Should not have thrown when reponse returned true."); }
public void ShouldThrowWhenReturnedContentIsNotABoolean() { OkHttpResponse.Content = HttpContent.Create("not a bool"); MockedHttpClientAdapter.Setup(hca => hca.GetHttpClient(It.IsAny <string>())).Returns(MockedHttpClient.Object); MockedHttpClient.Setup(hc => hc.Get(It.IsAny <string>())).Returns(OkHttpResponse); TestDelegate methodThatShouldThrow = () => PackageAuthenticator.EnsureKeyCanAccessPackage("key", "packageId", "packageVersion"); Assert.Throws <PackageAuthorizationException>(methodThatShouldThrow, "Should have thrown when reponse returned non-boolean."); }
public void ShouldThrowWhenGivenResponseReturnedBadRequest() { var response = new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest }; MockedHttpClientAdapter.Setup(hca => hca.GetHttpClient(It.IsAny <string>())).Returns(MockedHttpClient.Object); MockedHttpClient.Setup(hc => hc.Get(It.IsAny <string>())).Returns(response); TestDelegate methodThatShouldThrow = () => PackageAuthenticator.EnsureKeyCanAccessPackage("key", "packageId", "packageVersion"); Assert.Throws <Exception>(methodThatShouldThrow, "Should have thrown when reponse returned not-OK status code."); }
public void ShouldUseFrontEndWebSiteRootForHttpClient() { const string expectedWebSiteRoot = "web site root"; MockedConfigSettings.SetupGet(cs => cs.FrontEndWebSiteRoot).Returns(expectedWebSiteRoot); MockedHttpClientAdapter.Setup(hca => hca.GetHttpClient(It.IsAny <string>())).Returns(MockedHttpClient.Object); MockedHttpClient.Setup(hc => hc.Get(It.IsAny <string>())).Returns(OkHttpResponse); PackageAuthenticator.EnsureKeyCanAccessPackage("key", "packageId", "packageVersion"); MockedHttpClientAdapter.Verify(hca => hca.GetHttpClient(expectedWebSiteRoot), Times.Once(), "Should have called HttpClientAdpater's GetHttpClient() with expected web site root."); }
public void ShouldBuildPostHttpMethod() { // arrange const string url = "http://fakeurl/"; var httpClient = new MockedHttpClient(); IHttpClientVerbBuilder <MockedHttpClient> builder = new HttpClientVerbBuilder <MockedHttpClient>(httpClient); // act IHttpClientQueryBuilder queryBuilder = builder.Post(url); // assert Assert.Matches(queryBuilder.HttpMethod.Method, "POST"); }
public void ShouldPutValidatePackageKeyUriAndParametersInRequestUri() { const string expectedValidatePackageKeyUri = "foo"; const string key = "asdf"; const string packageId = "packageId"; const string packageVersion = "packageVersion"; string expectedUri = string.Format("{0}/{1}/{2}/{3}", expectedValidatePackageKeyUri, key, packageId, packageVersion); MockedConfigSettings.SetupGet(cs => cs.ValidatePackageKeyUri).Returns(expectedValidatePackageKeyUri); MockedHttpClientAdapter.Setup(hca => hca.GetHttpClient(It.IsAny <string>())).Returns(MockedHttpClient.Object); MockedHttpClient.Setup(hc => hc.Get(It.IsAny <string>())).Returns(OkHttpResponse); PackageAuthenticator.EnsureKeyCanAccessPackage(key, packageId, packageVersion); MockedHttpClient.Verify(hc => hc.Get(expectedUri), Times.Once(), string.Format("Should have called HttpClient's Get() with expected URI '{0}'.", expectedUri)); }
public void ShouldBuildPostHttpMethodWithJsonPayload() { // arrange const string url = "http://fakeurl/"; var payload = new FakePayload { Id = 10, NonNullDateTime = new DateTime(2010, 1, 1, 10, 00, 00), Note = "Note1", NullableDateTime = new DateTime(2012, 1, 1, 1, 0, 0, 0), Token = Guid.NewGuid() }; var httpClient = new MockedHttpClient(); IHttpClientVerbBuilder <MockedHttpClient> builder = new HttpClientVerbBuilder <MockedHttpClient>(httpClient); // act IHttpClientQueryBuilder queryBuilder = builder.PostAsJson(url, payload); // assert Assert.NotNull(queryBuilder.Content); Assert.Matches(queryBuilder.HttpMethod.Method, "POST"); JsonContent content = Assert.IsType <JsonContent>(queryBuilder.Content); }