@defra/lis-hubs-infra-core 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-core",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Shared hub mechanics for livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -1,4 +1,5 @@
1
1
  import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2
+ import { createServer } from 'node:http'
2
3
 
3
4
  import Wreck from '@hapi/wreck'
4
5
 
@@ -168,6 +169,72 @@ describe('request verbs', () => {
168
169
  })
169
170
  })
170
171
 
172
+ describe('configured timeout with an upstream server', () => {
173
+ let server
174
+
175
+ beforeEach(async () => {
176
+ server = createServer()
177
+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
178
+ mocks.getServiceBaseUrl.mockReturnValue(
179
+ new URL(`http://127.0.0.1:${server.address().port}`)
180
+ )
181
+ })
182
+
183
+ afterEach(async () => {
184
+ server.closeAllConnections()
185
+ await new Promise((resolve, reject) => {
186
+ server.close((error) => (error ? reject(error) : resolve()))
187
+ })
188
+ })
189
+
190
+ test('it returns the response within a non-default timeout', async () => {
191
+ // Arrange
192
+ const client = createClient({ timeout: 1000 })
193
+ server.on('request', (_request, response) => {
194
+ response.setHeader('content-type', 'application/json')
195
+ response.end(JSON.stringify({ data: [] }))
196
+ })
197
+
198
+ // Act
199
+ const { res, payload } = await client._get('api/cattle')
200
+
201
+ // Assert
202
+ expect(res.statusCode).toBe(200)
203
+ expect(payload).toEqual({ data: [] })
204
+ })
205
+
206
+ test('it times out waiting for response headers using the configured timeout', async () => {
207
+ // Arrange
208
+ const client = createClient({ timeout: 50 })
209
+ server.on('request', () => {
210
+ // Keep the connection open without sending response headers.
211
+ })
212
+
213
+ // Act / Assert
214
+ await expect(client._get('api/cattle')).rejects.toMatchObject({
215
+ message: 'Request failed - 504',
216
+ statusCode: 504
217
+ })
218
+ }, 1000)
219
+
220
+ test('it times out reading the response body using the configured timeout', async () => {
221
+ // Arrange
222
+ const client = createClient({ timeout: 50 })
223
+ server.on('request', (_request, response) => {
224
+ response.writeHead(200, { 'content-type': 'application/json' })
225
+ response.flushHeaders()
226
+ response.write('{"data":')
227
+ // Leave the body unfinished so that its read times out.
228
+ })
229
+
230
+ // Act / Assert
231
+ await expect(client._get('api/cattle')).rejects.toMatchObject({
232
+ message: 'Request failed - 200',
233
+ statusCode: 200
234
+ })
235
+ }, 1000)
236
+ })
237
+
171
238
  describe('service token', () => {
172
239
  test('it requests the token for the target service as the audience', async () => {
173
240
  // Arrange