var smock = require('smocks');

/**
 * Create a route definition that will keep track of (in the state) the requests that are called for all variants
 * http://localhost:8000/api/history
 */
smock.route('/api/history')
  // not necessary since default is GET but just to be explicit
  .method('GET')

  // add a route config (admin page input field) that we will use to indicate an attribute name in the response payload
  // this is really just a silly example used to demonstrate the usage of config values
  .config({
    // "attributeName" is arbitrary - it just represents the config key that the user input will be saved with
    attributeName: {
      label: 'What attribute name should I use to demonstrate state?',
      type: 'text',
      defaultValue: 'history'
    }
  })

  // add 3 different variants which will push a token to a "history" array that we store in state.
  // take a look at http://localhost:8000/api/foo multiple times to see the history grow
  // the reset the state in the admin panel (top button) and see the history go away
  .variant('scenario1').onRequest(historyScenario('scenario1'))
  .variant('scenario2').onRequest(historyScenario('scenario2'))
  .variant('scenario3').onRequest(historyScenario('scenario3'))


/**
 * Create a route definition that will return a hello world-ish payload.  This demonstrates variables in a route definition.
 * http://localhost:8000/hello/whatever
 */
.route('/api/hello/{message}')
  .onRequest(function(request, reply) {
    // this is the simplest route handler we can have because we are using the default method (GET)
    // and the default variant id ("default") for this route
    reply({hello: request.params.message});
  })


/**
 * You can also easily respond with files - and even use dynamic route variables using "withFile".
 * Remember that ./ will point to the root of your project rather than the current directory (which in this case will be the same).
 * To test this route, uncomment and add a file called "bar.json" in the root directory what some content.
 * Then browse to http://localhost:8000/foo/bar
 */
// .route('/foo/{something}').withFile('./{something}.json')


/**
 * Global variants (can be associated with any route definitions)
 */
.global()

  // simulate a back-end server error
  .variant('500').onRequest(function(request, reply) {
    reply({code: 'BAD_NEWS', message: 'Something bad happened'}).code(500);
  })

  // wait 3 seconds and then simulate a back-end timeout with a 504
  .variant('timeout').onRequest(function(request, reply) {
    setTimeout(function() {
      reply({code: 'TIMEOUT', message: 'Gateway timeout'}).code(504);
    }, 3000);
  })

.start({
    host: 'localhost',
    port: 8000
});


/**
 * Return a request handler scoped to a specific scenario name
 *
 * We will simply rstore the user's scenario request history (can be changed by altering the variants in the admin panel).
 */
function historyScenario(scenarioName) {

  // return the actual request handler (variant)
  return function(request, reply) {
    // add a state entry to indicate that we've seen "scenario1"
    // state values are meaningful because the user can reset the state to go back to a fresh start
    var history = this.state('history') || [];
    this.state('history', history);
    history.push(scenarioName);

    // get the user config attribute which we'll use to construct the payload
    // config values are meaningful because the user can control them using the admin panel
    var attributeName = this.config('attributeName');

    // not return the view history
    var payload = {};
    payload[attributeName] = history;
    reply(payload);
  };
}

@@ -1662,16 +1662,17 @@
  reply(%7B
+ 
 hello: r
@@ -1692,16 +1692,17 @@
 .message
+ 
 %7D);%0A  %7D)
@@ -2341,32 +2341,33 @@
 ly) %7B%0A    reply(
+ 
 %7Bcode: 'BAD_NEWS
@@ -2403,16 +2403,17 @@
 ppened'%7D
+ 
 ).code(5
@@ -2590,16 +2590,17 @@
   reply(
+ 
 %7Bcode: '
@@ -2636,16 +2636,17 @@
 imeout'%7D
+ 
 ).code(5

update example