Advertisement
Guest User

redirectTo API actually plus unit tests

a guest
Dec 5th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff -r ee134c770558 netwerk/protocol/http/HttpChannelChild.cpp
  2. --- a/netwerk/protocol/http/HttpChannelChild.cpp    Wed Dec 05 12:32:09 2012 -0800
  3. +++ b/netwerk/protocol/http/HttpChannelChild.cpp    Wed Dec 05 22:00:50 2012 +0000
  4. @@ -1054,15 +1054,16 @@
  5.    URIParams uri;
  6.    SerializeURI(mURI, uri);
  7.  
  8. -  OptionalURIParams originalURI, documentURI, referrer;
  9. +  OptionalURIParams originalURI, documentURI, referrer, redirectURI;
  10.    SerializeURI(mOriginalURI, originalURI);
  11.    SerializeURI(mDocumentURI, documentURI);
  12.    SerializeURI(mReferrer, referrer);
  13. +  SerializeURI(mInternalRedirectURI, redirectURI);
  14.  
  15.    OptionalInputStreamParams uploadStream;
  16.    SerializeInputStream(mUploadStream, uploadStream);
  17.  
  18. -  SendAsyncOpen(uri, originalURI, documentURI, referrer, mLoadFlags,
  19. +  SendAsyncOpen(uri, originalURI, documentURI, referrer, redirectURI, mLoadFlags,
  20.                  mClientSetRequestHeaders, mRequestHead.Method(), uploadStream,
  21.                  mUploadStreamHasHeaders, mPriority, mRedirectionLimit,
  22.                  mAllowPipelining, mForceAllowThirdPartyCookie, mSendResumeAt,
  23. @@ -1105,6 +1106,18 @@
  24.    DROP_DEAD();
  25.  }
  26.  
  27. +NS_IMETHODIMP
  28. +HttpChannelChild::RedirectTo(nsIURI *uri)
  29. +{
  30. +  // We can only redirect unopened channels
  31. +  NS_ENSURE_TRUE(!mIPCOpen, NS_ERROR_ALREADY_OPENED);
  32. +
  33. +  // The redirect is stored internally for use in AsyncOpen
  34. +  mInternalRedirectURI = uri;
  35. +
  36. +  return NS_OK;
  37. +}
  38. +
  39.  // The next four _should_ be implemented, but we need to figure out how
  40.  // to transfer the data from the chrome process first.
  41.  
  42. diff -r ee134c770558 netwerk/protocol/http/HttpChannelChild.h
  43. --- a/netwerk/protocol/http/HttpChannelChild.h  Wed Dec 05 12:32:09 2012 -0800
  44. +++ b/netwerk/protocol/http/HttpChannelChild.h  Wed Dec 05 22:00:50 2012 +0000
  45. @@ -75,6 +75,7 @@
  46.    NS_IMETHOD GetLocalPort(int32_t* port);
  47.    NS_IMETHOD GetRemoteAddress(nsACString& addr);
  48.    NS_IMETHOD GetRemotePort(int32_t* port);
  49. +  NS_IMETHOD RedirectTo(nsIURI *uri);
  50.    // nsISupportsPriority
  51.    NS_IMETHOD SetPriority(int32_t value);
  52.    // nsIResumableChannel
  53. @@ -125,6 +126,7 @@
  54.    RequestHeaderTuples mClientSetRequestHeaders;
  55.    nsCOMPtr<nsIChildChannel> mRedirectChannelChild;
  56.    nsCOMPtr<nsISupports> mSecurityInfo;
  57. +  nsCOMPtr<nsIURI>      mInternalRedirectURI;
  58.  
  59.    bool mIsFromCache;
  60.    bool mCacheEntryAvailable;
  61. diff -r ee134c770558 netwerk/protocol/http/HttpChannelParent.cpp
  62. --- a/netwerk/protocol/http/HttpChannelParent.cpp   Wed Dec 05 12:32:09 2012 -0800
  63. +++ b/netwerk/protocol/http/HttpChannelParent.cpp   Wed Dec 05 22:00:50 2012 +0000
  64. @@ -124,6 +124,7 @@
  65.                                   const OptionalURIParams&   aOriginalURI,
  66.                                   const OptionalURIParams&   aDocURI,
  67.                                   const OptionalURIParams&   aReferrerURI,
  68. +                                 const OptionalURIParams&   aInternalRedirectURI,
  69.                                   const uint32_t&            loadFlags,
  70.                                   const RequestHeaderTuples& requestHeaders,
  71.                                   const nsHttpAtom&          requestMethod,
  72. @@ -144,6 +145,7 @@
  73.    nsCOMPtr<nsIURI> originalUri = DeserializeURI(aOriginalURI);
  74.    nsCOMPtr<nsIURI> docUri = DeserializeURI(aDocURI);
  75.    nsCOMPtr<nsIURI> referrerUri = DeserializeURI(aReferrerURI);
  76. +  nsCOMPtr<nsIURI> internalRedirectUri = DeserializeURI(aInternalRedirectURI);
  77.  
  78.    nsCString uriSpec;
  79.    uri->GetSpec(uriSpec);
  80. @@ -174,6 +176,8 @@
  81.      httpChan->SetDocumentURI(docUri);
  82.    if (referrerUri)
  83.      httpChan->SetReferrerInternal(referrerUri);
  84. +  if (internalRedirectUri)
  85. +    httpChan->RedirectTo(internalRedirectUri);
  86.    if (loadFlags != nsIRequest::LOAD_NORMAL)
  87.      httpChan->SetLoadFlags(loadFlags);
  88.  
  89. diff -r ee134c770558 netwerk/protocol/http/HttpChannelParent.h
  90. --- a/netwerk/protocol/http/HttpChannelParent.h Wed Dec 05 12:32:09 2012 -0800
  91. +++ b/netwerk/protocol/http/HttpChannelParent.h Wed Dec 05 22:00:50 2012 +0000
  92. @@ -52,6 +52,7 @@
  93.                               const OptionalURIParams&   originalUri,
  94.                               const OptionalURIParams&   docUri,
  95.                               const OptionalURIParams&   referrerUri,
  96. +                             const OptionalURIParams&   internalRedirectUri,
  97.                               const uint32_t&            loadFlags,
  98.                               const RequestHeaderTuples& requestHeaders,
  99.                               const nsHttpAtom&          requestMethod,
  100. diff -r ee134c770558 netwerk/protocol/http/PHttpChannel.ipdl
  101. --- a/netwerk/protocol/http/PHttpChannel.ipdl   Wed Dec 05 12:32:09 2012 -0800
  102. +++ b/netwerk/protocol/http/PHttpChannel.ipdl   Wed Dec 05 22:00:50 2012 +0000
  103. @@ -37,6 +37,7 @@
  104.              OptionalURIParams   original,
  105.              OptionalURIParams   doc,
  106.              OptionalURIParams   referrer,
  107. +            OptionalURIParams   internalRedirect,
  108.              uint32_t            loadFlags,
  109.              RequestHeaderTuples requestHeaders,
  110.              nsHttpAtom          requestMethod,
  111. diff -r ee134c770558 netwerk/protocol/http/nsHttpChannel.cpp
  112. --- a/netwerk/protocol/http/nsHttpChannel.cpp   Wed Dec 05 12:32:09 2012 -0800
  113. +++ b/netwerk/protocol/http/nsHttpChannel.cpp   Wed Dec 05 22:00:50 2012 +0000
  114. @@ -35,6 +35,7 @@
  115.  #include "base/compiler_specific.h"
  116.  #include "NullHttpTransaction.h"
  117.  #include "mozilla/Attributes.h"
  118. +#include "mozilla/net/HttpBaseChannel.h"
  119.  
  120.  namespace mozilla { namespace net {
  121.  
  122. @@ -1545,18 +1546,17 @@
  123.          return;
  124.      }
  125.  
  126. -    nsresult rv = AsyncRedirectChannelToHttps();
  127. +    nsresult rv = InternalRedirectChannelToHttps();
  128.      if (NS_FAILED(rv))
  129. -        ContinueAsyncRedirectChannelToHttps(rv);
  130. +        ContinueInternalRedirectChannelToURI(rv);
  131.  }
  132.  
  133.  nsresult
  134. -nsHttpChannel::AsyncRedirectChannelToHttps()
  135. +nsHttpChannel::InternalRedirectChannelToHttps()
  136.  {
  137.      nsresult rv = NS_OK;
  138.      LOG(("nsHttpChannel::HandleAsyncRedirectChannelToHttps() [STS]\n"));
  139.  
  140. -    nsCOMPtr<nsIChannel> newChannel;
  141.      nsCOMPtr<nsIURI> upgradedURI;
  142.  
  143.      rv = mURI->Clone(getter_AddRefs(upgradedURI));
  144. @@ -1578,6 +1578,48 @@
  145.      else
  146.          upgradedURI->SetPort(oldPort);
  147.  
  148. +    return InternalRedirectChannelToURI(upgradedURI);
  149. +}
  150. +
  151. +NS_IMETHODIMP
  152. +nsHttpChannel::RedirectTo(nsIURI *newURI)
  153. +{
  154. +    // We can only redirect unopened channels
  155. +    NS_ENSURE_TRUE(!mRequestObserversCalled, NS_ERROR_ALREADY_OPENED);
  156. +
  157. +    // The redirect is stored internally for use in AsyncOpen
  158. +    mInternalRedirectURI = newURI;
  159. +
  160. +    return NS_OK;
  161. +}
  162. +
  163. +void
  164. +nsHttpChannel::HandleAsyncInternalRedirect()
  165. +{
  166. +    NS_PRECONDITION(!mCallOnResume, "How did that happen?");
  167. +    NS_PRECONDITION(mInternalRedirectURI, "How did that happen?");
  168. +
  169. +    if (mSuspendCount) {
  170. +        LOG(("Waiting until resume to do async API redirect [this=%p]\n", this));
  171. +        mCallOnResume = &nsHttpChannel::HandleAsyncInternalRedirect;
  172. +        return;
  173. +    }
  174. +
  175. +    nsresult rv = InternalRedirectChannelToURI(mInternalRedirectURI);
  176. +    if (NS_FAILED(rv))
  177. +        ContinueInternalRedirectChannelToURI(rv);
  178. +
  179. +    return;
  180. +}
  181. +
  182. +nsresult
  183. +nsHttpChannel::InternalRedirectChannelToURI(nsIURI *upgradedURI)
  184. +{
  185. +    nsresult rv = NS_OK;
  186. +    LOG(("nsHttpChannel::InternalRedirectChannelToURI()\n"));
  187. +
  188. +    nsCOMPtr<nsIChannel> newChannel;
  189. +
  190.      nsCOMPtr<nsIIOService> ioService;
  191.      rv = gHttpHandler->GetIOService(getter_AddRefs(ioService));
  192.      NS_ENSURE_SUCCESS(rv, rv);
  193. @@ -1593,7 +1635,7 @@
  194.      uint32_t flags = nsIChannelEventSink::REDIRECT_PERMANENT;
  195.  
  196.      PushRedirectAsyncFunc(
  197. -        &nsHttpChannel::ContinueAsyncRedirectChannelToHttps);
  198. +        &nsHttpChannel::ContinueInternalRedirectChannelToURI);
  199.      rv = gHttpHandler->AsyncOnChannelRedirect(this, newChannel, flags);
  200.  
  201.      if (NS_SUCCEEDED(rv))
  202. @@ -1601,15 +1643,18 @@
  203.  
  204.      if (NS_FAILED(rv)) {
  205.          AutoRedirectVetoNotifier notifier(this);
  206. +
  207. +        /* Remove the async call to ContinueInternalRedirectChannelToURI().
  208. +         * It is called directly by our callers upon return (to clean up
  209. +         * the failed redirect). */
  210.          PopRedirectAsyncFunc(
  211. -            &nsHttpChannel::ContinueAsyncRedirectChannelToHttps);
  212. +            &nsHttpChannel::ContinueInternalRedirectChannelToURI);
  213.      }
  214.  
  215.      return rv;
  216.  }
  217. -
  218.  nsresult
  219. -nsHttpChannel::ContinueAsyncRedirectChannelToHttps(nsresult rv)
  220. +nsHttpChannel::ContinueInternalRedirectChannelToURI(nsresult rv)
  221.  {
  222.      if (NS_SUCCEEDED(rv))
  223.          rv = OpenRedirectChannel(rv);
  224. @@ -4337,6 +4382,12 @@
  225.      if (mLoadGroup)
  226.          mLoadGroup->AddRequest(this, nullptr);
  227.  
  228. +    // Check to see if we should redirect this channel elsewhere by
  229. +    // nsIHttpChannel.redirectTo API request
  230. +    if (mInternalRedirectURI) {
  231. +        return AsyncCall(&nsHttpChannel::HandleAsyncInternalRedirect);
  232. +    }
  233. +
  234.      // record asyncopen time unconditionally and clear it if we
  235.      // don't want it after OnModifyRequest() weighs in. But waiting for
  236.      // that to complete would mean we don't include proxy resolution in the
  237. diff -r ee134c770558 netwerk/protocol/http/nsHttpChannel.h
  238. --- a/netwerk/protocol/http/nsHttpChannel.h Wed Dec 05 12:32:09 2012 -0800
  239. +++ b/netwerk/protocol/http/nsHttpChannel.h Wed Dec 05 22:00:50 2012 +0000
  240. @@ -108,6 +108,8 @@
  241.      // nsIChannel
  242.      NS_IMETHOD GetSecurityInfo(nsISupports **aSecurityInfo);
  243.      NS_IMETHOD AsyncOpen(nsIStreamListener *listener, nsISupports *aContext);
  244. +    // nsIHttpChannel
  245. +    NS_IMETHOD RedirectTo(nsIURI *newURI);
  246.      // nsIHttpChannelInternal
  247.      NS_IMETHOD SetupFallbackChannel(const char *aFallbackKey);
  248.      // nsISupportsPriority
  249. @@ -182,12 +184,14 @@
  250.  
  251.      // redirection specific methods
  252.      void     HandleAsyncRedirect();
  253. +    void     HandleAsyncInternalRedirect();
  254.      nsresult ContinueHandleAsyncRedirect(nsresult);
  255.      void     HandleAsyncNotModified();
  256.      void     HandleAsyncFallback();
  257.      nsresult ContinueHandleAsyncFallback(nsresult);
  258.      nsresult PromptTempRedirect();
  259. -    virtual nsresult SetupReplacementChannel(nsIURI *, nsIChannel *, bool preserveMethod);
  260. +    nsresult InternalRedirectChannelToURI(nsIURI *);
  261. +    virtual  nsresult SetupReplacementChannel(nsIURI *, nsIChannel *, bool preserveMethod);
  262.  
  263.      // proxy specific methods
  264.      nsresult ProxyFailover();
  265. @@ -242,8 +246,8 @@
  266.      nsresult DoAuthRetry(nsAHttpConnection *);
  267.  
  268.      void     HandleAsyncRedirectChannelToHttps();
  269. -    nsresult AsyncRedirectChannelToHttps();
  270. -    nsresult ContinueAsyncRedirectChannelToHttps(nsresult rv);
  271. +    nsresult InternalRedirectChannelToHttps();
  272. +    nsresult ContinueInternalRedirectChannelToURI(nsresult rv);
  273.      nsresult OpenRedirectChannel(nsresult rv);
  274.  
  275.      /**
  276. @@ -323,6 +327,7 @@
  277.      friend class HttpCacheQuery;
  278.  
  279.      nsCOMPtr<nsIURI>                  mRedirectURI;
  280. +    nsCOMPtr<nsIURI>                  mInternalRedirectURI;
  281.      nsCOMPtr<nsIChannel>              mRedirectChannel;
  282.      uint32_t                          mRedirectType;
  283.  
  284. diff -r ee134c770558 netwerk/protocol/http/nsIHttpChannel.idl
  285. --- a/netwerk/protocol/http/nsIHttpChannel.idl  Wed Dec 05 12:32:09 2012 -0800
  286. +++ b/netwerk/protocol/http/nsIHttpChannel.idl  Wed Dec 05 22:00:50 2012 +0000
  287. @@ -257,4 +257,16 @@
  288.       *         has been received (before onStartRequest).
  289.       */
  290.      boolean isNoCacheResponse();
  291. +
  292. +    /**
  293. +     * Instructs the channel to immediately redirect to a new destination.
  294. +     * Can only be called on channels not yet opened.
  295. +     *
  296. +     * This method provides no explicit conflict resolution. The last
  297. +     * caller to call it wins.
  298. +     *
  299. +     * @throws NS_ERROR_ALREADY_OPENED if called after the channel
  300. +     *         has been opened.
  301. +     */
  302. +    void redirectTo(in nsIURI aNewURI);
  303.  };
  304. diff -r ee134c770558 netwerk/protocol/viewsource/nsViewSourceChannel.cpp
  305. --- a/netwerk/protocol/viewsource/nsViewSourceChannel.cpp   Wed Dec 05 12:32:09 2012 -0800
  306. +++ b/netwerk/protocol/viewsource/nsViewSourceChannel.cpp   Wed Dec 05 22:00:50 2012 +0000
  307. @@ -680,4 +680,12 @@
  308.  {
  309.      return !mHttpChannel ? NS_ERROR_NULL_POINTER :
  310.          mHttpChannel->IsNoCacheResponse(_retval);
  311. -}
  312. +}
  313. +
  314. +NS_IMETHODIMP
  315. +nsViewSourceChannel::RedirectTo(nsIURI *uri)
  316. +{
  317. +    return !mHttpChannel ? NS_ERROR_NULL_POINTER :
  318. +        mHttpChannel->RedirectTo(uri);
  319. +}
  320. +
  321. diff -r ee134c770558 netwerk/test/unit/test_redirect_from_script.js
  322. --- /dev/null   Thu Jan 01 00:00:00 1970 +0000
  323. +++ b/netwerk/test/unit/test_redirect_from_script.js    Wed Dec 05 22:00:50 2012 +0000
  324. @@ -0,0 +1,240 @@
  325. +/*
  326. + * Test whether the rewrite-requests-from-script API implemented here:
  327. + * https://bugzilla.mozilla.org/show_bug.cgi?id=765934 is functioning
  328. + * correctly
  329. + *
  330. + * The test has the following components:
  331. + *
  332. + * testViaXHR() checks that internal redirects occur correctly for requests
  333. + * made with nsIXMLHttpRequest objects.
  334. + *
  335. + * testViaAsyncOpen() checks that internal redirects occur correctly when made
  336. + * with nsIHTTPChannel.asyncOpen().
  337. + *
  338. + * Both of the above functions tests four requests, a simple case that
  339. + * redirects within a server; a second that redirects to a second webserver;
  340. + * and a third where internal script redirects in response to a server-side
  341. + * 302 redirect, and a fourth where one internal script redirects in response
  342. + * to another's redirect.  The successful redirects are confirmed by the
  343. + * presence of a custom response header.
  344. + *
  345. + */
  346. +
  347. +const Cc = Components.classes;
  348. +const Ci = Components.interfaces;
  349. +const Cu = Components.utils;
  350. +const Cr = Components.results;
  351. +
  352. +Cu.import("resource://testing-common/httpd.js");
  353. +
  354. +var httpServer = null, httpServer2 = null;
  355. +
  356. +// Test Part 1: a cross-path redirect on a single HTTP server
  357. +// http://localhost:4444/bait -> http://localhost:4444/switch
  358. +var baitPath = "/bait";
  359. +var baitURI = "http://localhost:4444" + baitPath;
  360. +var baitText = "you got the worm";
  361. +
  362. +var redirectedPath = "/switch";
  363. +var redirectedURI = "http://localhost:4444" + redirectedPath;
  364. +var redirectedText = "worms are not tasty";
  365. +
  366. +// Test Part 2: Now, a redirect to a different server
  367. +// http://localhost:4444/bait2 -> http://localhost:4445/switch
  368. +var bait2Path = "/bait2";
  369. +var bait2URI = "http://localhost:4444" + bait2Path;
  370. +var redirected2URI = "http://localhost:4445" + redirectedPath;
  371. +
  372. +// Test Part 3, begin with a serverside redirect that itself turns into an instance
  373. +// of Test Part 1
  374. +var bait3Path = "/frog";
  375. +var bait3URI = "http://localhost:4444" + bait3Path;
  376. +
  377. +// Test Part 4, begin with this client-side redirect and which then redirects
  378. +// to an instance of Test Part 1
  379. +var bait4Path = "/prince";
  380. +var bait4URI = "http://localhost:4444" + bait4Path;
  381. +
  382. +var testHeaderName = "X-Redirected-By-Script"
  383. +var testHeaderVal = "Yes indeed";
  384. +var testHeaderVal2 = "Very Yes";
  385. +
  386. +
  387. +function make_channel(url, callback, ctx) {
  388. +  var ios = Cc["@mozilla.org/network/io-service;1"].
  389. +            getService(Ci.nsIIOService);
  390. +  return ios.newChannel(url, "", null);
  391. +}
  392. +
  393. +function baitHandler(metadata, response)
  394. +{
  395. +  // Content-Type required: https://bugzilla.mozilla.org/show_bug.cgi?id=748117
  396. +  response.setHeader("Content-Type", "text/html", false);
  397. +  response.bodyOutputStream.write(baitText, baitText.length);
  398. +}
  399. +
  400. +function redirectedHandler(metadata, response)
  401. +{
  402. +  response.setHeader("Content-Type", "text/html", false);
  403. +  response.bodyOutputStream.write(redirectedText, redirectedText.length);
  404. +  response.setHeader(testHeaderName, testHeaderVal);
  405. +}
  406. +
  407. +function redirected2Handler(metadata, response)
  408. +{
  409. +  response.setHeader("Content-Type", "text/html", false);
  410. +  response.bodyOutputStream.write(redirectedText, redirectedText.length);
  411. +  response.setHeader(testHeaderName, testHeaderVal2);
  412. +}
  413. +
  414. +function bait3Handler(metadata, response)
  415. +{
  416. +  response.setHeader("Content-Type", "text/html", false);  
  417. +  response.setStatusLine(metadata.httpVersion, 302, "Found");
  418. +  response.setHeader("Location", redirectedURI);
  419. +}
  420. +
  421. +
  422. +Redirector.prototype = {
  423. +  // This class observes the an event and uses that to
  424. +  // trigger a redirectTo(uri) redirect using the new API
  425. +  // before https://bugzilla.mozilla.org/show_bug.cgi?id=800799
  426. +  // the event was http-on-opening-request; now it's http-on-opening-request
  427. +  register: function()
  428. +  {
  429. +    Cc["@mozilla.org/observer-service;1"].
  430. +      getService(Ci.nsIObserverService).
  431. +      addObserver(this, "http-on-opening-request", true);
  432. +  },
  433. +
  434. +  QueryInterface: function(iid)
  435. +  {
  436. +    if (iid.equals(Ci.nsIObserver) ||
  437. +        iid.equals(Ci.nsISupportsWeakReference) ||
  438. +        iid.equals(Ci.nsISupports))
  439. +      return this;
  440. +    throw Components.results.NS_NOINTERFACE;
  441. +  },
  442. +
  443. +  observe: function(subject, topic, data)
  444. +  {
  445. +    if (topic == "http-on-opening-request") {
  446. +      if (!(subject instanceof Ci.nsIHttpChannel))
  447. +        do_throw("http-on-opening-request observed a non-HTTP channel");
  448. +      var channel = subject.QueryInterface(Ci.nsIHttpChannel);
  449. +      var ioservice = Cc["@mozilla.org/network/io-service;1"].
  450. +                        getService(Ci.nsIIOService);
  451. +      var target = null;
  452. +      if (channel.URI.spec == baitURI)  target = redirectedURI;
  453. +      if (channel.URI.spec == bait2URI) target = redirected2URI;
  454. +      if (channel.URI.spec == bait4URI) target = baitURI;
  455. +      // if we have a target, redirect there
  456. +      if (target) {
  457. +        var tURI = ioservice.newURI(target, null, null);
  458. +        try       { channel.redirectTo(tURI); }
  459. +        catch (e) { do_throw("Exception in redirectTo " + e + "\n"); }
  460. +      }
  461. +    }
  462. +  }
  463. +}
  464. +
  465. +finished=false;
  466. +
  467. +function Redirector()
  468. +{
  469. +  this.register();
  470. +}
  471. +
  472. +function makeAsyncOpenTest(uri, verifier)
  473. +{
  474. +  // Produce a function to run an asyncOpen test
  475. +  var test = function()
  476. +  {
  477. +    var chan = make_channel(uri);
  478. +    chan.asyncOpen(new ChannelListener(verifier), null);
  479. +  };
  480. +  return test;
  481. +}
  482. +
  483. +function makeVerifier(headerValue, nextTask)
  484. +{
  485. +  // Produce a callback function which checks for the presence of headerValue,
  486. +  // and then continues to the next async test task
  487. +  var verifier = function(req, buffer)
  488. +  {
  489. +    if (!(req instanceof Ci.nsIHttpChannel))
  490. +      do_throw(req + " is not an nsIHttpChannel, catastrophe imminent!");
  491. +
  492. +    var httpChannel = req.QueryInterface(Ci.nsIHttpChannel);
  493. +    do_check_eq(httpChannel.getResponseHeader(testHeaderName), headerValue);
  494. +    do_check_eq(buffer, redirectedText);
  495. +    nextTask();
  496. +  };
  497. +  return verifier;
  498. +}
  499. +
  500. +// The tests and verifier callbacks depend on each other, and therefore need
  501. +// to be defined in the reverse of the order they are called in.  It is
  502. +// therefore best to read this stanza backwards!
  503. +// Skip test 4
  504. +//asyncVerifyCallback4 = makeVerifier     (testHeaderVal,  done);
  505. +//testViaAsyncOpen4    = makeAsyncOpenTest(bait4URI,       asyncVerifyCallback4);
  506. +//asyncVerifyCallback3 = makeVerifier     (testHeaderVal,  testViaAsyncOpen4);
  507. +asyncVerifyCallback3 = makeVerifier     (testHeaderVal,  done); // skip test 4
  508. +testViaAsyncOpen3    = makeAsyncOpenTest(bait3URI,       asyncVerifyCallback3);
  509. +asyncVerifyCallback2 = makeVerifier     (testHeaderVal2, testViaAsyncOpen3);
  510. +testViaAsyncOpen2    = makeAsyncOpenTest(bait2URI,       asyncVerifyCallback2);
  511. +asyncVerifyCallback  = makeVerifier     (testHeaderVal,  testViaAsyncOpen2);
  512. +testViaAsyncOpen     = makeAsyncOpenTest(baitURI,        asyncVerifyCallback);
  513. +
  514. +function testViaXHR()
  515. +{
  516. +  dump("Test 1");
  517. +  runXHRTest(baitURI,  testHeaderVal);
  518. +  dump("Test 2");
  519. +  runXHRTest(bait2URI, testHeaderVal2);
  520. +  dump("Test 3");
  521. +  runXHRTest(bait3URI, testHeaderVal);
  522. +function runXHRTest(uri, headerValue)
  523. +{
  524. +  // Check that making an XHR request for uri winds up redirecting to a result with the
  525. +  // appropriate headerValue
  526. +  var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"];
  527. +
  528. +  var req = xhr.createInstance(Ci.nsIXMLHttpRequest);
  529. +  req.open("GET", uri, false);
  530. +  req.send();
  531. +  do_check_eq(req.getResponseHeader(testHeaderName), headerValue);
  532. +  do_check_eq(req.response, redirectedText);
  533. +}
  534. +
  535. +function done()
  536. +{
  537. +  dump("done()");
  538. +  httpServer.stop(function () {httpServer2.stop(do_test_finished);});
  539. +}
  540. +
  541. +function run_test()
  542. +{
  543. +  httpServer = new HttpServer();
  544. +  httpServer.registerPathHandler(baitPath, baitHandler);
  545. +  httpServer.registerPathHandler(bait2Path, baitHandler);
  546. +  httpServer.registerPathHandler(bait3Path, bait3Handler);
  547. +  httpServer.registerPathHandler(bait4Path, baitHandler);
  548. +  httpServer.registerPathHandler(redirectedPath, redirectedHandler);
  549. +  httpServer.start(4444);
  550. +  httpServer2 = new HttpServer();
  551. +  httpServer2.registerPathHandler(redirectedPath, redirected2Handler);
  552. +  httpServer2.start(4445);
  553. +
  554. +  redirected = new Redirector();
  555. +
  556. +  testViaXHR();
  557. +  testViaAsyncOpen();  // will call done() asynchronously for cleanup
  558. +
  559. +  do_test_pending();
  560. +}
  561. diff -r ee134c770558 netwerk/test/unit/xpcshell.ini
  562. --- a/netwerk/test/unit/xpcshell.ini    Wed Dec 05 12:32:09 2012 -0800
  563. +++ b/netwerk/test/unit/xpcshell.ini    Wed Dec 05 22:00:50 2012 +0000
  564. @@ -174,6 +174,7 @@
  565.  [test_redirect-caching_passing.js]
  566.  [test_redirect_canceled.js]
  567.  [test_redirect_failure.js]
  568. +[test_redirect_from_script.js]
  569.  [test_redirect_passing.js]
  570.  [test_redirect_loop.js]
  571.  [test_redirect_baduri.js]
  572. diff -r ee134c770558 netwerk/test/unit_ipc/xpcshell.ini
  573. --- a/netwerk/test/unit_ipc/xpcshell.ini    Wed Dec 05 12:32:09 2012 -0800
  574. +++ b/netwerk/test/unit_ipc/xpcshell.ini    Wed Dec 05 22:00:50 2012 +0000
  575. @@ -19,6 +19,7 @@
  576.  [test_redirect-caching_passing_wrap.js]
  577.  [test_redirect_canceled_wrap.js]
  578.  [test_redirect_failure_wrap.js]
  579. +[test_redirect_from_script_wrap.js]
  580.  [test_redirect_passing_wrap.js]
  581.  [test_reentrancy_wrap.js]
  582.  [test_resumable_channel_wrap.js]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement