Response cookies not stored by default.

This commit is contained in:
Taloth Saldono 2015-04-13 23:25:14 +02:00
parent d4532c3856
commit ee87537848
4 changed files with 54 additions and 5 deletions

View File

@ -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<HttpBinResource>(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<HttpBinResource>(request);
response.Resource.Headers.Should().ContainKey("Cookie");
var cookie = response.Resource.Headers["Cookie"].ToString();
cookie.Should().Contain("my=cookie");
ExceptionVerification.IgnoreErrors();
}
}
public class HttpBinResource

View File

@ -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());

View File

@ -46,6 +46,7 @@ namespace NzbDrone.Common.Http
public bool SuppressHttpError { get; set; }
public bool AllowAutoRedirect { get; set; }
public Dictionary<string, string> Cookies { get; private set; }
public bool StoreResponseCookie { get; set; }
public override string ToString()
{

View File

@ -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<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));