<commit_before>// Integration - test the router within the whole server functionality
const server = require('server');
const run = require('server/test/run');
const { get, post, put, del, sub, error } = server.router;



// Mock middlewares and data:
const question = { answer: 42 };
const mirror = ctx => ctx.data;
const hello = () => 'Hello 世界';
const throwError = () => {
  const err = new Error('MockError');
  err.code = 'test';
  throw err;
};



// CSRF validation is checked in another place; disable it for these tests
run.options = { security: false };

describe('Basic router types', () => {
  it('can do a GET request', async () => {
    const mid = get('/', hello);

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  it('can do a POST request', async () => {
    const mid = post('/', ctx => ctx.data);

    const res = await run(mid).post('/', { body: question });
    expect(res.body).toEqual({ answer: 42 });
    expect(res.status).toBe(200);
  });

  it('can do a PUT request', async () => {
    const mid = post('/', ctx => ctx.data);

    const res = await run(mid).post('/', { body: question });
    expect(res.body).toEqual({ answer: 42 });
    expect(res.status).toBe(200);
  });

  it('can do a DELETE request', async () => {
    const mid = del('/', ctx => 'Hello 世界');

    const res = await run(mid).del('/', { body: question });
    expect(res.body).toEqual('Hello 世界');
    expect(res.status).toBe(200);
  });
});


describe('Generic paths', () => {
  it('can do a GET request', async () => {
    const mid = get(hello);

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  it('can do a GET request', async () => {
    const mid = get('*', hello);

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  // it('can do a POST request', async () => {
  //   const mid = post('/', ctx => ctx.data);
  //
  //   const res = await run(mid).post('/', { body: question });
  //   expect(res.body).toEqual({ answer: 42 });
  //   expect(res.status).toBe(200);
  // });
  //
  // it('can do a PUT request', async () => {
  //   const mid = post('/', ctx => ctx.data);
  //
  //   const res = await run(mid).post('/', { body: question });
  //   expect(res.body).toEqual({ answer: 42 });
  //   expect(res.status).toBe(200);
  // });
  //
  // it('can do a DELETE request', async () => {
  //   const mid = del('/', ctx => 'Hello 世界');
  //
  //   const res = await run(mid).del('/', { body: question });
  //   expect(res.body).toEqual('Hello 世界');
  //   expect(res.status).toBe(200);
  // });
});


describe('Subdomain router', () => {
  it('can do a request to a subdomain', async () => {
    const mid = sub('api', get('/', hello));

    const res = await run((ctx) => {
      ctx.headers.host = 'api.example.com';
    }, mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  it('can handle regex', async () => {
    const mid = sub(/^api$/, get('/', hello));

    const res = await run((ctx) => {
      ctx.headers.host = 'api.example.com';
    }, mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  it('does not do partial match', async () => {
    const mid = sub(/^api$/, get('/', hello));

    const res = await run((ctx) => {
      ctx.headers.host = 'bla.api.example.com';
    }, mid, () => 'Did not match').get('/');
    expect(res).toMatchObject({ status: 200, body: 'Did not match' });
  });

  it('can do a request to a multi-level subdomain', async () => {
    const mid = sub('api.local', get('/', hello));

    const res = await run((ctx) => {
      ctx.headers.host = 'api.local.example.com';
    }, mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });
});


describe('Ends where it should end', () => {

  it('uses the matching method', async () => {
    const mid = [
      post('/', throwError),
      put('/', throwError),
      del('/', throwError),
      get('/', hello)
    ];

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });


  it('uses the matching path', async () => {
    const mid = [
      get('/bla', throwError),
      get('/:id', throwError),
      get('/', hello)
    ];

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });


  it('uses a route only once', async () => {
    const mid = [
      get('/', hello),
      get('/', throwError)
    ];

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });


  it('parses params correctly', async () => {
    const mid = get('/:id', ctx => ctx.params.id);

    const res = await run(mid).get('/42?ignored=true');
    expect(res.body).toBe('42');
  });

  // A bug shifted the router's middleware on each request so now we test for
  // multiple request to make sure the middleware remains the same
  it('does not modify the router', async () => {
    const inst = run(get('/', hello)).alive(async api => {
      for (let url of [0, 1, 2]) {
        const res = await api.get('/');
        expect(res.body).toBe('Hello 世界');
      }
    });<commit_msg>Fixed tests<commit_after>// Integration - test the router within the whole server functionality
const server = require('server');
const run = require('server/test/run');
const { get, post, put, del, sub, error } = server.router;



// Mock middlewares and data:
const question = { answer: 42 };
const mirror = ctx => ctx.data;
const hello = () => 'Hello 世界';
const throwError = () => {
  const err = new Error('MockError');
  err.code = 'test';
  throw err;
};



// CSRF validation is checked in another place; disable it for these tests
run.options = { security: false };

describe('Basic router types', () => {
  it('can do a GET request', async () => {
    const mid = get('/', hello);

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  it('can do a POST request', async () => {
    const mid = post('/', ctx => ctx.data);

    const res = await run(mid).post('/', { body: question });
    expect(res.body).toEqual({ answer: 42 });
    expect(res.status).toBe(200);
  });

  it('can do a PUT request', async () => {
    const mid = post('/', ctx => ctx.data);

    const res = await run(mid).post('/', { body: question });
    expect(res.body).toEqual({ answer: 42 });
    expect(res.status).toBe(200);
  });

  it('can do a DELETE request', async () => {
    const mid = del('/', ctx => 'Hello 世界');

    const res = await run(mid).del('/', { body: question });
    expect(res.body).toEqual('Hello 世界');
    expect(res.status).toBe(200);
  });
});


describe('Generic paths', () => {
  it('can do a GET request', async () => {
    const mid = get(hello);

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  it('can do a GET request', async () => {
    const mid = get('*', hello);

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  // it('can do a POST request', async () => {
  //   const mid = post('/', ctx => ctx.data);
  //
  //   const res = await run(mid).post('/', { body: question });
  //   expect(res.body).toEqual({ answer: 42 });
  //   expect(res.status).toBe(200);
  // });
  //
  // it('can do a PUT request', async () => {
  //   const mid = post('/', ctx => ctx.data);
  //
  //   const res = await run(mid).post('/', { body: question });
  //   expect(res.body).toEqual({ answer: 42 });
  //   expect(res.status).toBe(200);
  // });
  //
  // it('can do a DELETE request', async () => {
  //   const mid = del('/', ctx => 'Hello 世界');
  //
  //   const res = await run(mid).del('/', { body: question });
  //   expect(res.body).toEqual('Hello 世界');
  //   expect(res.status).toBe(200);
  // });
});


describe('Subdomain router', () => {
  it('can do a request to a subdomain', async () => {
    const mid = sub('api', get('/', hello));

    const res = await run((ctx) => {
      ctx.headers.host = 'api.example.com';
    }, mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  it('can handle regex', async () => {
    const mid = sub(/^api$/, get('/', hello));

    const res = await run((ctx) => {
      ctx.headers.host = 'api.example.com';
    }, mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });

  it('does not do partial match', async () => {
    const mid = sub(/^api$/, get('/', hello));

    const res = await run((ctx) => {
      ctx.headers.host = 'bla.api.example.com';
    }, mid, () => 'Did not match').get('/');
    expect(res).toMatchObject({ status: 200, body: 'Did not match' });
  });

  it('can do a request to a multi-level subdomain', async () => {
    const mid = sub('api.local', get('/', hello));

    const res = await run((ctx) => {
      ctx.headers.host = 'api.local.example.com';
    }, mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });
});


describe('Ends where it should end', () => {

  it('uses the matching method', async () => {
    const mid = [
      post('/', throwError),
      put('/', throwError),
      del('/', throwError),
      get('/', hello)
    ];

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });


  it('uses the matching path', async () => {
    const mid = [
      get('/bla', throwError),
      get('/:id', throwError),
      get('/', hello)
    ];

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });


  it('uses a route only once', async () => {
    const mid = [
      get('/', hello),
      get('/', throwError)
    ];

    const res = await run(mid).get('/');
    expect(res).toMatchObject({ status: 200, body: 'Hello 世界' });
  });


  it('parses params correctly', async () => {
    const mid = get('/:id', ctx => ctx.params.id);

    const res = await run(mid).get('/42?ignored=true');
    expect(res.body).toBe('42');
  });

  // A bug shifted the router's middleware on each request so now we test for
  // multiple request to make sure the middleware remains the same
  it('does not modify the router', async () => {
    await run(get('/', hello)).alive(async api => {
      for (let url of [0, 1, 2]) {
        const res = await api.get('/');
        expect(res.body).toBe('Hello 世界');
      }
    });
{'fraction_non_alphanumeric': 0.2187939604238957, 'fraction_numerical': 0.012191690893744726, 'mean_word_length': 2.74438202247191, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3365515', 'n_tokens_mistral': 3672, 'n_tokens_neox': 3456, 'n_words': 994}