Closed Bug 1176092 Opened 4 years ago Closed 4 years ago

Test that redirectUrl works as documented in the WebRequest API

Categories

(WebExtensions :: Untriaged, defect)

defect
Not set

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: wbamberg, Assigned: mao)

References

Details

(Whiteboard: [webRequest])

The Chrome docs say that you can redirect web requests from onBeforeRequest and onHeadersReceived event listeners only[0].

In Firefox, it looks like we can redirect from neither of these listeners, but can redirect from onBeforeSendHeaders. To see this:

* run the linked script[1] in Scratchpad's browser context, then:

* call listen(WebRequest.onBeforeRequest) in the Browser Console
* load https://developer.mozilla.org/en-US/docs/Tools/Web_Console
-> observe that images *are not* replaced
* call unlisten(WebRequest.onBeforeRequest) in the Browser Console

* call listen(WebRequest.onBeforeSendHeaders) in the Browser Console
* load https://developer.mozilla.org/en-US/docs/Tools/Web_Console
-> observe that images are replaced
* call unlisten(WebRequest.onBeforeSendHeaders) in the Browser Console

* call listen(WebRequest.onHeadersReceived) in the Browser Console
* load https://developer.mozilla.org/en-US/docs/Tools/Web_Console
-> observe that images *are not* replaced

[0] https://developer.chrome.com/extensions/webRequest#type-BlockingResponse
[1] https://gist.github.com/wbamberg/fd02ea935eb6ca051fa8
Component: General → WebExtensions
Product: Firefox → Toolkit
This has be fixed by bug 1163862 (for onBeforeRequest) and bug 1231512 (for onHeadersReceived).

The test case, before of incompatible changes in the web page and in WebRequest, needs to open https://maone.net/ with the code updated as follows:
<scratchpad>
var {WebRequest} = Cu.import("resource://gre/modules/WebRequest.jsm", {});
var {MatchPattern} = Cu.import("resource://gre/modules/MatchPattern.jsm", {});
{
  let  pattern = "https://maone.net/*";

  function listen(event) {
    event.addListener(redirect, {urls: new MatchPattern([pattern]), types: ["image"]},
                                                   ["blocking"]);
  }

  function redirect(e) {
    console.log("Redirecting: " + e.url);
    return {redirectUrl: "https://38.media.tumblr.com/tumblr_ldbj01lZiP1qe0eclo1_500.gif"};
  } 

  function unlisten(event) {
    event.removeListener(redirect);
  }
}
</scratchpad>

However I'm keeping this open and assigned to me because it lacks tests, which I'm going to add on the WebExtensions front-end side.
Assignee: nobody → g.maone
Blocks: 1236122
Depends on: 1163862, 1231512
Whiteboard: [webRequest]
Summary: redirectUrl in WebRequest.jsm does not work as expected → Test that redirectUrl works as documented in the WebRequest API
Status: NEW → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Product: Toolkit → WebExtensions
You need to log in before you can comment on or make changes to this bug.