'use strict';var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}(); // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

var _sinon = require('sinon/pkg/sinon');var _sinon2 = _interopRequireDefault(_sinon);
var _mockedResponses = require('../../../test/mocked-responses.json');var _mockedResponses2 = _interopRequireDefault(_mockedResponses);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}var

FakeRpcServer = function () {
  function FakeRpcServer() {var _this = this;_classCallCheck(this, FakeRpcServer);this.




















    handleRequest = function (req) {
      setTimeout(function () {
        req.body = JSON.parse(req.requestBody);
        var middlewaresForMethod = _this.middlewares.
        filter(function (m) {return m.rpcMethod === req.body.method;});

        var response = middlewaresForMethod.
        map(function (m) {return m.middleware;}).
        reduce(function (replied, middleware) {
          if (replied) {
            return replied;
          }

          return middleware(req);
        }, false);

        if (!response) {
          return req.respond(405, {
            'Content-Type': 'application/json' },
          JSON.stringify({
            jsonrpc: '2.0',
            id: req.body.id,
            result: null }));

        }

        return req.respond(200, {
          'Content-Type': 'application/json' },
        JSON.stringify({
          jsonrpc: '2.0',
          id: req.body.id,
          result: response }));

      });
    };this.xhr = null;this.middlewares = [];}_createClass(FakeRpcServer, [{ key: 'start', value: function start() {var _this2 = this;this.xhr = _sinon2.default.useFakeXMLHttpRequest();this.xhr.onCreate = this.handleRequest;return function () {return _this2.xhr.restore();};} }, { key: 'simpleRpc', value: function simpleRpc(rpcMethod, result) {this.rpc(rpcMethod, function (req) {return result;});} }, { key: 'rpc', value: function rpc(rpcMethod, middleware) {this.middlewares.unshift({ rpcMethod: rpcMethod, middleware: middleware });} }]);return FakeRpcServer;}();


var fakeRpc = new FakeRpcServer();

fakeRpc.start();
_mockedResponses2.default.rpc.forEach(function (method) {return fakeRpc.simpleRpc(method.name, method.response);});

// export fakeRpc to mock stuff in tests
window.fakeRpc = fakeRpc;