main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const { Snekfetch, TestRoot } = require('./interop');
  2. const server = require('./server');
  3. function makeTestObj({ unicode = true, numbers = false } = {}) {
  4. const test = {
  5. Hello: 'world',
  6. Test: numbers ? 1337 : '1337',
  7. };
  8. if (unicode)
  9. test.Unicode = '( ͡° ͜ʖ ͡°)';
  10. return {
  11. test,
  12. check: (obj) => {
  13. expect(obj).not.toBeUndefined();
  14. expect(obj.Hello).toBe(test.Hello);
  15. expect(obj.Test).toBe(test.Test);
  16. if (unicode)
  17. expect(obj.Unicode).toBe(test.Unicode);
  18. },
  19. };
  20. }
  21. test('should return a promise', () => {
  22. expect(Snekfetch.get(`${TestRoot}/get`).end())
  23. .toBeInstanceOf(Promise);
  24. });
  25. test('should reject with error on network failure', () => {
  26. const invalid = 'http://localhost:0/';
  27. /* https://gc.gy/❥ȗ.png
  28. return expect(Snekfetch.get(invalid).end())
  29. .rejects.toBeInstanceOf(Error);*/
  30. return Snekfetch.get(invalid).catch((err) => {
  31. expect(err.name).toMatch(/(Fetch)?Error/);
  32. });
  33. });
  34. test('should resolve on success', () =>
  35. Snekfetch.get(`${TestRoot}/get`).then((res) => {
  36. expect(res.status).toBe(200);
  37. expect(res.ok).toBe(true);
  38. expect(res).toHaveProperty('text');
  39. expect(res).toHaveProperty('body');
  40. })
  41. );
  42. test('end should work', () =>
  43. Snekfetch.get(`${TestRoot}/get`).end((err, res) => {
  44. expect(err).toBe(null);
  45. expect(res.body).not.toBeUndefined();
  46. })
  47. );
  48. test('should reject if response is not between 200 and 300', () =>
  49. Snekfetch.get(`${TestRoot}/404`).catch((err) => {
  50. expect(err.status).toBe(404);
  51. expect(err.ok).toBe(false);
  52. })
  53. );
  54. test('unzipping works', () =>
  55. Snekfetch.get(`${TestRoot}/gzip`)
  56. .then((res) => {
  57. expect(res.body).not.toBeUndefined();
  58. expect(res.body.gzipped).toBe(true);
  59. })
  60. );
  61. test('query should work', () => {
  62. const { test, check } = makeTestObj();
  63. Promise.all([
  64. Snekfetch.get(`${TestRoot}/get?inline=true`)
  65. .query(test).end(),
  66. Snekfetch.get(`${TestRoot}/get?inline=true`, { query: test })
  67. .end(),
  68. ])
  69. .then((ress) => {
  70. for (const res of ress) {
  71. const { args } = res.body;
  72. check(args);
  73. expect(args.inline).toBe('true');
  74. }
  75. });
  76. });
  77. test('headers should work', () => {
  78. const { test, check } = makeTestObj({ unicode: false });
  79. return Promise.all([
  80. Snekfetch.get(`${TestRoot}/get`)
  81. .set(test).end(),
  82. Snekfetch.get(`${TestRoot}/get`, { headers: test })
  83. .end(),
  84. ])
  85. .then((ress) => {
  86. for (const res of ress)
  87. check(res.body.headers);
  88. });
  89. });
  90. test('attach should work', () => {
  91. const { test, check } = makeTestObj();
  92. return Snekfetch.post(`${TestRoot}/post`)
  93. .attach(test)
  94. .then((res) => check(res.body.form));
  95. });
  96. test('send should work with json', () => {
  97. const { test, check } = makeTestObj({ numbers: true });
  98. return Promise.all([
  99. Snekfetch.post(`${TestRoot}/post`)
  100. .send(test).end(),
  101. Snekfetch.post(`${TestRoot}/post`, { data: test })
  102. .end(),
  103. ])
  104. .then((ress) => {
  105. for (const res of ress)
  106. check(res.body.json);
  107. });
  108. });
  109. test('send should work with urlencoded', () => {
  110. const { test, check } = makeTestObj();
  111. return Snekfetch.post(`${TestRoot}/post`)
  112. .set('content-type', 'application/x-www-form-urlencoded')
  113. .send(test)
  114. .then((res) => check(res.body.form));
  115. });
  116. test('invalid json is just text', () =>
  117. Snekfetch.get(`http://localhost:${server.port}/invalid-json`)
  118. .then((res) => {
  119. expect(res.body).toBe('{ "a": 1');
  120. })
  121. );
  122. test('x-www-form-urlencoded response body', () =>
  123. Snekfetch.get(`http://localhost:${server.port}/form-urlencoded`)
  124. .then((res) => {
  125. const { body } = res;
  126. expect(body.test).toBe('1');
  127. expect(body.hello).toBe('world');
  128. })
  129. );
  130. test('redirects', () =>
  131. Snekfetch.get(`${TestRoot}/redirect/1`)
  132. .then((res) => {
  133. expect(res.body).not.toBeUndefined();
  134. expect(res.body.url).toBe(`${TestRoot}/get`);
  135. })
  136. );