diff --git a/src/BloomExe/web/BloomApiHandler.cs b/src/BloomExe/web/BloomApiHandler.cs index 375bb7449bcb..83f26e70d0fb 100644 --- a/src/BloomExe/web/BloomApiHandler.cs +++ b/src/BloomExe/web/BloomApiHandler.cs @@ -318,8 +318,11 @@ public async Task ProcessRequestAsync(IRequestInfo info, string localPath) info.WriteError(404, $"Server could not process {localPath}"); return true; // we sort of handled it. } - // otherwise it's a programmer error we want to know about. - ReportMissingApiEndpoint(info, localPath); + if (ShouldReportMissingApiEndpoint(endpointPath)) + { + // otherwise it's a programmer error we want to know about. + ReportMissingApiEndpoint(info, localPath); + } // If the user continues from there, we need to pretend to have handled // the request. Otherwise the caller will keep trying to handle it in // other ways. @@ -329,6 +332,16 @@ public async Task ProcessRequestAsync(IRequestInfo info, string localPath) return false; } + private static bool ShouldReportMissingApiEndpoint(string endpointPath) + { + // There are older books out in the wild in which the src for branding images included + // this endpoint. We now handle getting branding images differently. + // Note that this will eventually result in a 404. That's ok because + // the docs in the wild have `onerror="this.style.display='none'"`, + // so we don't get the missing image indicator in the preview. See BL-16300. + return endpointPath != "branding/image"; + } + private static void ReportMissingApiEndpoint(IRequestInfo info, string localPath) { var userMsg = LocalizationManager.GetString( diff --git a/src/BloomTests/web/BloomServerTests.cs b/src/BloomTests/web/BloomServerTests.cs index 4e9f84211e3d..377bfc6b487f 100644 --- a/src/BloomTests/web/BloomServerTests.cs +++ b/src/BloomTests/web/BloomServerTests.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Net; using System.Net.Sockets; +using System.Threading.Tasks; using Bloom; using Bloom.Api; using Bloom.Book; @@ -226,6 +227,58 @@ public void SupportsHandlerInjection() } } + [Test] + public async Task MissingLegacyBrandingApiEndpoint_DoesNotReportNonFatalProblem() + { + using (var server = CreateBloomServer()) + { + server.ApiHandler.RegisterEndpointHandler( + "existingProjectEndpoint", + request => request.ReplyWithText("ok"), + true + ); + NonFatalProblem.LastNonFatalProblemReported = null; + var transaction = new PretendRequestInfo( + BloomServer.ServerUrlWithBloomPrefixEndingInSlash + + "api/branding/image?id=back-cover-outside.png" + ); + + await server.ApiHandler.ProcessRequestAsync(transaction, "api/branding/image"); + + Assert.That(transaction.StatusCode, Is.EqualTo(404)); + Assert.That(transaction.StatusDescription, Is.EqualTo("API endpoint not found")); + Assert.That(NonFatalProblem.LastNonFatalProblemReported, Is.Null); + } + } + + [Test] + public async Task MissingNonLegacyApiEndpoint_ReportsNonFatalProblem() + { + using (var server = CreateBloomServer()) + { + server.ApiHandler.RegisterEndpointHandler( + "existingProjectEndpoint", + request => request.ReplyWithText("ok"), + true + ); + NonFatalProblem.LastNonFatalProblemReported = null; + var transaction = new PretendRequestInfo( + BloomServer.ServerUrlWithBloomPrefixEndingInSlash + "api/notARealEndpoint" + ); + + await server.ApiHandler.ProcessRequestAsync(transaction, "api/notARealEndpoint"); + + Assert.That(transaction.StatusCode, Is.EqualTo(404)); + Assert.That(transaction.StatusDescription, Is.EqualTo("API endpoint not found")); + Assert.That( + NonFatalProblem.LastNonFatalProblemReported, + Does.Contain( + "Server could not find an API endpoint for /bloom/api/notARealEndpoint" + ) + ); + } + } + [Test] public void RegisterBoolEndpointHandler_Works() {