Skip to content

Commit

Permalink
Merge pull request #1991 from GoogleChrome/migrate-range-requests-tests
Browse files Browse the repository at this point in the history
Migrate range requests tests to run in a SW environment
  • Loading branch information
philipwalton committed Mar 28, 2019
2 parents f327ba0 + 9fc84a2 commit cb2eac0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 55 deletions.
Expand Up @@ -7,17 +7,27 @@
*/

const expect = require('chai').expect;

const activateAndControlSW = require('../../../infra/testing/activate-and-control');
const cleanSWEnv = require('../../../infra/testing/clean-sw');
const {runUnitTests} = require('../../../infra/testing/webdriver/runUnitTests');


// Store local references of these globals.
const {webdriver, server} = global.__workbox;

describe(`[workbox-range-requests]`, function() {
it(`passes all SW unit tests`, async function() {
await runUnitTests('/test/workbox-range-requests/sw/');
});
});

describe(`rangeRequests.Plugin`, function() {
const testServerAddress = global.__workbox.server.getAddress();
describe(`[workbox-range-requests] Plugin`, function() {
const testServerAddress = server.getAddress();
const testingURL = `${testServerAddress}/test/workbox-range-requests/static/`;

beforeEach(async function() {
// Navigate to our test page and clear all caches before this test runs.
await cleanSWEnv(global.__workbox.webdriver, testingURL);
await cleanSWEnv(webdriver, testingURL);
});

it(`should return a partial response that satisfies the request's Range: header, and an error response when it can't be satisfied`, async function() {
Expand All @@ -26,7 +36,7 @@ describe(`rangeRequests.Plugin`, function() {

const dummyURL = `this-file-doesnt-exist.txt`;

const partialResponseBody = await global.__workbox.webdriver.executeAsyncScript((dummyURL, cb) => {
const partialResponseBody = await webdriver.executeAsyncScript((dummyURL, cb) => {
// Prime the cache, and then make the Range: request.
fetch(new Request(dummyURL, {headers: {Range: `bytes=5-6`}}))
.then((response) => response.text())
Expand All @@ -38,7 +48,7 @@ describe(`rangeRequests.Plugin`, function() {
// 11 characters returned in the partial response.
expect(partialResponseBody).to.eql('56');

const errorResponseStatus = await global.__workbox.webdriver.executeAsyncScript((dummyURL, cb) => {
const errorResponseStatus = await webdriver.executeAsyncScript((dummyURL, cb) => {
// These are arbitrary large values that extend past the end of the file.
fetch(new Request(dummyURL, {headers: {Range: `bytes=100-101`}}))
.then((response) => cb(response.status));
Expand Down
21 changes: 0 additions & 21 deletions test/workbox-range-requests/node/exports.mjs

This file was deleted.

Expand Up @@ -6,11 +6,10 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import {Plugin} from 'workbox-range-requests/Plugin.mjs';

import {Plugin} from '../../../packages/workbox-range-requests/Plugin.mjs';

describe(`[workbox-range-requests] Plugin`, function() {
describe(`Plugin`, function() {
it(`should construct with no values`, function() {
new Plugin();
});
Expand Down
Expand Up @@ -6,11 +6,10 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import {createPartialResponse} from 'workbox-range-requests/createPartialResponse.mjs';

import {createPartialResponse} from '../../../packages/workbox-range-requests/createPartialResponse.mjs';

describe(`[workbox-range-requests] createPartialResponse`, function() {
describe(`createPartialResponse()`, function() {
// This uses an interface that matches what our Blob mock currently supports.
// It's *not* the same way we'd use native browser implementation.
function constructBlob(length) {
Expand All @@ -35,37 +34,39 @@ describe(`[workbox-range-requests] createPartialResponse`, function() {

it(`should return a Response with status 416 when the 'request' parameter isn't valid`, async function() {
const response = await createPartialResponse(null, VALID_RESPONSE);
expect(response.status).to.eql(416);
expect(response.status).to.equal(416);
});

it(`should return a Response with status 416 when the 'response' parameter isn't valid`, async function() {
const response = await createPartialResponse(VALID_REQUEST, null);
expect(response.status).to.eql(416);
expect(response.status).to.equal(416);
});

it(`should return a Response with status 416 when there's no Range: header in the request`, async function() {
const noRangeHeaderRequest = new Request('/');
const response = await createPartialResponse(noRangeHeaderRequest, VALID_RESPONSE);
expect(response.status).to.eql(416);
expect(response.status).to.equal(416);
});

it(`should return the expected Response when it's called with valid parameters`, async function() {
const response = await createPartialResponse(VALID_REQUEST, VALID_RESPONSE);
expect(response.status).to.eql(206);
expect(response.headers.get('Content-Length')).to.eql(101);
expect(response.headers.get('Content-Range')).to.eql(`bytes 100-200/${SOURCE_BLOB_SIZE}`);
expect(response.status).to.equal(206);
expect(response.headers.get('Content-Length')).to.equal('101');
expect(response.headers.get('Content-Range')).to.equal(`bytes 100-200/${SOURCE_BLOB_SIZE}`);

const responseBlob = await response.blob();
const expectedBlob = constructBlob(101);
expect(responseBlob._text).to.eql(expectedBlob._text);

expect(await (new Response(responseBlob)).text())
.to.equal(await (new Response(expectedBlob)).text());
});

it(`should handle being passed a Response with a status of 206 by returning it as-is`, async function() {
const originalPartialResponse = new Response('expected text', {status: 206});
const createdPartialResponse = await createPartialResponse(VALID_REQUEST, originalPartialResponse);

// We should get back the exact same response.
expect(createdPartialResponse).to.eql(originalPartialResponse);
expect(createdPartialResponse).to.equal(originalPartialResponse);
});
});
});
Expand Up @@ -6,15 +6,10 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import {calculateEffectiveBoundaries} from 'workbox-range-requests/utils/calculateEffectiveBoundaries.mjs';

import {calculateEffectiveBoundaries} from '../../../../packages/workbox-range-requests/utils/calculateEffectiveBoundaries.mjs';
import expectError from '../../../../infra/testing/expectError';
import {devOnly} from '../../../../infra/testing/env-it';

describe(`[workbox-range-requests] utils/calculateEffectiveBoundaries`, function() {
// This uses an interface that matches what our Blob mock currently supports.
// It's *not* the same way we'd use native browser implementation.
describe(`calculateEffectiveBoundaries()`, function() {
function constructBlob(length) {
let string = '';
for (let i = 0; i < length; i++) {
Expand All @@ -26,7 +21,9 @@ describe(`[workbox-range-requests] utils/calculateEffectiveBoundaries`, function
const SOURCE_BLOB_SIZE = 256;
const SOURCE_BLOB = constructBlob(SOURCE_BLOB_SIZE);

devOnly.it(`should throw when it's is called with an invalid 'blob' parameter`, async function() {
it(`should throw when it's is called with an invalid 'blob' parameter`, async function() {
if (process.env.NODE_ENV === 'production') this.skip();

const invalidBlob = null;
await expectError(
() => calculateEffectiveBoundaries(invalidBlob),
Expand Down
Expand Up @@ -6,14 +6,13 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import {parseRangeHeader} from 'workbox-range-requests/utils/parseRangeHeader.mjs';

import {parseRangeHeader} from '../../../../packages/workbox-range-requests/utils/parseRangeHeader.mjs';
import expectError from '../../../../infra/testing/expectError';
import {devOnly} from '../../../../infra/testing/env-it';

describe(`[workbox-range-requests] utils/parseRangeHeader`, function() {
devOnly.it(`should throw when it's is called with an invalid 'rangeHeader' parameter`, async function() {
describe(`parseRangeHeader()`, function() {
it(`should throw when it's is called with an invalid 'rangeHeader' parameter`, async function() {
if (process.env.NODE_ENV === 'production') this.skip();

const rangeHeader = null;
await expectError(
() => parseRangeHeader(rangeHeader),
Expand Down

0 comments on commit cb2eac0

Please sign in to comment.