Fixed: Mock AcoustId responses so tests don't fail when API down

This commit is contained in:
ta264 2019-10-08 21:50:13 +01:00 committed by Qstick
parent bbca9d1ede
commit 2f96c962a5
2 changed files with 68 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,23 +1,55 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NzbDrone.Core.Parser.Model;
using System;
using NzbDrone.Common.Http;
using Moq;
using static NzbDrone.Core.Parser.FingerprintingService;
using NzbDrone.Test.Common;
using System.Net;
using FluentAssertions;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Http;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
using static NzbDrone.Core.Parser.FingerprintingService;
namespace NzbDrone.Core.Test.ParserTests
{
[TestFixture]
public class FingerprintingServiceFixture : CoreTest<FingerprintingService>
{
public class FingerPrintTest
{
public string RequestContent { get; set; }
public string Response { get; set; }
}
[SetUp]
public void UseAcoustidResponses()
{
// responses were generated by editing HttpClient to write out the content bytes as a string
// using BitConverter.ToString(request.ContentData)
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "Fingerprinting", "AcoustidResponses.json");
var responses = JsonConvert.DeserializeObject<List<FingerPrintTest>>(File.ReadAllText(path));
foreach (var response in responses)
{
Mocker.GetMock<IHttpClient>()
.Setup(o => o.Post<LookupResponse>(
It.Is<HttpRequest>(v =>
v.Url.Equals(new HttpUri("https://api.acoustid.org/v2/lookup")) &&
v.Headers.Contains(new KeyValuePair<string, string>("Content-Encoding", "gzip")) &&
v.Headers.ContentType == "application/x-www-form-urlencoded" &&
// Skip past the first bit of gzip header which varies by OS:
// http://www.onicos.com/staff/iz/formats/gzip.html
BitConverter.ToString(v.ContentData).Substring(31) == response.RequestContent.Substring(31)
)))
.Returns<HttpRequest>(r => new HttpResponse<LookupResponse>(new HttpResponse(r, new HttpHeader(), response.Response)));
}
}
[Test]
public void should_parse_fpcalc_json()
@ -72,8 +104,6 @@ FINGERPRINT=AQAHJlMURlEURcgP6cwRD43Y4Ptw9FowncWPWkf6GB9-JYdP9OgJHw8u4Apw4SsOHMdx
[TestCase("nin.flac")]
public void should_lookup_file(string file)
{
UseRealHttp();
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "Media", file);
var localTrack = new LocalTrack { Path = path };
Subject.Lookup(new List<LocalTrack> { localTrack }, 0.5);
@ -84,8 +114,6 @@ FINGERPRINT=AQAHJlMURlEURcgP6cwRD43Y4Ptw9FowncWPWkf6GB9-JYdP9OgJHw8u4Apw4SsOHMdx
[Test]
public void should_lookup_list()
{
UseRealHttp();
var files = new [] {
"nin.mp3",
"nin.flac"
@ -99,8 +127,6 @@ FINGERPRINT=AQAHJlMURlEURcgP6cwRD43Y4Ptw9FowncWPWkf6GB9-JYdP9OgJHw8u4Apw4SsOHMdx
[Test]
public void should_lookup_list_when_fpcalc_fails_for_some_files()
{
UseRealHttp();
var files = new [] {
"nin.mp3",
"missing.mp3",
@ -119,8 +145,6 @@ FINGERPRINT=AQAHJlMURlEURcgP6cwRD43Y4Ptw9FowncWPWkf6GB9-JYdP9OgJHw8u4Apw4SsOHMdx
[Test]
public void should_lookup_list_when_fpcalc_fails_for_all_files()
{
UseRealHttp();
var files = new [] {
"missing1.mp3",
"missing2.mp3"
@ -137,8 +161,6 @@ FINGERPRINT=AQAHJlMURlEURcgP6cwRD43Y4Ptw9FowncWPWkf6GB9-JYdP9OgJHw8u4Apw4SsOHMdx
[Test]
public void should_not_fail_if_duration_reported_as_zero()
{
UseRealHttp();
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "Media", "missing.mp3");
var localTrack = new LocalTrack { Path = path };
var acoustId = new AcoustId {
@ -152,8 +174,6 @@ FINGERPRINT=AQAHJlMURlEURcgP6cwRD43Y4Ptw9FowncWPWkf6GB9-JYdP9OgJHw8u4Apw4SsOHMdx
[Test]
public void should_not_throw_if_fingerprint_invalid()
{
UseRealHttp();
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "Media", "missing.mp3");
var localTrack = new LocalTrack { Path = path };
var acoustId = new AcoustId {
@ -169,8 +189,6 @@ FINGERPRINT=AQAHJlMURlEURcgP6cwRD43Y4Ptw9FowncWPWkf6GB9-JYdP9OgJHw8u4Apw4SsOHMdx
[Test]
public void should_not_fail_for_some_invalid_fingerprints()
{
UseRealHttp();
var files = new [] {
"nin.mp3",
"nin.flac"