-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10106 from jmcdo29/fix/continue-on-stream-cancel
fix: add a check if the res is destroyed before sending response
- Loading branch information
Showing
5 changed files
with
82 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { IncomingMessage, request, RequestOptions } from 'http'; | ||
import { URL } from 'url'; | ||
|
||
export const getHttpBaseOptions = async ( | ||
app: INestApplication, | ||
): Promise<URL> => { | ||
const url = await app.getUrl(); | ||
return new URL(url); | ||
}; | ||
|
||
export const sendCanceledHttpRequest = async (url: URL) => { | ||
return new Promise((resolve, reject) => { | ||
const req = request(url, res => { | ||
// close the request once we get the first response of data | ||
res.on('data', () => { | ||
req.destroy(); | ||
}); | ||
// response is closed, move on to next request and verify it's doable | ||
res.on('close', resolve); | ||
}); | ||
// fire the request | ||
req.end(); | ||
}); | ||
}; | ||
|
||
export const sendHttpRequest = async (url: URL) => { | ||
return new Promise<IncomingMessage>((resolve, reject) => { | ||
const req = request(url, res => { | ||
// this makes sure that the response actually starts and is read. We could verify this value against the same | ||
// that is in an earlier test, but all we care about in _this_ test is that the status code is 200 | ||
res.on('data', chunk => { | ||
// no op | ||
}); | ||
// fail the test if somethin goes wrong | ||
res.on('error', err => { | ||
reject(err); | ||
}); | ||
// pass the response back so we can verify values in the test | ||
res.on('end', () => { | ||
resolve(res); | ||
}); | ||
}); | ||
// fire the request | ||
req.end(); | ||
}); | ||
}; |
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