Skip to content

Make URLSession client honor the HTTP status code#241

Merged
kylebrowning merged 1 commit into
mainfrom
fix/urlsession-status-code
Jun 26, 2026
Merged

Make URLSession client honor the HTTP status code#241
kylebrowning merged 1 commit into
mainfrom
fix/urlsession-status-code

Conversation

@kylebrowning

Copy link
Copy Markdown
Owner

Problem

APNSURLSessionClient.send determined success/failure purely by whether the response body decoded as an APNSErrorResponse, ignoring the HTTP status code, and threw urlResponseNotFound whenever the apns-id header was missing. Consequences:

  • A non-2xx response with an empty or non-JSON body was returned as a success APNSResponse.
  • An error response without an apns-id header surfaced as urlResponseNotFound rather than a typed APNSError.

The NIO APNSClient already keys off response.status == .ok; this brings the URLSession client in line.

Changes

  • Treat statusCode == 200 as success; on any other status, decode the error body when present and otherwise surface the status code alone.
  • Read headers via the case-insensitive value(forHTTPHeaderField:) and tolerate a missing apns-id.
  • Add the APNSURLSession target to the test target in both Package.swift and Package@swift-5.10.swift.
  • New APNSURLSessionClientTests (previously the entire target had zero tests) covering success, header propagation, bad-device-token, and missing-topic, exercised against APNSTestServer.

Testing

swift test — 76 tests pass (up from 72).

`APNSURLSessionClient.send` decided success vs. failure purely by whether the
response body decoded as an `APNSErrorResponse`, ignoring the HTTP status code,
and threw `urlResponseNotFound` whenever the `apns-id` header was absent. As a
result a non-2xx response with an empty/non-JSON body was reported as success,
and an error response without `apns-id` surfaced as `urlResponseNotFound`
instead of a typed `APNSError`.

- Treat `statusCode == 200` as success (per Apple's APNs spec); decode the error
  body on any other status, falling back to the status code alone when absent.
- Look up response headers via the case-insensitive `value(forHTTPHeaderField:)`
  and tolerate a missing `apns-id`.
- Add the `APNSURLSession` target to the test target (both manifests) and an
  `APNSURLSessionClientTests` suite covering success, header propagation, and the
  bad-device-token / missing-topic error paths against `APNSTestServer`.
@kylebrowning kylebrowning merged commit ca89e26 into main Jun 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant