-
Notifications
You must be signed in to change notification settings - Fork 0
Send Expect: 100-continue on BackbeatRoutes requests with a body #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,240 @@ | ||
| import http, { Server } from 'http'; | ||
| import { AddressInfo } from 'net'; | ||
| import { promisify } from 'util'; | ||
| import { | ||
| BackbeatRoutesClient, | ||
| PutDataCommand, | ||
| GetObjectCommand, | ||
| attachExpectContinueMiddleware, | ||
| } from '../src/index'; | ||
|
|
||
| jest.setTimeout(20000); | ||
|
|
||
| let server: Server; | ||
| let client: BackbeatRoutesClient; | ||
| let sendContinue: boolean; | ||
| let earlyReject: boolean; | ||
| let unsolicitedContinue: boolean; | ||
| let continueSent: boolean; | ||
| let captured: { | ||
| method?: string; | ||
| headers: http.IncomingHttpHeaders; | ||
| body: Buffer; | ||
| bodyArrivedBeforeContinueSent: boolean; | ||
| headersReceivedAt?: number; | ||
| firstBodyChunkAt?: number; | ||
| }; | ||
|
|
||
| describe('Expect: 100-continue middleware on PutDataCommand', () => { | ||
| beforeAll(async () => { | ||
| server = http.createServer(); | ||
|
|
||
| const handle = (req: http.IncomingMessage, res: http.ServerResponse) => { | ||
| captured.method = req.method; | ||
| captured.headers = req.headers; | ||
| if (captured.headersReceivedAt === undefined) { | ||
| captured.headersReceivedAt = Date.now(); | ||
| } | ||
| if (unsolicitedContinue && !continueSent) { | ||
| res.writeContinue(); | ||
| continueSent = true; | ||
| } | ||
| const chunks: Buffer[] = []; | ||
|
|
||
| req.on('data', chunk => { | ||
| if (captured.firstBodyChunkAt === undefined) { | ||
| captured.firstBodyChunkAt = Date.now(); | ||
| } | ||
| if (!continueSent) { | ||
| captured.bodyArrivedBeforeContinueSent = true; | ||
| } | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| captured.body = Buffer.concat(chunks); | ||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify([{ key: 'k', dataStoreName: 'd' }])); | ||
| }); | ||
| }; | ||
|
|
||
| // Once a 'checkContinue' listener exists, Node stops auto-sending 100 Continue. | ||
| server.on('checkContinue', (req, res) => { | ||
| if (captured.headersReceivedAt === undefined) { | ||
| captured.headersReceivedAt = Date.now(); | ||
| } | ||
| if (earlyReject) { | ||
| captured.method = req.method; | ||
| captured.headers = req.headers; | ||
| req.on('data', chunk => { | ||
| captured.bodyArrivedBeforeContinueSent = true; | ||
| captured.body = Buffer.concat([captured.body, chunk]); | ||
| }); | ||
| res.writeHead(409); | ||
| res.end(); | ||
| return; | ||
| } | ||
|
|
||
| if (sendContinue) { | ||
| res.writeContinue(); | ||
| continueSent = true; | ||
| } | ||
|
|
||
| handle(req, res); | ||
| }); | ||
| server.on('request', handle); | ||
|
|
||
| await promisify<number, string>(server.listen).call(server, 0, '127.0.0.1'); | ||
| const { port } = server.address() as AddressInfo; | ||
|
|
||
| client = new BackbeatRoutesClient({ | ||
| endpoint: `http://127.0.0.1:${port}`, | ||
| credentials: { accessKeyId: 'a', secretAccessKey: 'b' }, | ||
| region: 'us-east-1', | ||
| maxAttempts: 1, | ||
| }); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| client.destroy(); | ||
| await promisify(server.close).call(server); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| sendContinue = true; | ||
| earlyReject = false; | ||
| unsolicitedContinue = false; | ||
| continueSent = false; | ||
| captured = { headers: {}, body: Buffer.alloc(0), bodyArrivedBeforeContinueSent: false }; | ||
| }); | ||
|
|
||
| const putData = (Body: Buffer) => client.send(attachExpectContinueMiddleware( | ||
| new PutDataCommand({ | ||
| Bucket: 'bucket', | ||
| Key: 'obj', | ||
| ContentMD5: 'x', | ||
| CanonicalID: 'c', | ||
| Body, | ||
| }), | ||
| client.config.requestHandler, | ||
| )); | ||
|
|
||
| it('sets Expect and waits for 100 before streaming the body', async () => { | ||
| const body = Buffer.from('hello-world'); | ||
| await putData(body); | ||
|
|
||
| expect(captured.method).toBe('PUT'); | ||
| expect(captured.headers.expect).toBe('100-continue'); | ||
| expect(captured.bodyArrivedBeforeContinueSent).toBe(false); | ||
| expect(captured.body.length).toBe(body.length); | ||
| }); | ||
|
|
||
| it('does NOT set Expect on body-less commands (GetObject)', async () => { | ||
| await client | ||
| .send(new GetObjectCommand({ Bucket: 'bucket', Key: 'obj' })) | ||
| .catch(() => undefined); | ||
| expect(captured.headers.expect).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('does NOT set Expect on PutData without attachExpectContinueMiddleware', async () => { | ||
| await client.send(new PutDataCommand({ | ||
| Bucket: 'bucket', | ||
| Key: 'obj', | ||
| ContentMD5: 'x', | ||
| CanonicalID: 'c', | ||
| Body: Buffer.from('hello-world'), | ||
| })); | ||
| expect(captured.method).toBe('PUT'); | ||
| expect(captured.headers.expect).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('honors a numeric expectContinueHeader threshold (below threshold => no header)', async () => { | ||
| const body = Buffer.from('tiny'); | ||
| await client.send(attachExpectContinueMiddleware( | ||
| new PutDataCommand({ | ||
| Bucket: 'bucket', | ||
| Key: 'obj', | ||
| ContentMD5: 'x', | ||
| CanonicalID: 'c', | ||
| Body: body, | ||
| }), | ||
| client.config.requestHandler, | ||
| 1024, | ||
| )); | ||
| expect(captured.method).toBe('PUT'); | ||
| expect(captured.headers.expect).toBeUndefined(); | ||
| expect(captured.body.length).toBe(body.length); | ||
| }); | ||
|
|
||
| it('honors a numeric expectContinueHeader threshold (at/above threshold => header set)', async () => { | ||
| const body = Buffer.alloc(1024, 'a'); | ||
| await client.send(attachExpectContinueMiddleware( | ||
| new PutDataCommand({ | ||
| Bucket: 'bucket', | ||
| Key: 'obj', | ||
| ContentMD5: 'x', | ||
| CanonicalID: 'c', | ||
| Body: body, | ||
| }), | ||
| client.config.requestHandler, | ||
| 1024, | ||
| )); | ||
| expect(captured.headers.expect).toBe('100-continue'); | ||
| expect(captured.body.length).toBe(body.length); | ||
| }); | ||
|
|
||
| it('still uploads when the server sends an unsolicited 100-continue', async () => { | ||
| unsolicitedContinue = true; | ||
| const body = Buffer.from('hello-world'); | ||
| await client.send(new PutDataCommand({ | ||
| Bucket: 'bucket', | ||
| Key: 'obj', | ||
| ContentMD5: 'x', | ||
| CanonicalID: 'c', | ||
| Body: body, | ||
| })); | ||
| expect(captured.method).toBe('PUT'); | ||
| expect(captured.headers.expect).toBeUndefined(); | ||
| expect(captured.body.length).toBe(body.length); | ||
| }); | ||
|
|
||
| it('still uploads if the server never sends 100-continue (falls back after timeout)', async () => { | ||
| sendContinue = false; | ||
| const body = Buffer.from('hello-world'); | ||
| await putData(body); | ||
|
|
||
| expect(captured.headers.expect).toBe('100-continue'); | ||
| expect(captured.body.length).toBe(body.length); | ||
| }); | ||
|
|
||
| it('waits ~6s before streaming the body when no 100-continue is received', async () => { | ||
| sendContinue = false; | ||
| const body = Buffer.from('hello-world'); | ||
| await putData(body); | ||
|
|
||
| expect(captured.headersReceivedAt).toBeDefined(); | ||
| expect(captured.firstBodyChunkAt).toBeDefined(); | ||
| const waited = captured.firstBodyChunkAt! - captured.headersReceivedAt!; | ||
| expect(waited).toBeGreaterThanOrEqual(5500); | ||
| expect(waited).toBeLessThan(8000); | ||
| expect(captured.body.length).toBe(body.length); | ||
| }); | ||
|
|
||
|
|
||
| it('surfaces an early 4xx response without streaming the body', async () => { | ||
| earlyReject = true; | ||
| const body = Buffer.from('hello-world'); | ||
|
|
||
| const err = await putData(body).then( | ||
| () => { throw new Error('expected request to fail'); }, | ||
| (e: Error & { $metadata?: { httpStatusCode?: number } }) => e, | ||
| ); | ||
|
|
||
| expect(err.$metadata?.httpStatusCode).toBe(409); | ||
| expect(captured.method).toBe('PUT'); | ||
| expect(captured.headers.expect).toBe('100-continue'); | ||
| expect(captured.bodyArrivedBeforeContinueSent).toBe(false); | ||
| expect(captured.body.length).toBe(0); | ||
| }); | ||
|
|
||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.