Skip to content

Commit 3997b1a

Browse files
blachaflovilmart
authored andcommitted
Fixing #1900 JS SDK file upload (#1935)
* Fixing #1900 JS SDK file upload JS SDK file upload uses req.body._ContentType to specify the upload content type * Fixing import statements * Dont clear the cache just delete the new entry that the test added. * adding E2E test for _ContentType support
1 parent cd52580 commit 3997b1a

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

spec/Middlewares.spec.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var middlewares = require('../src/middlewares');
2+
var AppCache = require('../src/cache').AppCache;
3+
4+
describe('middlewares', () => {
5+
6+
var fakeReq, fakeRes;
7+
8+
beforeEach(() => {
9+
fakeReq = {
10+
originalUrl: 'http://example.com/parse/',
11+
url: 'http://example.com/',
12+
body: {
13+
_ApplicationId: 'FakeAppId'
14+
},
15+
headers: {},
16+
get: (key) => {
17+
return fakeReq.headers[key.toLowerCase()]
18+
}
19+
};
20+
AppCache.put(fakeReq.body._ApplicationId, {});
21+
});
22+
23+
afterEach(() => {
24+
AppCache.del(fakeReq.body._ApplicationId);
25+
});
26+
27+
it('should use _ContentType if provided', (done) => {
28+
expect(fakeReq.headers['content-type']).toEqual(undefined);
29+
fakeReq.body._ContentType = 'image/jpeg';
30+
middlewares.handleParseHeaders(fakeReq, fakeRes, () => {
31+
expect(fakeReq.headers['content-type']).toEqual(fakeReq.body._ContentType);
32+
done()
33+
});
34+
});
35+
36+
const BodyParams = {
37+
clientVersion: '_ClientVersion',
38+
installationId: '_InstallationId',
39+
sessionToken: '_SessionToken',
40+
masterKey: '_MasterKey',
41+
javascriptKey: '_JavaScriptKey'
42+
};
43+
44+
const BodyKeys = Object.keys(BodyParams);
45+
46+
BodyKeys.forEach((infoKey) => {
47+
const bodyKey = BodyParams[infoKey];
48+
const keyValue = 'Fake' + bodyKey;
49+
// javascriptKey is the only one that gets defaulted,
50+
const otherKeys = BodyKeys.filter((otherKey) => otherKey !== infoKey && otherKey !== 'javascriptKey');
51+
52+
it(`it should pull ${bodyKey} into req.info`, (done) => {
53+
fakeReq.body[bodyKey] = keyValue;
54+
55+
middlewares.handleParseHeaders(fakeReq, fakeRes, () => {
56+
expect(fakeReq.body[bodyKey]).toEqual(undefined);
57+
expect(fakeReq.info[infoKey]).toEqual(keyValue);
58+
59+
otherKeys.forEach((otherKey) => {
60+
expect(fakeReq.info[otherKey]).toEqual(undefined);
61+
});
62+
63+
done();
64+
});
65+
});
66+
});
67+
});

spec/ParseFile.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ describe('Parse.File testing', () => {
3636
});
3737
});
3838

39+
40+
it('works with _ContentType', done => {
41+
42+
request.post({
43+
url: 'http://localhost:8378/1/files/file',
44+
body: JSON.stringify({
45+
_ApplicationId: 'test',
46+
_JavaScriptKey: 'test',
47+
_ContentType: 'text/html',
48+
base64: 'PGh0bWw+PC9odG1sPgo='
49+
})
50+
}, (error, response, body) => {
51+
expect(error).toBe(null);
52+
var b = JSON.parse(body);
53+
expect(b.name).toMatch(/_file.html/);
54+
expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/test\/.*file.html$/);
55+
request.get(b.url, (error, response, body) => {
56+
expect(response.headers['content-type']).toMatch('^text/html');
57+
expect(error).toBe(null);
58+
expect(body).toEqual('<html></html>\n');
59+
done();
60+
});
61+
});
62+
});
63+
3964
it('works without Content-Type', done => {
4065
var headers = {
4166
'X-Parse-Application-Id': 'test',

src/middlewares.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ function handleParseHeaders(req, res, next) {
8484
info.masterKey = req.body._MasterKey;
8585
delete req.body._MasterKey;
8686
}
87+
if (req.body._ContentType) {
88+
req.headers['content-type'] = req.body._ContentType;
89+
delete req.body_contentType;
90+
}
8791
} else {
8892
return invalidRequest(req, res);
8993
}

0 commit comments

Comments
 (0)