Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/BloomExe/web/BloomApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,11 @@ public async Task<bool> 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.
Expand All @@ -329,6 +332,16 @@ public async Task<bool> 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(
Expand Down
53 changes: 53 additions & 0 deletions src/BloomTests/web/BloomServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down