You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs

124 lines
3.9 KiB

10 years ago
using System;
using System.Collections.Generic;
using System.IO;
10 years ago
using System.Net;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Test.Common;
using NzbDrone.Test.Common.Categories;
namespace NzbDrone.Common.Test.Http
{
[TestFixture]
[IntegrationTest]
public class HttpClientFixture : TestBase<HttpClient>
10 years ago
{
[Test]
public void should_execute_simple_get()
{
var request = new HttpRequest("http://eu.httpbin.org/get");
var response = Subject.Execute(request);
response.Content.Should().NotBeNullOrWhiteSpace();
}
[Test]
public void should_execute_typed_get()
{
var request = new HttpRequest("http://eu.httpbin.org/get");
var response = Subject.Get<HttpBinResource>(request);
response.Resource.Url.Should().Be(request.Url.ToString());
}
[TestCase("gzip")]
public void should_execute_get_using_gzip(string compression)
{
var request = new HttpRequest("http://eu.httpbin.org/" + compression);
var response = Subject.Get<HttpBinResource>(request);
response.Resource.Headers["Accept-Encoding"].ToString().Should().Be(compression);
response.Headers.ContentLength.Should().BeLessOrEqualTo(response.Content.Length);
}
[TestCase(HttpStatusCode.Unauthorized)]
[TestCase(HttpStatusCode.Forbidden)]
[TestCase(HttpStatusCode.NotFound)]
[TestCase(HttpStatusCode.InternalServerError)]
[TestCase(HttpStatusCode.ServiceUnavailable)]
[TestCase(HttpStatusCode.BadGateway)]
[TestCase(429)]
public void should_throw_on_unsuccessful_status_codes(int statusCode)
10 years ago
{
var request = new HttpRequest("http://eu.httpbin.org/status/" + statusCode);
10 years ago
var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
((int)exception.Response.StatusCode).Should().Be(statusCode);
ExceptionVerification.IgnoreWarns();
10 years ago
}
[TestCase(HttpStatusCode.Moved)]
[TestCase(HttpStatusCode.MovedPermanently)]
public void should_not_follow_redirects_when_not_in_production(HttpStatusCode statusCode)
{
var request = new HttpRequest("http://eu.httpbin.org/status/" + (int)statusCode);
Subject.Get<HttpBinResource>(request);
ExceptionVerification.ExpectedErrors(1);
10 years ago
}
[Test]
public void should_send_user_agent()
{
var request = new HttpRequest("http://eu.httpbin.org/get");
var response = Subject.Get<HttpBinResource>(request);
response.Resource.Headers.Should().ContainKey("User-Agent");
var userAgent = response.Resource.Headers["User-Agent"].ToString();
userAgent.Should().Contain("Sonarr");
}
[TestCase("Accept", "text/xml, text/rss+xml, application/rss+xml")]
public void should_send_headers(String header, String value)
{
var request = new HttpRequest("http://eu.httpbin.org/get");
request.Headers.Add(header, value);
var response = Subject.Get<HttpBinResource>(request);
response.Resource.Headers[header].ToString().Should().Be(value);
}
[Test]
public void should_not_download_file_with_error()
{
var file = GetTempFilePath();
Assert.Throws<WebException>(() => Subject.DownloadFile("http://download.sonarr.tv/wrongpath", file));
File.Exists(file).Should().BeFalse();
ExceptionVerification.ExpectedWarns(1);
}
}
10 years ago
public class HttpBinResource
{
public Dictionary<string, object> Headers { get; set; }
public string Origin { get; set; }
public string Url { get; set; }
}
}