Fixed: BitMeTv cookie will now also be used for the fetching the torrent file.

This commit is contained in:
Taloth Saldono 2015-04-02 20:58:10 +02:00
parent a6d2283be8
commit ccfd66260d
4 changed files with 80 additions and 6 deletions

View File

@ -4,9 +4,11 @@ using System.IO;
using System.Net;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Http;
using NzbDrone.Test.Common;
using NzbDrone.Test.Common.Categories;
using NLog;
namespace NzbDrone.Common.Test.Http
{
@ -14,6 +16,12 @@ namespace NzbDrone.Common.Test.Http
[IntegrationTest]
public class HttpClientFixture : TestBase<HttpClient>
{
[SetUp]
public void SetUp()
{
Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>());
}
[Test]
public void should_execute_simple_get()
{
@ -100,7 +108,6 @@ namespace NzbDrone.Common.Test.Http
response.Resource.Headers[header].ToString().Should().Be(value);
}
[Test]
public void should_not_download_file_with_error()
{
@ -111,7 +118,63 @@ namespace NzbDrone.Common.Test.Http
File.Exists(file).Should().BeFalse();
ExceptionVerification.ExpectedWarns(1);
}
[Test]
public void should_send_cookie()
{
var request = new HttpRequest("http://eu.httpbin.org/get");
request.AddCookie("my", "cookie");
var response = Subject.Get<HttpBinResource>(request);
response.Resource.Headers.Should().ContainKey("Cookie");
var cookie = response.Resource.Headers["Cookie"].ToString();
cookie.Should().Contain("my=cookie");
}
public void GivenOldCookie()
{
var oldRequest = new HttpRequest("http://eu.httpbin.org/get");
oldRequest.AddCookie("my", "cookie");
var oldClient = new HttpClient(Mocker.Resolve<ICacheManager>(), Mocker.Resolve<Logger>());
oldClient.Should().NotBeSameAs(Subject);
var oldResponse = oldClient.Get<HttpBinResource>(oldRequest);
oldResponse.Resource.Headers.Should().ContainKey("Cookie");
}
[Test]
public void should_preserve_cookie_during_session()
{
GivenOldCookie();
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");
}
[Test]
public void should_not_send_cookie_to_other_host()
{
GivenOldCookie();
var request = new HttpRequest("http://httpbin.org/get");
var response = Subject.Get<HttpBinResource>(request);
response.Resource.Headers.Should().NotContainKey("Cookie");
}
}

View File

@ -3,6 +3,7 @@ using System.Diagnostics;
using System.IO;
using System.Net;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
@ -23,10 +24,14 @@ namespace NzbDrone.Common.Http
{
private readonly Logger _logger;
public HttpClient(Logger logger)
private readonly ICached<CookieContainer> _cookieContainerCache;
public HttpClient(ICacheManager cacheManager, Logger logger)
{
_logger = logger;
ServicePointManager.DefaultConnectionLimit = 12;
_cookieContainerCache = cacheManager.GetCache<CookieContainer>(typeof(HttpClient));
}
public HttpResponse Execute(HttpRequest request)
@ -47,6 +52,8 @@ namespace NzbDrone.Common.Http
webRequest.AllowAutoRedirect = request.AllowAutoRedirect;
webRequest.ContentLength = 0;
webRequest.CookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer());
if (!RuntimeInfoBase.IsProduction)
{
webRequest.AllowAutoRedirect = false;
@ -61,10 +68,12 @@ namespace NzbDrone.Common.Http
if (request.Cookies.Count != 0)
{
webRequest.CookieContainer = new CookieContainer();
foreach (var pair in request.Cookies)
{
webRequest.CookieContainer.Add(new Cookie(pair.Key, pair.Value, "/", request.Url.Host));
webRequest.CookieContainer.Add(new Cookie(pair.Key, pair.Value, "/", request.Url.Host)
{
Expires = DateTime.UtcNow.AddHours(1)
});
}
}

View File

@ -1,5 +1,6 @@
using System.IO;
using NUnit.Framework;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Cloud;
using NzbDrone.Common.Http;
using NzbDrone.Test.Common;
@ -16,7 +17,7 @@ namespace NzbDrone.Core.Test.Framework
protected void UseRealHttp()
{
Mocker.SetConstant<IHttpProvider>(new HttpProvider(TestLogger));
Mocker.SetConstant<IHttpClient>(new HttpClient(TestLogger));
Mocker.SetConstant<IHttpClient>(new HttpClient(Mocker.Resolve<CacheManager>(), TestLogger));
Mocker.SetConstant<IDroneServicesRequestBuilder>(new DroneServicesHttpRequestBuilder());
}
}

View File

@ -1,6 +1,7 @@
using System.IO;
using System.Net;
using System.Xml.Linq;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Http;
using NzbDrone.Common.Instrumentation;
using TVDBSharp.Models.Enums;
@ -16,7 +17,7 @@ namespace TVDBSharp.Models.DAO
private const string BaseUrl = "http://thetvdb.com";
private static HttpClient httpClient = new HttpClient(NzbDroneLogger.GetLogger(typeof(DataProvider)));
private static HttpClient httpClient = new HttpClient(new CacheManager(), NzbDroneLogger.GetLogger(typeof(DataProvider)));
public XDocument GetShow(int showID)
{