-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move validateHttpResponse helper to avoid interdependencies
- Loading branch information
1 parent
b96bb90
commit 07323db
Showing
8 changed files
with
37 additions
and
40 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './ActorHttp'; | ||
export * from './utils'; |
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,24 @@ | ||
// Use require instead of import for default exports, to be compatible with variants of esModuleInterop in tsconfig. | ||
import stringifyStream = require('stream-to-string'); | ||
import { ActorHttp } from './ActorHttp'; | ||
|
||
/** | ||
* Check if the http response is valid, and throw an error if not. | ||
* @param url The original URL that was to be updated. | ||
* @param httpResponse The update response. | ||
*/ | ||
export async function validateAndCloseHttpResponse(url: string, httpResponse: Response): Promise<void> { | ||
// Check if update was successful | ||
if (httpResponse.status >= 400) { | ||
// Consume the body, to avoid process to hang | ||
let bodyString = 'empty response'; | ||
if (httpResponse.body) { | ||
const responseStream = ActorHttp.toNodeReadable(httpResponse.body); | ||
bodyString = await stringifyStream(responseStream); | ||
} | ||
throw new Error(`Could not update ${url} (HTTP status ${httpResponse.status}):\n${bodyString}`); | ||
} | ||
|
||
// Close response body, as we don't need it | ||
await httpResponse.body?.cancel(); | ||
} |
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
12 changes: 6 additions & 6 deletions
12
...pdate-quads/test/IQuadDestination-test.ts → packages/bus-http/test/utils-test.ts
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 |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import { validateHttpResponse } from '../lib/IQuadDestination'; | ||
import { validateAndCloseHttpResponse } from '../lib/utils'; | ||
|
||
const streamifyString = require('streamify-string'); | ||
|
||
describe('validateHttpResponse', () => { | ||
describe('validateAndCloseHttpResponse', () => { | ||
it('should do nothing with a valid response', async() => { | ||
await validateHttpResponse('URL', <Response> { status: 200 }); | ||
await validateAndCloseHttpResponse('URL', <Response> { status: 200 }); | ||
}); | ||
|
||
it('should cancel the body on a valid response', async() => { | ||
const body = <any> { cancel: jest.fn() }; | ||
await validateHttpResponse('URL', <Response> { status: 200, body }); | ||
await validateAndCloseHttpResponse('URL', <Response> { status: 200, body }); | ||
expect(body.cancel).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should throw with an invalid response', async() => { | ||
await expect(validateHttpResponse('URL', <Response> { status: 400 })).rejects | ||
await expect(validateAndCloseHttpResponse('URL', <Response> { status: 400 })).rejects | ||
.toThrow('Could not update URL (HTTP status 400):\nempty response'); | ||
}); | ||
|
||
it('should throw with an invalid response with body', async() => { | ||
const body = streamifyString('BODY'); | ||
await expect(validateHttpResponse('URL', <Response> { status: 400, body })).rejects | ||
await expect(validateAndCloseHttpResponse('URL', <Response> { status: 400, body })).rejects | ||
.toThrow('Could not update URL (HTTP status 400):\nBODY'); | ||
}); | ||
}); |
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