-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bugfix/ARSN-384-redirect-error-body' into q/7.10
- Loading branch information
Showing
5 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
tests/unit/s3routes/routesUtils/redirectRequestOnError.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const assert = require('assert'); | ||
const sinon = require('sinon'); | ||
|
||
const DummyRequestLogger = require('../../storage/metadata/mongoclient/utils/DummyRequestLogger'); | ||
const HttpResponseMock = require('../../../utils/HttpResponseMock'); | ||
const routesUtils = require('../../../../lib/s3routes/routesUtils'); | ||
const { errors } = require('../../../../index'); | ||
const DataWrapper = require('../../../../lib/storage/data/DataWrapper'); | ||
|
||
const corsHeaders = { | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-methods': 'GET', | ||
}; | ||
|
||
function assertHeaders(responseMock, expectedHeaders) { | ||
for (const [key, val] of Object.entries(expectedHeaders)) { | ||
assert.strictEqual(responseMock._headers[key], val); | ||
} | ||
} | ||
|
||
describe('routesUtils.redirectRequestOnError', () => { | ||
describe('from request on folder containing ' + | ||
'index without trailing /', () => { | ||
const errorHeaders = { | ||
'x-amz-error-code': errors.Found.type, | ||
'x-amz-error-message': errors.Found.description, | ||
}; | ||
|
||
it('should redirect 302 with body on GET', () => { | ||
const responseMock = new HttpResponseMock(); | ||
const routing = { withError: true, location: '/photos/' }; | ||
routesUtils.redirectRequestOnError( | ||
errors.Found, 'GET', | ||
routing, null, null, responseMock, | ||
corsHeaders, new DummyRequestLogger(), | ||
); | ||
|
||
assert.strictEqual(responseMock.statusCode, 302); | ||
assertHeaders(responseMock, corsHeaders); | ||
assertHeaders(responseMock, errorHeaders); | ||
assert.strictEqual(responseMock._headers.Location, routing.location); | ||
assert.match(responseMock._body, /<h1>302 Found<\/h1>/); | ||
assert.match(responseMock._body, /<li>Code: Found<\/li>/); | ||
assert.match(responseMock._body, /<li>Message: Resource Found<\/li>/); | ||
}); | ||
|
||
it('should redirect 302 without body on HEAD', () => { | ||
const responseMock = new HttpResponseMock(); | ||
const routing = { withError: true, location: '/photos/' }; | ||
routesUtils.redirectRequestOnError( | ||
errors.Found, 'HEAD', | ||
routing, null, null, responseMock, | ||
corsHeaders, new DummyRequestLogger(), | ||
); | ||
|
||
assert.strictEqual(responseMock.statusCode, 302); | ||
assertHeaders(responseMock, corsHeaders); | ||
assertHeaders(responseMock, errorHeaders); | ||
assert.strictEqual(responseMock._headers.Location, routing.location); | ||
assert.strictEqual(responseMock._body, null); | ||
}); | ||
}); | ||
|
||
describe('from error document redirect location header', () => { | ||
let dataWrapperGetStub; | ||
|
||
afterAll(() => { | ||
if (dataWrapperGetStub) { | ||
dataWrapperGetStub.restore(); | ||
} | ||
}); | ||
|
||
it('should redirect 301 with body on GET', () => { | ||
const responseMock = new HttpResponseMock(); | ||
const routing = { withError: true, | ||
location: 'http://scality.com/test' }; | ||
const errorHeaders = { | ||
'x-amz-error-code': errors.AccessDenied.type, | ||
'x-amz-error-message': errors.AccessDenied.description, | ||
}; | ||
dataWrapperGetStub = sinon.stub(DataWrapper.prototype, 'get'); | ||
|
||
const mockedDataLocations = [{ mock: true }]; | ||
const mockedRetrieveDataParams = { | ||
mockRetrieveDataParams: true, | ||
}; | ||
|
||
routesUtils.redirectRequestOnError( | ||
errors.AccessDenied, 'GET', | ||
routing, mockedDataLocations, mockedRetrieveDataParams, | ||
responseMock, corsHeaders, new DummyRequestLogger(), | ||
); | ||
|
||
assert.strictEqual(responseMock.statusCode, 301); | ||
assertHeaders(responseMock, corsHeaders); | ||
assertHeaders(responseMock, errorHeaders); | ||
assert.strictEqual(responseMock._headers.Location, routing.location); | ||
assert.strictEqual(dataWrapperGetStub.callCount, 1); | ||
assert.strictEqual(dataWrapperGetStub.getCall(0).args[0], | ||
mockedDataLocations[0]); | ||
assert.strictEqual(dataWrapperGetStub.getCall(0).args[1], | ||
responseMock); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters