Skip to main content
module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    stubby: {
      stubsServer: {
        files: [{src: ['stubs/*.json']}],
        options: {
          callback: function (server, options) {
            server.get(1, function (err, endpoint) {
              if (!err) {
                console.log(endpoint);
              }
            });
          },
          stubs: 8882,
          admin: 8889,
          mute: false
        }
      }
    },
  });

  grunt.loadNpmTasks('grunt-stubby');

  grunt.registerTask('http', 'Make an HTTP request', function() {
    var done = this.async();

    grunt.log.writeln('Requesting data.json');
    var request = require('request');
    request('http://localhost:8882/static/data.json', function(error, resp, body) {
      if (!error && resp.statusCode == 200) {
        var data = JSON.parse(body);
        grunt.log.writeln('/static/data.json was server correctly: ' + data.value);
        grunt.log.writeln('body: ' + body);
      } else {
        grunt.log.writeln('/static/data.json was not served correctly');
        grunt.log.writeln('Response Code: ' + resp.statusCode.toString());
      }
      done();
    });
  });

  grunt.registerTask('run', 'Start a stubby server and make an HTTP request', function() {
    grunt.task.run(['stubby', 'http']);
  });
};
Share