Response cookies not stored by default.

pull/6/head
Taloth Saldono 10 years ago
parent d4532c3856
commit ee87537848

@ -176,6 +176,45 @@ namespace NzbDrone.Common.Test.Http
response.Resource.Headers.Should().NotContainKey("Cookie"); 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 public class HttpBinResource

@ -52,8 +52,6 @@ namespace NzbDrone.Common.Http
webRequest.AllowAutoRedirect = request.AllowAutoRedirect; webRequest.AllowAutoRedirect = request.AllowAutoRedirect;
webRequest.ContentLength = 0; webRequest.ContentLength = 0;
webRequest.CookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer());
if (!RuntimeInfoBase.IsProduction) if (!RuntimeInfoBase.IsProduction)
{ {
webRequest.AllowAutoRedirect = false; webRequest.AllowAutoRedirect = false;
@ -66,17 +64,29 @@ namespace NzbDrone.Common.Http
AddRequestHeaders(webRequest, request.Headers); AddRequestHeaders(webRequest, request.Headers);
} }
var cookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer());
if (request.Cookies.Count != 0) if (request.Cookies.Count != 0)
{ {
foreach (var pair in request.Cookies) 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) 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()) if (!request.Body.IsNullOrWhiteSpace())
{ {
var bytes = request.Headers.GetEncodingFromContentType().GetBytes(request.Body.ToCharArray()); var bytes = request.Headers.GetEncodingFromContentType().GetBytes(request.Body.ToCharArray());

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

@ -29,7 +29,6 @@ namespace NzbDrone.Core.Test.IndexerTests.BitMeTvTests
{ {
var recentFeed = ReadAllText(@"Files/RSS/BitMeTv.xml"); var recentFeed = ReadAllText(@"Files/RSS/BitMeTv.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));

Loading…
Cancel
Save