| 520 | |
| 521 | // Creates a PATCH test for the given resource with the given expected outcomes |
| 522 | function describePatch ({ path, exists = true, patch, contentType = 'text/n3' }, |
| 523 | { status = 200, text, result }) { |
| 524 | return () => { |
| 525 | const filename = `patch${path}` |
| 526 | let originalContents |
| 527 | // Back up and restore an existing file |
| 528 | if (exists) { |
| 529 | before(() => backup(filename)) |
| 530 | after(() => restore(filename)) |
| 531 | // Store its contents to verify non-modification |
| 532 | if (!result) { |
| 533 | originalContents = read(filename) |
| 534 | } |
| 535 | // Ensure a non-existing file is removed |
| 536 | } else { |
| 537 | before(() => rm(filename)) |
| 538 | after(() => rm(filename)) |
| 539 | } |
| 540 | |
| 541 | // Create the request and obtain the response |
| 542 | let response |
| 543 | before((done) => { |
| 544 | request.patch(path) |
| 545 | .set('Content-Type', contentType) |
| 546 | .send(`@prefix solid: <http://www.w3.org/ns/solid/terms#>.\n${patch}`) |
| 547 | .then(res => { response = res }) |
| 548 | .then(done, done) |
| 549 | }) |
| 550 | |
| 551 | // Verify the response's status code and body text |
| 552 | it(`returns HTTP status code ${status}`, () => { |
| 553 | assert.isObject(response) |
| 554 | assert.equal(response.statusCode, status) |
| 555 | }) |
| 556 | it(`has "${text}" in the response`, () => { |
| 557 | assert.isObject(response) |
| 558 | assert.include(response.text, text) |
| 559 | }) |
| 560 | |
| 561 | // For existing files, verify correct patch application |
| 562 | if (exists) { |
| 563 | if (result) { |
| 564 | it('patches the file correctly', () => { |
| 565 | assert.equal(read(filename), result) |
| 566 | }) |
| 567 | } else { |
| 568 | it('does not modify the file', () => { |
| 569 | assert.equal(read(filename), originalContents) |
| 570 | }) |
| 571 | } |
| 572 | // For non-existing files, verify creation and contents |
| 573 | } else { |
| 574 | if (result) { |
| 575 | it('creates the file', () => { |
| 576 | assert.isTrue(fs.existsSync(`${root}/${path}`)) |
| 577 | }) |
| 578 | |
| 579 | it('writes the correct contents', () => { |