diff --git a/spec/Middlewares.spec.js b/spec/Middlewares.spec.js new file mode 100644 index 0000000000..746b2bcda4 --- /dev/null +++ b/spec/Middlewares.spec.js @@ -0,0 +1,67 @@ +var middlewares = require('../src/middlewares'); +var AppCache = require('../src/cache').AppCache; + +describe('middlewares', () => { + + var fakeReq, fakeRes; + + beforeEach(() => { + fakeReq = { + originalUrl: 'http://example.com/parse/', + url: 'http://example.com/', + body: { + _ApplicationId: 'FakeAppId' + }, + headers: {}, + get: (key) => { + return fakeReq.headers[key.toLowerCase()] + } + }; + AppCache.put(fakeReq.body._ApplicationId, {}); + }); + + afterEach(() => { + AppCache.del(fakeReq.body._ApplicationId); + }); + + it('should use _ContentType if provided', (done) => { + expect(fakeReq.headers['content-type']).toEqual(undefined); + fakeReq.body._ContentType = 'image/jpeg'; + middlewares.handleParseHeaders(fakeReq, fakeRes, () => { + expect(fakeReq.headers['content-type']).toEqual(fakeReq.body._ContentType); + done() + }); + }); + + const BodyParams = { + clientVersion: '_ClientVersion', + installationId: '_InstallationId', + sessionToken: '_SessionToken', + masterKey: '_MasterKey', + javascriptKey: '_JavaScriptKey' + }; + + const BodyKeys = Object.keys(BodyParams); + + BodyKeys.forEach((infoKey) => { + const bodyKey = BodyParams[infoKey]; + const keyValue = 'Fake' + bodyKey; + // javascriptKey is the only one that gets defaulted, + const otherKeys = BodyKeys.filter((otherKey) => otherKey !== infoKey && otherKey !== 'javascriptKey'); + + it(`it should pull ${bodyKey} into req.info`, (done) => { + fakeReq.body[bodyKey] = keyValue; + + middlewares.handleParseHeaders(fakeReq, fakeRes, () => { + expect(fakeReq.body[bodyKey]).toEqual(undefined); + expect(fakeReq.info[infoKey]).toEqual(keyValue); + + otherKeys.forEach((otherKey) => { + expect(fakeReq.info[otherKey]).toEqual(undefined); + }); + + done(); + }); + }); + }); +}); \ No newline at end of file diff --git a/spec/ParseFile.spec.js b/spec/ParseFile.spec.js index 2b3ad7d0f4..20c751947e 100644 --- a/spec/ParseFile.spec.js +++ b/spec/ParseFile.spec.js @@ -36,6 +36,31 @@ describe('Parse.File testing', () => { }); }); + + it('works with _ContentType', done => { + + request.post({ + url: 'http://localhost:8378/1/files/file', + body: JSON.stringify({ + _ApplicationId: 'test', + _JavaScriptKey: 'test', + _ContentType: 'text/html', + base64: 'PGh0bWw+PC9odG1sPgo=' + }) + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_file.html/); + expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/test\/.*file.html$/); + request.get(b.url, (error, response, body) => { + expect(response.headers['content-type']).toMatch('^text/html'); + expect(error).toBe(null); + expect(body).toEqual('\n'); + done(); + }); + }); + }); + it('works without Content-Type', done => { var headers = { 'X-Parse-Application-Id': 'test', diff --git a/src/middlewares.js b/src/middlewares.js index e46eb62557..46b633ec21 100644 --- a/src/middlewares.js +++ b/src/middlewares.js @@ -84,6 +84,10 @@ function handleParseHeaders(req, res, next) { info.masterKey = req.body._MasterKey; delete req.body._MasterKey; } + if (req.body._ContentType) { + req.headers['content-type'] = req.body._ContentType; + delete req.body_contentType; + } } else { return invalidRequest(req, res); }