@ -11,6 +11,7 @@ using NUnit.Framework;
using NzbDrone.Common.Cache ;
using NzbDrone.Common.Cache ;
using NzbDrone.Common.Http ;
using NzbDrone.Common.Http ;
using NzbDrone.Common.Http.Dispatchers ;
using NzbDrone.Common.Http.Dispatchers ;
using NzbDrone.Common.Http.Proxy ;
using NzbDrone.Common.TPL ;
using NzbDrone.Common.TPL ;
using NzbDrone.Test.Common ;
using NzbDrone.Test.Common ;
using NzbDrone.Test.Common.Categories ;
using NzbDrone.Test.Common.Categories ;
@ -22,19 +23,32 @@ namespace NzbDrone.Common.Test.Http
[TestFixture(typeof(CurlHttpDispatcher))]
[TestFixture(typeof(CurlHttpDispatcher))]
public class HttpClientFixture < TDispatcher > : TestBase < HttpClient > where TDispatcher : IHttpDispatcher
public class HttpClientFixture < TDispatcher > : TestBase < HttpClient > where TDispatcher : IHttpDispatcher
{
{
private static string [ ] _httpBinHosts = new [ ] { "eu.httpbin.org" , "httpbin.org" } ;
private static int _httpBinRandom ;
private string _httpBinHost ;
[SetUp]
[SetUp]
public void SetUp ( )
public void SetUp ( )
{
{
Mocker . SetConstant < ICacheManager > ( Mocker . Resolve < CacheManager > ( ) ) ;
Mocker . SetConstant < ICacheManager > ( Mocker . Resolve < CacheManager > ( ) ) ;
Mocker . SetConstant < ICreateManagedWebProxy > ( Mocker . Resolve < ManagedWebProxyFactory > ( ) ) ;
Mocker . SetConstant < IRateLimitService > ( Mocker . Resolve < RateLimitService > ( ) ) ;
Mocker . SetConstant < IRateLimitService > ( Mocker . Resolve < RateLimitService > ( ) ) ;
Mocker . SetConstant < IEnumerable < IHttpRequestInterceptor > > ( new IHttpRequestInterceptor [ 0 ] ) ;
Mocker . SetConstant < IEnumerable < IHttpRequestInterceptor > > ( new IHttpRequestInterceptor [ 0 ] ) ;
Mocker . SetConstant < IHttpDispatcher > ( Mocker . Resolve < TDispatcher > ( ) ) ;
Mocker . SetConstant < IHttpDispatcher > ( Mocker . Resolve < TDispatcher > ( ) ) ;
// Used for manual testing of socks proxies.
//Mocker.GetMock<IHttpProxySettingsProvider>()
// .Setup(v => v.GetProxySettings(It.IsAny<HttpRequest>()))
// .Returns(new HttpProxySettings(ProxyType.Socks5, "127.0.0.1", 5476, "", false));
// Roundrobin over the two servers, to reduce the chance of hitting the ratelimiter.
_httpBinHost = _httpBinHosts [ _httpBinRandom + + % _httpBinHosts . Length ] ;
}
}
[Test]
[Test]
public void should_execute_simple_get ( )
public void should_execute_simple_get ( )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Execute ( request ) ;
var response = Subject . Execute ( request ) ;
@ -44,7 +58,7 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_execute_https_get ( )
public void should_execute_https_get ( )
{
{
var request = new HttpRequest ( "https://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "https://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Execute ( request ) ;
var response = Subject . Execute ( request ) ;
@ -54,7 +68,7 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_execute_typed_get ( )
public void should_execute_typed_get ( )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -66,7 +80,7 @@ namespace NzbDrone.Common.Test.Http
{
{
var message = "{ my: 1 }" ;
var message = "{ my: 1 }" ;
var request = new HttpRequest ( "http://eu.httpbin.org/post" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/post" , _httpBinHost ) ) ;
request . SetContent ( message ) ;
request . SetContent ( message ) ;
var response = Subject . Post < HttpBinResource > ( request ) ;
var response = Subject . Post < HttpBinResource > ( request ) ;
@ -77,7 +91,7 @@ namespace NzbDrone.Common.Test.Http
[TestCase("gzip")]
[TestCase("gzip")]
public void should_execute_get_using_gzip ( string compression )
public void should_execute_get_using_gzip ( string compression )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/" + compression ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/{1}" , _httpBinHost , compression ) ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -93,7 +107,7 @@ namespace NzbDrone.Common.Test.Http
[TestCase(HttpStatusCode.BadGateway)]
[TestCase(HttpStatusCode.BadGateway)]
public void should_throw_on_unsuccessful_status_codes ( int statusCode )
public void should_throw_on_unsuccessful_status_codes ( int statusCode )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/status/" + statusCode ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/status/{1}" , _httpBinHost , statusCode ) ) ;
var exception = Assert . Throws < HttpException > ( ( ) = > Subject . Get < HttpBinResource > ( request ) ) ;
var exception = Assert . Throws < HttpException > ( ( ) = > Subject . Get < HttpBinResource > ( request ) ) ;
@ -105,7 +119,7 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_not_follow_redirects_when_not_in_production ( )
public void should_not_follow_redirects_when_not_in_production ( )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/redirect/1" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/redirect/1" , _httpBinHost ) ) ;
Subject . Get ( request ) ;
Subject . Get ( request ) ;
@ -115,7 +129,7 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_follow_redirects ( )
public void should_follow_redirects ( )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/redirect/1" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/redirect/1" , _httpBinHost ) ) ;
request . AllowAutoRedirect = true ;
request . AllowAutoRedirect = true ;
Subject . Get ( request ) ;
Subject . Get ( request ) ;
@ -126,7 +140,7 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_send_user_agent ( )
public void should_send_user_agent ( )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -140,7 +154,7 @@ namespace NzbDrone.Common.Test.Http
[TestCase("Accept", "text/xml, text/rss+xml, application/rss+xml")]
[TestCase("Accept", "text/xml, text/rss+xml, application/rss+xml")]
public void should_send_headers ( string header , string value )
public void should_send_headers ( string header , string value )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
request . Headers . Add ( header , value ) ;
request . Headers . Add ( header , value ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -163,7 +177,7 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_send_cookie ( )
public void should_send_cookie ( )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
request . Cookies [ "my" ] = "cookie" ;
request . Cookies [ "my" ] = "cookie" ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -220,12 +234,12 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_not_store_response_cookie ( )
public void should_not_store_response_cookie ( )
{
{
var requestSet = new HttpRequest ( "http://eu.httpbin.org/cookies/set?my=cookie" ) ;
var requestSet = new HttpRequest ( string . Format ( "http://{0}/cookies/set?my=cookie" , _httpBinHost ) ) ;
requestSet . AllowAutoRedirect = false ;
requestSet . AllowAutoRedirect = false ;
var responseSet = Subject . Get ( requestSet ) ;
var responseSet = Subject . Get ( requestSet ) ;
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -237,13 +251,13 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_store_response_cookie ( )
public void should_store_response_cookie ( )
{
{
var requestSet = new HttpRequest ( "http://eu.httpbin.org/cookies/set?my=cookie" ) ;
var requestSet = new HttpRequest ( string . Format ( "http://{0}/cookies/set?my=cookie" , _httpBinHost ) ) ;
requestSet . AllowAutoRedirect = false ;
requestSet . AllowAutoRedirect = false ;
requestSet . StoreResponseCookie = true ;
requestSet . StoreResponseCookie = true ;
var responseSet = Subject . Get ( requestSet ) ;
var responseSet = Subject . Get ( requestSet ) ;
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -259,14 +273,14 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_overwrite_response_cookie ( )
public void should_overwrite_response_cookie ( )
{
{
var requestSet = new HttpRequest ( "http://eu.httpbin.org/cookies/set?my=cookie" ) ;
var requestSet = new HttpRequest ( string . Format ( "http://{0}/cookies/set?my=cookie" , _httpBinHost ) ) ;
requestSet . AllowAutoRedirect = false ;
requestSet . AllowAutoRedirect = false ;
requestSet . StoreResponseCookie = true ;
requestSet . StoreResponseCookie = true ;
requestSet . Cookies [ "my" ] = "oldcookie" ;
requestSet . Cookies [ "my" ] = "oldcookie" ;
var responseSet = Subject . Get ( requestSet ) ;
var responseSet = Subject . Get ( requestSet ) ;
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -282,7 +296,7 @@ namespace NzbDrone.Common.Test.Http
[Test]
[Test]
public void should_throw_on_http429_too_many_requests ( )
public void should_throw_on_http429_too_many_requests ( )
{
{
var request = new HttpRequest ( "http://eu.httpbin.org/status/429" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/status/429" , _httpBinHost ) ) ;
Assert . Throws < TooManyRequestsException > ( ( ) = > Subject . Get ( request ) ) ;
Assert . Throws < TooManyRequestsException > ( ( ) = > Subject . Get ( request ) ) ;
@ -302,7 +316,7 @@ namespace NzbDrone.Common.Test.Http
. Setup ( v = > v . PostResponse ( It . IsAny < HttpResponse > ( ) ) )
. Setup ( v = > v . PostResponse ( It . IsAny < HttpResponse > ( ) ) )
. Returns < HttpResponse > ( r = > r ) ;
. Returns < HttpResponse > ( r = > r ) ;
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
Subject . Get ( request ) ;
Subject . Get ( request ) ;
@ -324,7 +338,7 @@ namespace NzbDrone.Common.Test.Http
{
{
// the date is bad in the below - should be 13-Jul-2016
// the date is bad in the below - should be 13-Jul-2016
string malformedCookie = @"__cfduid=d29e686a9d65800021c66faca0a29b4261436890790; expires=Wed, 13-Jul-16 16:19:50 GMT; path=/; HttpOnly" ;
string malformedCookie = @"__cfduid=d29e686a9d65800021c66faca0a29b4261436890790; expires=Wed, 13-Jul-16 16:19:50 GMT; path=/; HttpOnly" ;
var requestSet = new HttpRequestBuilder ( "http://eu.httpbin.org/response-headers" )
var requestSet = new HttpRequestBuilder ( string . Format ( "http://{0}/response-headers" , _httpBinHost ) )
. AddQueryParam ( "Set-Cookie" , malformedCookie )
. AddQueryParam ( "Set-Cookie" , malformedCookie )
. Build ( ) ;
. Build ( ) ;
@ -333,7 +347,7 @@ namespace NzbDrone.Common.Test.Http
var responseSet = Subject . Get ( requestSet ) ;
var responseSet = Subject . Get ( requestSet ) ;
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -358,7 +372,7 @@ namespace NzbDrone.Common.Test.Http
try
try
{
{
// the date is bad in the below - should be 13-Jul-2016
// the date is bad in the below - should be 13-Jul-2016
string url = "http://eu.httpbin.org/response-headers?Set-Cookie=" + Uri . EscapeUriString ( malformedCookie ) ;
string url = string . Format ( "http://{0}/response-headers?Set-Cookie={1}" , _httpBinHost , Uri . EscapeUriString ( malformedCookie ) ) ;
var requestSet = new HttpRequest ( url ) ;
var requestSet = new HttpRequest ( url ) ;
requestSet . AllowAutoRedirect = false ;
requestSet . AllowAutoRedirect = false ;
@ -366,7 +380,7 @@ namespace NzbDrone.Common.Test.Http
var responseSet = Subject . Get ( requestSet ) ;
var responseSet = Subject . Get ( requestSet ) ;
var request = new HttpRequest ( "http://eu.httpbin.org/get" ) ;
var request = new HttpRequest ( string . Format ( "http://{0}/get" , _httpBinHost ) ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
var response = Subject . Get < HttpBinResource > ( request ) ;
@ -378,21 +392,6 @@ namespace NzbDrone.Common.Test.Http
{
{
}
}
}
}
public void should_submit_formparameters_in_body ( )
{
Assert . Fail ( ) ;
}
public void should_submit_attachments_as_multipart ( )
{
Assert . Fail ( ) ;
}
public void should_submit_formparameters_as_multipart_if_attachments_exist ( )
{
Assert . Fail ( ) ;
}
}
}
public class HttpBinResource
public class HttpBinResource