Skip to content

Commit

Permalink
ensure resolving graph.json respects base path configuration (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 authored Nov 7, 2023
1 parent 26938f3 commit ce0c14f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/cli/src/plugins/resource/plugin-standard-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class StandardJsonResource extends ResourceInterface {

async shouldServe(url) {
const { protocol, pathname } = url;
const { basePath } = this.compilation.config;
const isJson = pathname.split('.').pop() === this.extensions[0];
const isGraphJson = pathname === '/graph.json';
const isGraphJson = pathname === `${basePath}/graph.json`;
const isWorkspaceFile = protocol === 'file:' && await checkResourceExists(url);

return isJson && (isWorkspaceFile || isGraphJson);
Expand All @@ -27,7 +28,8 @@ class StandardJsonResource extends ResourceInterface {
async serve(url) {
const { pathname } = url;
const { scratchDir } = this.compilation.context;
const finalUrl = pathname.startsWith('/graph.json')
const { basePath } = this.compilation.config;
const finalUrl = pathname === `${basePath}/graph.json`
? new URL('./graph.json', scratchDir)
: url;
const contents = await fs.readFile(finalUrl, 'utf-8');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,31 @@ describe('Develop Greenwood With: ', function() {
expect(cards.length).to.be.greaterThan(0);
});
});

describe('Fetching graph.json client side', function() {
let response;
let graph;

before(async function() {
response = await fetch(`${hostname}:${port}${basePath}/graph.json`);
graph = await response.clone().json();
});

it('should return the correct content type', function(done) {
expect(response.headers.get('content-type')).to.contain('application/json');
done();
});

it('should return a 200', function(done) {
expect(response.status).to.equal(200);
done();
});

it('should have the expected length for all content', function(done) {
expect(graph.length).to.equal(3);
done();
});
});
});

after(function() {
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/test/cases/develop.default/develop.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,31 @@ describe('Develop Greenwood With: ', function() {
done();
});
});

describe('Fetching graph.json client side', function() {
let response;
let graph;

before(async function() {
response = await fetch(`${hostname}:${port}/graph.json`);
graph = await response.clone().json();
});

it('should return the correct content type', function(done) {
expect(response.headers.get('content-type')).to.contain('application/json');
done();
});

it('should return a 200', function(done) {
expect(response.status).to.equal(200);
done();
});

it('should have the expected length for all content', function(done) {
expect(graph.length).to.equal(2);
done();
});
});
});

after(function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,31 @@ describe('Serve Greenwood With: ', function() {
expect(cards.length).to.be.greaterThan(0);
});
});

describe('Fetching graph.json client side', function() {
let response;
let graph;

before(async function() {
response = await fetch(`${hostname}${basePath}/graph.json`);
graph = await response.clone().json();
});

it('should return the correct content type', function(done) {
expect(response.headers.get('content-type')).to.contain('application/json');
done();
});

it('should return a 200', function(done) {
expect(response.status).to.equal(200);
done();
});

it('should have the expected length for all content', function(done) {
expect(graph.length).to.equal(4);
done();
});
});
});

after(function() {
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/test/cases/serve.default/serve.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,30 @@ describe('Serve Greenwood With: ', function() {
});
});

describe('Fetching graph.json client side', function() {
let response;
let graph;

before(async function() {
response = await fetch(`${hostname}/graph.json`);
graph = await response.clone().json();
});

it('should return the correct content type', function(done) {
expect(response.headers.get('content-type')).to.contain('application/json');
done();
});

it('should return a 200', function(done) {
expect(response.status).to.equal(200);
done();
});

it('should have the expected length for all content', function(done) {
expect(graph.length).to.equal(2);
done();
});
});
});

after(function() {
Expand Down

0 comments on commit ce0c14f

Please sign in to comment.