Skip to content

Commit

Permalink
Percent-encode X-XlsForm-FormId-Fallback header
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-white committed Mar 24, 2021
1 parent fa97e32 commit d4e36bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
11 changes: 6 additions & 5 deletions src/components/form/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ export default {
// but that didn't work in Edge.)
contentType() {
const { name } = this.file;
if (name.length >= 5 && name.slice(-5) === '.xlsx')
if (name.endsWith('.xlsx'))
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
if (name.length >= 4 && name.slice(-4) === '.xls')
return 'application/vnd.ms-excel';
if (name.endsWith('.xls')) return 'application/vnd.ms-excel';
return 'application/xml';
}
},
Expand Down Expand Up @@ -165,8 +164,10 @@ export default {

const query = ignoreWarnings ? { ignoreWarnings } : null;
const headers = { 'Content-Type': this.contentType };
if (this.contentType !== 'application/xml')
headers['X-XlsForm-FormId-Fallback'] = this.file.name.replace(/\.xlsx?$/, '');
if (this.contentType !== 'application/xml') {
const fallback = this.file.name.replace(/\.xlsx?$/, '');
headers['X-XlsForm-FormId-Fallback'] = encodeURIComponent(fallback);
}
const { currentRoute } = this.$store.state.router;
this.request({
method: 'POST',
Expand Down
28 changes: 16 additions & 12 deletions test/components/form/new.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,15 @@ describe('FormNew', () => {
project: testData.extendedProjects.createPast(1).last()
}
})
.request(modal => selectFileByInput(modal, xlsForm())
.then(trigger.click('#form-new-upload-button')))
.beforeEachResponse((modal, config) => {
config.headers['Content-Type'].should.equal('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
config.headers['X-XlsForm-FormId-Fallback'].should.equal('my_form');
.request(async (modal) => {
const type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
const file = new File([''], 'formulář.xlsx', { type });
await selectFileByInput(modal, file);
return trigger.click(modal, '#form-new-upload-button');
})
.beforeEachResponse((_, { headers }) => {
headers['Content-Type'].should.equal('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
headers['X-XlsForm-FormId-Fallback'].should.equal('formul%C3%A1%C5%99');
})
.respondWithProblem());

Expand All @@ -260,15 +264,15 @@ describe('FormNew', () => {
project: testData.extendedProjects.createPast(1).last()
}
})
.request(modal => {
.request(async (modal) => {
const type = 'application/vnd.ms-excel';
const file = new File([''], 'my_form.xls', { type });
return selectFileByInput(modal, file)
.then(trigger.click('#form-new-upload-button'));
const file = new File([''], 'formulář.xls', { type });
await selectFileByInput(modal, file);
return trigger.click(modal, '#form-new-upload-button');
})
.beforeEachResponse((modal, config) => {
config.headers['Content-Type'].should.equal('application/vnd.ms-excel');
config.headers['X-XlsForm-FormId-Fallback'].should.equal('my_form');
.beforeEachResponse((_, { headers }) => {
headers['Content-Type'].should.equal('application/vnd.ms-excel');
headers['X-XlsForm-FormId-Fallback'].should.equal('formul%C3%A1%C5%99');
})
.respondWithProblem());

Expand Down

0 comments on commit d4e36bc

Please sign in to comment.