예제 #1
0
        public async Task <HttpClientProxyResponse> SendAsync(HttpClientProxyRequest proxyRequest)
        {
            var httpRequest = new HttpRequestMessage(proxyRequest.HttpMethod, proxyRequest.Url);

            if (proxyRequest.Values != null)
            {
                httpRequest.Content = new FormUrlEncodedContent(proxyRequest.Values);
            }

            if (proxyRequest.Cookies != null)
            {
                // TODO
                // Do I need to add this cookie back in or does it remeber for the current session?
                //_cookies.Add(proxyRequest.Cookies);
            }

            var httpResponse = await _client.SendAsync(httpRequest);

            var proxyResponse = new HttpClientProxyResponse
            {
                StatusCode = httpResponse.StatusCode,
                Contents   = httpResponse.Content == null ? "" : await httpResponse.Content.ReadAsStringAsync(),
                Cookies    = _cookies.GetCookies(new Uri(_client.BaseAddress, proxyRequest.Url))
            };

            //var responseCookies = _cookies.GetCookies().Cast<Cookie>();

            return(proxyResponse);
        }
        public void GetToken()
        {
            var responseContent = new StringBuilder();

            responseContent.AppendLine("<html>");
            responseContent.AppendLine("<body>");
            responseContent.AppendLine("<form>");
            responseContent.AppendLine("<input name=\"__RequestVerificationToken\" type=\"hidden\" value=\"ThisIsATestToken\"/>");
            responseContent.AppendLine("</form>");
            responseContent.AppendLine("</body>");
            responseContent.AppendLine("</html>");

            var response = new HttpClientProxyResponse
            {
                StatusCode = HttpStatusCode.OK,
                Contents   = responseContent.ToString()
            };

            var client = new Mock <IHttpClientProxy>();

            client.Setup(x => x.SendAsync(It.IsAny <HttpClientProxyRequest>()))
            .Returns(Task.FromResult(response));

            var sut    = new AntiForgeryAction(client.Object);
            var result = sut.GetToken("");

            Assert.Equal("ThisIsATestToken", result);
        }
예제 #3
0
        public void GetTargetSecret()
        {
            var responseContent = new StringBuilder();

            responseContent.AppendLine("<html>");
            responseContent.AppendLine("<body>");
            responseContent.AppendLine("<div class=\"panel-heading\">   MySecret   </div>");
            responseContent.AppendLine("<div class=\"panel-body\">  TEST1234   </div>");
            responseContent.AppendLine("</body>");
            responseContent.AppendLine("</html>");

            var response = new HttpClientProxyResponse
            {
                StatusCode = HttpStatusCode.OK,
                Contents   = responseContent.ToString()
            };

            var client = new Mock <IHttpClientProxy>();

            client.Setup(x => x.SendAsync(It.IsAny <HttpClientProxyRequest>()))
            .Returns(Task.FromResult(response));

            var sut    = new SecretsList(client.Object);
            var result = sut.GetTargetSecret("MySecret");

            Assert.Equal("TEST1234", result);
        }
예제 #4
0
        public async void LoginSuccessfulWithAntiForgery()
        {
            var client   = new Mock <IHttpClientProxy>();
            var response = new HttpClientProxyResponse
            {
                StatusCode = HttpStatusCode.OK,
                Contents   = "<html><body><h1>Hello World</h2></body></html>"
            };

            client.Setup(x => x.SendAsync(It.Is <HttpClientProxyRequest>(y =>
                                                                         y.Values.Any(z => z.Key == "Username" && z.Value == "Username") &&
                                                                         y.Values.Any(z => z.Key == "Password" && z.Value == "Password") &&
                                                                         y.Values.Any(z => z.Key == "__RequestVerificationToken" && z.Value == "Token1234"))))
            .Returns(Task.FromResult(response));

            var antiForgery = new Mock <IAntiForgeryAction>();

            antiForgery.Setup(x => x.GetToken(It.IsAny <string>())).Returns("Token1234");

            var sut = new LoginAction(client.Object, antiForgery.Object);

            await sut.LoginAsync("Username", "Password");

            Assert.True(sut.Successful);
            client.VerifyAll();
        }
        public void CreatesSecret()
        {
            var client   = new Mock <IHttpClientProxy>();
            var response = new HttpClientProxyResponse
            {
                StatusCode = HttpStatusCode.OK
            };

            client.Setup(x => x.SendAsync(It.Is <HttpClientProxyRequest>(y =>
                                                                         y.Values.Any(z => z.Key == "Key" && z.Value == "SecretKey") &&
                                                                         y.Values.Any(z => z.Key == "Value" && z.Value == "SecretValue"))))
            .Returns(Task.FromResult(response));

            var antiForgery = new Mock <IAntiForgeryAction>();

            var sut = new CreateSecretAction(client.Object, antiForgery.Object);

            sut.Create("SecretKey", "SecretValue");

            client.VerifyAll();
        }
예제 #6
0
        public async void LoginFailure()
        {
            var client   = new Mock <IHttpClientProxy>();
            var response = new HttpClientProxyResponse
            {
                StatusCode = HttpStatusCode.OK,
                Contents   = "<html><body><h1>Hello Hacker -> Invalid login attempt.</h2></body></html>"
            };

            client.Setup(x => x.SendAsync(It.Is <HttpClientProxyRequest>(y =>
                                                                         y.Values.Any(z => z.Key == "Username" && z.Value == "Username") &&
                                                                         y.Values.Any(z => z.Key == "Password" && z.Value == "Password"))))
            .Returns(Task.FromResult(response));

            var antiForgery = new Mock <IAntiForgeryAction>();

            var sut = new LoginAction(client.Object, antiForgery.Object);

            await sut.LoginAsync("Username", "Password");

            Assert.False(sut.Successful);
            client.VerifyAll();
        }