Skip to content

API mock interceptor: XHR onreadystatechange wrapper creates synthetic Event #125

Description

@tyevco

Summary

In plugins/api-mock-interceptor/src/core/interceptor.ts (lines 236-295), the XHR onreadystatechange wrapper calls the original handler with a newly constructed Event object instead of forwarding the actual event.

Current Behavior

const originalOnReadyStateChange = xhr.onreadystatechange;
xhr.onreadystatechange = function() {
  // ... interceptor logic ...
  
  if (originalOnReadyStateChange) {
    return originalOnReadyStateChange.call(this, new Event('readystatechange'));
  }
};

Problem

The original handler receives a synthetic new Event('readystatechange') instead of the real browser event. This means:

  • event.target will be null instead of the XHR object
  • event.currentTarget will be null
  • Any custom properties the browser adds to the event are lost
  • Code that inspects event.target to get the XHR reference will break

Suggested Fix

Capture and forward the actual event:

xhr.onreadystatechange = function(event: Event) {
  // ... interceptor logic ...
  
  if (originalOnReadyStateChange) {
    return originalOnReadyStateChange.call(this, event);
  }
};

Priority

Low-Medium — most XHR handlers use this (which is correctly bound) rather than event.target, but it's a correctness issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions