|
| 1 | +import qunit from 'qunit'; |
| 2 | +import { merge } from 'lodash-es'; |
| 3 | + |
| 4 | +import { appScenarios } from './scenarios.mjs'; |
| 5 | +import emberServe from './helpers/ember-serve.mjs'; |
| 6 | +import fetch from 'node-fetch'; |
| 7 | +import loadFromFixtureData from './helpers/load-from-fixture-data.mjs'; |
| 8 | + |
| 9 | +const { module: Qmodule, test } = qunit; |
| 10 | + |
| 11 | +appScenarios |
| 12 | + .map('fastboot-location', (project) => { |
| 13 | + merge(project.files, loadFromFixtureData('fastboot-location')); |
| 14 | + |
| 15 | + project.removeDependency('ember-fetch'); |
| 16 | + }) |
| 17 | + .forEachScenario((scenario) => { |
| 18 | + Qmodule(scenario.name, function (hooks) { |
| 19 | + let app; // PreparedApp |
| 20 | + let process; |
| 21 | + |
| 22 | + hooks.before(async () => { |
| 23 | + app = await scenario.prepare(); |
| 24 | + process = await emberServe(app); |
| 25 | + }); |
| 26 | + |
| 27 | + hooks.after(() => { |
| 28 | + return process.stop(); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should NOT redirect when no transition is called', async function (assert) { |
| 32 | + const response = await fetch(`http://localhost:${process.port}/my-root/test-passed`, { |
| 33 | + redirect: 'manual', |
| 34 | + headers: { |
| 35 | + Accept: 'text/html', |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + if (response.status === 500) throw new Error(await response.text()); |
| 40 | + assert.equal(response.status, 200); |
| 41 | + |
| 42 | + assert.notOk(response.headers.has('location')); |
| 43 | + assert.ok(response.headers.has('x-fastboot-path')); |
| 44 | + assert.equal(response.headers.get('x-fastboot-path'), '/my-root/test-passed'); |
| 45 | + |
| 46 | + const bodyText = await response.text(); |
| 47 | + |
| 48 | + assert.ok(await bodyText.includes('The Test Passed!')); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should NOT redirect when intermediateTransitionTo is called', async function (assert) { |
| 52 | + const response = await fetch( |
| 53 | + `http://localhost:${process.port}/my-root/redirect-on-intermediate-transition-to`, |
| 54 | + { |
| 55 | + redirect: 'manual', |
| 56 | + headers: { |
| 57 | + Accept: 'text/html', |
| 58 | + }, |
| 59 | + } |
| 60 | + ); |
| 61 | + if (response.status === 500) throw new Error(await response.text()); |
| 62 | + assert.equal(response.status, 200); |
| 63 | + |
| 64 | + assert.notOk(response.headers.has('location')); |
| 65 | + assert.ok(response.headers.has('x-fastboot-path')); |
| 66 | + assert.equal( |
| 67 | + response.headers.get('x-fastboot-path'), |
| 68 | + '/my-root/redirect-on-intermediate-transition-to' |
| 69 | + ); |
| 70 | + |
| 71 | + const bodyText = await response.text(); |
| 72 | + |
| 73 | + assert.notOk(bodyText.includes('Welcome to Ember')); |
| 74 | + assert.notOk(bodyText.includes('The Test Passed!')); |
| 75 | + }); |
| 76 | + |
| 77 | + test('should redirect when transitionTo is called', async function (assert) { |
| 78 | + const response = await fetch( |
| 79 | + `http://localhost:${process.port}/my-root/redirect-on-transition-to`, |
| 80 | + { |
| 81 | + redirect: 'manual', |
| 82 | + headers: { |
| 83 | + Accept: 'text/html', |
| 84 | + }, |
| 85 | + } |
| 86 | + ); |
| 87 | + if (response.status === 500) throw new Error(await response.text()); |
| 88 | + assert.equal(response.status, 307); |
| 89 | + |
| 90 | + assert.ok(response.headers.has('location')); |
| 91 | + assert.ok(response.headers.has('x-fastboot-path')); |
| 92 | + assert.equal( |
| 93 | + response.headers.get('location'), |
| 94 | + `http://localhost:${process.port}/my-root/test-passed` |
| 95 | + ); |
| 96 | + assert.equal(response.headers.get(['x-fastboot-path']), '/my-root/test-passed'); |
| 97 | + |
| 98 | + const body = await response.text(); |
| 99 | + assert.ok(body.includes('Redirecting to')); |
| 100 | + assert.ok(body.includes('/my-root/test-passed')); |
| 101 | + }); |
| 102 | + |
| 103 | + test('should redirect when replaceWith is called', async function (assert) { |
| 104 | + const response = await fetch( |
| 105 | + `http://localhost:${process.port}/my-root/redirect-on-replace-with`, |
| 106 | + { |
| 107 | + redirect: 'manual', |
| 108 | + headers: { |
| 109 | + Accept: 'text/html', |
| 110 | + }, |
| 111 | + } |
| 112 | + ); |
| 113 | + if (response.status === 500) throw new Error(await response.text()); |
| 114 | + assert.equal(response.status, 307); |
| 115 | + |
| 116 | + assert.ok(response.headers.has('location')); |
| 117 | + assert.ok(response.headers.has('x-fastboot-path')); |
| 118 | + assert.equal( |
| 119 | + response.headers.get('location'), |
| 120 | + `http://localhost:${process.port}/my-root/test-passed` |
| 121 | + ); |
| 122 | + assert.equal(response.headers.get(['x-fastboot-path']), '/my-root/test-passed'); |
| 123 | + |
| 124 | + const body = await response.text(); |
| 125 | + |
| 126 | + assert.ok(body.includes('Redirecting to')); |
| 127 | + assert.ok(body.includes('/my-root/test-passed')); |
| 128 | + }); |
| 129 | + |
| 130 | + test('should NOT redirect when transitionTo is called with identical route name', async function (assert) { |
| 131 | + const response = await fetch( |
| 132 | + `http://localhost:${process.port}/my-root/noop-transition-to`, |
| 133 | + { |
| 134 | + redirect: 'manual', |
| 135 | + headers: { |
| 136 | + Accept: 'text/html', |
| 137 | + }, |
| 138 | + } |
| 139 | + ); |
| 140 | + if (response.status === 500) throw new Error(await response.text()); |
| 141 | + assert.equal(response.status, 200); |
| 142 | + |
| 143 | + assert.notOk(response.headers.has('location')); |
| 144 | + assert.ok(response.headers.has('x-fastboot-path')); |
| 145 | + assert.equal(response.headers.get(['x-fastboot-path']), '/my-root/noop-transition-to'); |
| 146 | + |
| 147 | + const body = await response.text(); |
| 148 | + |
| 149 | + assert.ok(body.includes('Redirect to self')); |
| 150 | + }); |
| 151 | + |
| 152 | + test('should NOT redirect when replaceWith is called with identical route name', async function (assert) { |
| 153 | + const response = await fetch(`http://localhost:${process.port}/my-root/noop-replace-with`, { |
| 154 | + redirect: 'manual', |
| 155 | + headers: { |
| 156 | + Accept: 'text/html', |
| 157 | + }, |
| 158 | + }); |
| 159 | + if (response.status === 500) throw new Error(await response.text()); |
| 160 | + assert.equal(response.status, 200); |
| 161 | + |
| 162 | + assert.notOk(response.headers.has('location')); |
| 163 | + assert.ok(response.headers.has('x-fastboot-path')); |
| 164 | + assert.equal(response.headers.get(['x-fastboot-path']), '/my-root/noop-replace-with'); |
| 165 | + |
| 166 | + const body = await response.text(); |
| 167 | + |
| 168 | + assert.ok(body.includes('Redirect to self')); |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
0 commit comments