How I Diagnosed and Fixed a Bug in hub4j/github-api

A review of the GHNotificationStream bug where a missing Last-Modified value produced an invalid If-Modified-Since request header and a GitHub API 422 response.

While using the notification stream in hub4j/github-api, I encountered a GitHub API 422 response:

1
2
Unable to parse If-Modified-Since request header.
Please make sure value is in an acceptable format.

The fix was merged in PR #1984 and linked to Issue #1985.

Trace the error to the request header

The response explicitly named If-Modified-Since, so the investigation focused on request construction rather than response parsing. In GHNotificationStream.fetch(), the request reused the Last-Modified value from the previous response:

1
req.setHeader("If-Modified-Since", lastModified);

The problem was that lastModified is optional. When it was null, the request layer could send the string "null". That is not a valid HTTP date, so the GitHub API returned 422.

Apply the smallest correct fix

The notification stream did not need a broad refactor. The header should only be set when a value exists:

1
2
3
if (lastModified != null) {
    req.setHeader("If-Modified-Since", lastModified);
}

This preserves the valid path. When GitHub returns Last-Modified, later polls still use a conditional request. When it is absent, the client sends a normal request.

Add a regression test

A one-line code change is not enough evidence that the bug will stay fixed. I added WireMock data for the notification endpoint and extended the existing test with another iteration after a response without Last-Modified.

The test verifies observable behavior rather than a private field: consecutive notification reads must not send an If-Modified-Since header containing "null" and must not receive a 422 response.

Takeaway

An effective open-source fix combines a reproducible failure, a focused code change, and a regression test that matches the project’s existing style.

The same boundary condition applies to many integrations: optional response headers, cursors, continuation tokens, and pagination links must be checked before they are copied into a later request.

Built with Hugo
Theme Stack designed by Jimmy