How to use the sinon.fakeServer function in sinon

To help you get started, we’ve selected a few sinon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DigitalFlow / Xrm-WebApi-Client / src / spec / WebApiClientSpec.js View on Github external
beforeEach(function() {
        account = {
            Name: "Adventure Works"
        };

        contact = {
            FirstName: "Joe"
        };

        WebApiClient.Configure(defaults);

        xhr = sinon.fakeServer.create();
        xhr.autoRespond = true;

        // Respond to Create Request for account with No-Content response and created entity url in header
        var createAccountUrl = new RegExp(RegExp.escape(fakeUrl + "/api/data/v8.0/accounts"));
        xhr.respondWith("POST", createAccountUrl,
            [204, { "Content-Type": "application/json", "OData-EntityId": "Fake-Account-Url" }, JSON.stringify(successMock)]
        );

        // Respond to create request with return=representation
        var createLeadUrlRepresentation = new RegExp(RegExp.escape(fakeUrl + "/api/data/v8.0/leads"));
        xhr.respondWith("POST", createLeadUrlRepresentation,
            [201, { "Content-Type": "application/json" }, JSON.stringify(account)]
        );

        // Respond to Retrieve by id Request for account
        var retrieveAccountByIdUrl = RegExp.escape(fakeUrl + "/api/data/v8.0/accounts(00000000-0000-0000-0000-000000000001)");
github elastic / kibana / x-pack / legacy / plugins / cross_cluster_replication / __jest__ / client_integration / helpers / http_requests.js View on Github external
export const init = () => {
  const server = sinon.fakeServer.create();
  server.respondImmediately = true;

  // We make requests to APIs which don't impact the UX, e.g. UI metric telemetry,
  // and we can mock them all with a 200 instead of mocking each one individually.
  server.respondWith([200, {}, '']);

  return {
    server,
    httpRequestsMockHelpers: registerHttpRequestMockHelpers(server),
  };
};
github mapbox / mapbox-directions.js / test / directions.js View on Github external
u.test("decodes polyline geometries", function (v) {
            var server = sinon.fakeServer.create();
            var directions = L.mapbox.directions({accessToken: 'key'});

            directions.on('load', function (e) {
                v.deepEqual(e.routes[0].geometry, {
                    type: 'LineString',
                    coordinates: [[-120.2, 38.5], [-120.95, 40.7], [-126.453, 43.252]]
                });
                v.end();
                server.restore();
            });

            directions
                .setOrigin(L.latLng(1, 2))
                .setDestination(L.latLng(3, 4))
                .query();
github TTLabs / EvaporateJS / test / request_headers.spec.js View on Github external
test.before(() => {
  sinon.xhr.supportsCORS = true
  server = sinon.fakeServer.create({
    respondImmediately: true
  })

  server.respondWith('POST', /^.*\?uploads.*$/, (xhr) => {
    requestHeaders.initiate = xhr.requestHeaders
    xhr.respond(200, CONTENT_TYPE_XML, initResponse(AWS_BUCKET, AWS_UPLOAD_KEY))
  })
  server.respondWith('PUT', /^.*$/, (xhr) => {
    requestHeaders.upload = xhr.requestHeaders
    xhr.respond(200)
  })

  server.respondWith('POST', /.*\?uploadId.*$/, (xhr) => {
    requestHeaders.complete = xhr.requestHeaders
    xhr.respond(200, CONTENT_TYPE_XML, completeResponse(AWS_BUCKET, AWS_UPLOAD_KEY))
  })
github TTLabs / EvaporateJS / test / aws_url.spec.js View on Github external
test.before(() => {
  sinon.xhr.supportsCORS = true
  server = serverCommonCase()

  global.XMLHttpRequest = sinon.fakeServer.xhr
})
github TTLabs / EvaporateJS / test / retry.spec.js View on Github external
test.beforeEach((t) => {
  t.context.requestedAwsObjectKey = randomAwsKey()

  t.context.baseAddConfig = {
    name: t.context.requestedAwsObjectKey,
    file: new File({
      path: '/tmp/file',
      size: 6000000,
      name: randomAwsKey()
    })
  }

  t.context.cryptoMd5 = sinon.spy(function (data) { return 'md5Checksum'; })

  headStatus = 200
  server = sinon.fakeServer.create({
    respondImmediately: true
  })

  headersForMethod = function(method, urlRegex) {
    var r = urlRegex || /./
    for (var i = 0; i < server.requests.length; i++) {
      var xhr = server.requests[i]
      if (xhr.method === method && xhr.url.match(r)) {
        return xhr.requestHeaders
      }
    }
    return {}
  }

  t.context.testBase = async function (addConfig, evapConfig) {
    t.context.deferred = defer();
github microfeedback / microfeedback-button / test / test-microfeedback-button.js View on Github external
test.cb('sends request to URL', t => {
  const server = sinon.fakeServer.create();
  const url = 'http://test.test/';
  const btn = new MicroFeedbackButton({url});
  const response = {
    backend: {name: 'github', version: '1.2.3'},
    result: {},
  };
  server.respondWith('POST', url, [
    201,
    {'Content-Type': 'application/json'},
    JSON.stringify(response),
  ]);
  syn.click(btn.el, () => {
    const input = $('.swal2-textarea');
    syn.type(input, 'foo bar baz', () => {
      const submit = $('button.swal2-confirm');
      syn.click(submit).delay(() => {
github braintree / braintree-web / test / client / unit / request / ajax-driver.js View on Github external
it.skip('accepts an ajax timeout value which will terminate the request if it is not completed', function (done) {
    this.server.restore();
    this.server = sinon.fakeServer.create();
    this.server.respondWith([200, {}, '']);

    AJAXDriver.request({
      url: TEST_SERVER_URL,
      timeout: 50,
      graphQL: this.fakeGraphQL,
      metadata: this.fakeMetadata
    }, function callback(err) {
      expect(err).to.not.eql(null);
      done();
    });

    setTimeout(function () {
      this.server.respond();
    }.bind(this), 1000);
  });
github haydn / json-api-store / spec / adapters / ajax / create-spec.js View on Github external
test("create must post a resource to the server and add it to the store on success", function (t) {
  var server = sinon.fakeServer.create({ autoRespond: false });
  var adapter = new Store.AjaxAdapter();
  var store = new Store(adapter);
  t.plan(3);
  t.timeoutAfter(1000);
  store.define("products", {
    title: Store.attr()
  });
  server.respondWith("POST", "/products", function (request) {
    t.deepEqual(JSON.parse(request.requestBody), {
      data: {
        type: "products",
        attributes: {
          title: "My Book"
        },
        relationships: {}
      }
github influxdata / influxdb / ui / spec / retention_policies / containers / RetentionPoliciesAppSpec.js View on Github external
  before(() => { server = sinon.fakeServer.create(); });
  after(() =>  { server.restore() });