api-net-custom-protocols-spec.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { net, protocol } from 'electron/main';
  2. import { expect } from 'chai';
  3. import * as path from 'node:path';
  4. import * as url from 'node:url';
  5. import { defer } from './lib/spec-helpers';
  6. describe('net module custom protocols', () => {
  7. it('can request file:// URLs', async () => {
  8. const resp = await net.fetch(url.pathToFileURL(path.join(__dirname, 'fixtures', 'hello.txt')).toString());
  9. expect(resp.ok).to.be.true();
  10. // trimRight instead of asserting the whole string to avoid line ending shenanigans on WOA
  11. expect((await resp.text()).trimRight()).to.equal('hello world');
  12. });
  13. it('can make requests to custom protocols', async () => {
  14. protocol.registerStringProtocol('electron-test', (req, cb) => { cb('hello ' + req.url); });
  15. defer(() => {
  16. protocol.unregisterProtocol('electron-test');
  17. });
  18. const body = await net.fetch('electron-test://foo').then(r => r.text());
  19. expect(body).to.equal('hello electron-test://foo');
  20. });
  21. it('runs through intercept handlers', async () => {
  22. protocol.interceptStringProtocol('http', (req, cb) => { cb('hello ' + req.url); });
  23. defer(() => {
  24. protocol.uninterceptProtocol('http');
  25. });
  26. const body = await net.fetch('http://foo').then(r => r.text());
  27. expect(body).to.equal('hello http://foo/');
  28. });
  29. it('file: runs through intercept handlers', async () => {
  30. protocol.interceptStringProtocol('file', (req, cb) => { cb('hello ' + req.url); });
  31. defer(() => {
  32. protocol.uninterceptProtocol('file');
  33. });
  34. const body = await net.fetch('file://foo').then(r => r.text());
  35. expect(body).to.equal('hello file://foo/');
  36. });
  37. it('can be redirected', async () => {
  38. protocol.interceptStringProtocol('file', (req, cb) => { cb({ statusCode: 302, headers: { location: 'electron-test://bar' } }); });
  39. defer(() => {
  40. protocol.uninterceptProtocol('file');
  41. });
  42. protocol.registerStringProtocol('electron-test', (req, cb) => { cb('hello ' + req.url); });
  43. defer(() => {
  44. protocol.unregisterProtocol('electron-test');
  45. });
  46. const body = await net.fetch('file://foo').then(r => r.text());
  47. expect(body).to.equal('hello electron-test://bar');
  48. });
  49. it('should not follow redirect when redirect: error', async () => {
  50. protocol.registerStringProtocol('electron-test', (req, cb) => {
  51. if (/redirect/.test(req.url)) return cb({ statusCode: 302, headers: { location: 'electron-test://bar' } });
  52. cb('hello ' + req.url);
  53. });
  54. defer(() => {
  55. protocol.unregisterProtocol('electron-test');
  56. });
  57. await expect(net.fetch('electron-test://redirect', { redirect: 'error' })).to.eventually.be.rejectedWith('Attempted to redirect, but redirect policy was \'error\'');
  58. });
  59. it('a 307 redirected POST request preserves the body', async () => {
  60. const bodyData = 'Hello World!';
  61. let postedBodyData: any;
  62. protocol.registerStringProtocol('electron-test', async (req, cb) => {
  63. if (/redirect/.test(req.url)) return cb({ statusCode: 307, headers: { location: 'electron-test://bar' } });
  64. postedBodyData = req.uploadData![0].bytes.toString();
  65. cb('hello ' + req.url);
  66. });
  67. defer(() => {
  68. protocol.unregisterProtocol('electron-test');
  69. });
  70. const response = await net.fetch('electron-test://redirect', {
  71. method: 'POST',
  72. body: bodyData
  73. });
  74. expect(response.status).to.equal(200);
  75. await response.text();
  76. expect(postedBodyData).to.equal(bodyData);
  77. });
  78. });