From ee8753784838f5e3dc2c8f00be57f9f8e671fe03 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Mon, 13 Apr 2015 23:25:14 +0200 Subject: [PATCH] Response cookies not stored by default. --- .../Http/HttpClientFixture.cs | 39 +++++++++++++++++++ src/NzbDrone.Common/Http/HttpClient.cs | 16 ++++++-- src/NzbDrone.Common/Http/HttpRequest.cs | 1 + .../BitMeTvTests/BitMeTVFixture.cs | 3 +- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index 7039e34a1..79a2dc38b 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -176,6 +176,45 @@ namespace NzbDrone.Common.Test.Http response.Resource.Headers.Should().NotContainKey("Cookie"); } + + [Test] + public void should_not_store_response_cookie() + { + var requestSet = new HttpRequest("http://eu.httpbin.org/cookies/set?my=cookie"); + requestSet.AllowAutoRedirect = false; + + var responseSet = Subject.Get(requestSet); + + var request = new HttpRequest("http://eu.httpbin.org/get"); + + var response = Subject.Get(request); + + response.Resource.Headers.Should().NotContainKey("Cookie"); + + ExceptionVerification.IgnoreErrors(); + } + + [Test] + public void should_store_response_cookie() + { + var requestSet = new HttpRequest("http://eu.httpbin.org/cookies/set?my=cookie"); + requestSet.AllowAutoRedirect = false; + requestSet.StoreResponseCookie = true; + + var responseSet = Subject.Get(requestSet); + + var request = new HttpRequest("http://eu.httpbin.org/get"); + + var response = Subject.Get(request); + + response.Resource.Headers.Should().ContainKey("Cookie"); + + var cookie = response.Resource.Headers["Cookie"].ToString(); + + cookie.Should().Contain("my=cookie"); + + ExceptionVerification.IgnoreErrors(); + } } public class HttpBinResource diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 9f88541b1..56c495c0c 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -52,8 +52,6 @@ namespace NzbDrone.Common.Http webRequest.AllowAutoRedirect = request.AllowAutoRedirect; webRequest.ContentLength = 0; - webRequest.CookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer()); - if (!RuntimeInfoBase.IsProduction) { webRequest.AllowAutoRedirect = false; @@ -66,17 +64,29 @@ namespace NzbDrone.Common.Http AddRequestHeaders(webRequest, request.Headers); } + var cookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer()); + if (request.Cookies.Count != 0) { foreach (var pair in request.Cookies) { - webRequest.CookieContainer.Add(new Cookie(pair.Key, pair.Value, "/", request.Url.Host) + cookieContainer.Add(new Cookie(pair.Key, pair.Value, "/", request.Url.Host) { Expires = DateTime.UtcNow.AddHours(1) }); } } + if (request.StoreResponseCookie) + { + webRequest.CookieContainer = cookieContainer; + } + else + { + webRequest.CookieContainer = new CookieContainer(); + webRequest.CookieContainer.Add(cookieContainer.GetCookies(request.Url)); + } + if (!request.Body.IsNullOrWhiteSpace()) { var bytes = request.Headers.GetEncodingFromContentType().GetBytes(request.Body.ToCharArray()); diff --git a/src/NzbDrone.Common/Http/HttpRequest.cs b/src/NzbDrone.Common/Http/HttpRequest.cs index 787fab2d8..f71e3f473 100644 --- a/src/NzbDrone.Common/Http/HttpRequest.cs +++ b/src/NzbDrone.Common/Http/HttpRequest.cs @@ -46,6 +46,7 @@ namespace NzbDrone.Common.Http public bool SuppressHttpError { get; set; } public bool AllowAutoRedirect { get; set; } public Dictionary Cookies { get; private set; } + public bool StoreResponseCookie { get; set; } public override string ToString() { diff --git a/src/NzbDrone.Core.Test/IndexerTests/BitMeTvTests/BitMeTVFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/BitMeTvTests/BitMeTVFixture.cs index 2d6c93e29..af7320c9f 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/BitMeTvTests/BitMeTVFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/BitMeTvTests/BitMeTVFixture.cs @@ -28,8 +28,7 @@ namespace NzbDrone.Core.Test.IndexerTests.BitMeTvTests public void should_parse_recent_feed_from_BitMeTv() { var recentFeed = ReadAllText(@"Files/RSS/BitMeTv.xml"); - - + Mocker.GetMock() .Setup(o => o.Execute(It.Is(v => v.Method == HttpMethod.GET))) .Returns(r => new HttpResponse(r, new HttpHeader(), recentFeed));