1

I'm trying to test the behavior of my site when scripts take too long to load. I know how to completely block the request so that it returns a 404 by adding something to the host file, but not sure how to force the request to just not complete.

Example,

127.0.0.1 translate.google.com # this blocks google translate from loading (404)

Instead, I'd like to make translate.google.com not respond for x seconds.

I'm using requirejs to load my scripts and I'm noticing that timeout behaves differently than 404.

I added a module to allow google translate to be optionally required, and when I block it via host file, the site behaves correctly. When the script times out, the site fails to load.

// http://stackoverflow.com/questions/14164610/requirejs-optional-dependency

define("optional", [], {
    load: function (moduleName, parentRequire, onload, config) {
        var onLoadSuccess = function (moduleInstance) {
            // Module successfully loaded, call the onload callback so that
            // requirejs can work its internal magic.
            onload(moduleInstance);
        }

        var onLoadFailure = function (err) {
            // optional module failed to load.
            var failedId = err.requireModules && err.requireModules[0];
            console.warn("Could not load optional module: " + failedId);

            // Undefine the module to cleanup internal stuff in requireJS
            requirejs.undef(failedId);

            // Now define the module instance as a simple empty object
            // (NOTE: you can return any other value you want here)
            define(failedId, [], function () { return {}; });

            // Now require the module make sure that requireJS thinks 
            // that is it loaded. Since we've just defined it, requirejs 
            // will not attempt to download any more script files and
            // will just call the onLoadSuccess handler immediately
            parentRequire([failedId], onLoadSuccess);
        }

        parentRequire([moduleName], onLoadSuccess, onLoadFailure);
    }
});
MPavlak
  • 2,133
  • 1
  • 23
  • 38
  • Could you elaborate what you are trying to test? You could simply delay calling the `onload` method in your `onLoadSuccess` callback handler using something like `setTimeout(() => onload(moduleInstance), 4 * 1000)` for a 4s delay. – alpeware May 05 '17 at 14:34
  • Trying to test what happens when a script timesout. when it returns 404, it behaves correctly, when it timesout, it does not behave correctly. – MPavlak May 05 '17 at 15:21

1 Answers1

0

You need to test it with a website you have control over. Simplest solution is to use your own localhost with a little script that waits for several seconds.

A short PHP example to wait for 30 seconds would look like:

<?php
  sleep(30);
  echo 'DONE';

Just increase the seconds to sleep until your application has a timeout.

Here is another solution in Node.js

UPDATE:

Next step is to redirect the url you want to timeout in your host file (as in your question):

127.0.0.1 translate.google.com

Run a local Apache server (127.0.0.1 is standard for localhost) and put the following .htaccess file in your apache root (normally "htdocs"):

DirectoryIndex index.php
RewriteEngine on
RewriteRule ^(.*)$ index.php [QSA,L]

Then just put the PHP example from above in a file named index.php in your root (normally "htdocs").

Then your local server should respond to the call and you can adjust the response time until you have your timeout.

Community
  • 1
  • 1
dcman
  • 91
  • 1
  • 7
  • Technically, I could do this. However, I would not prefer to move all the external scripts into my project just to test what happens when they fail to respond. – MPavlak May 05 '17 at 15:22
  • As long as your application runs in your browser you don't have to. You can adjust your local host file so that the desired request is redirected to your localhost (127.0.0.1). – dcman May 05 '17 at 15:41
  • pointing local for scripts which i'm not serving returns a 404. it doesn't hang – MPavlak May 05 '17 at 15:43
  • OK, I updated my answer how you can avoid the 404 and serve your local timeout script instead. Hope it helps. – dcman May 05 '17 at 16:15
  • I do believe this will work. However, I'm using C# and asp.net not php and apache. do you know to make this work using something more friendly to that stack? – MPavlak May 05 '17 at 16:17
  • I am no expert for C# and IIS configuration. But it should be the same principle: Configure your local IIS so it serves your request with a local script. And then put a local script there that produces the timeout. – dcman May 05 '17 at 16:21
  • Yep, I get the principle, just not positive how to do it. If no one volunteers an IIS based answer I'll give you the check. – MPavlak May 05 '17 at 17:04