Skip to content

Commit

Permalink
Merge pull request #32 from elastic/update-prod-check
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor authored Nov 23, 2021
2 parents 8dacef2 + ede51ca commit cba8562
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export default class Transport {
result.statusCode = statusCode
result.headers = headers

if (this[kProductCheck] != null && headers['x-elastic-product'] !== this[kProductCheck] && statusCode !== 401 && statusCode !== 403) {
if (this[kProductCheck] != null && headers['x-elastic-product'] !== this[kProductCheck] && statusCode >= 200 && statusCode < 300) {
throw new ProductNotSupportedError(this[kProductCheck] as string, result)
}

Expand Down
10 changes: 5 additions & 5 deletions src/connection/UndiciConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import BaseConnection, {
ConnectionRequestResponse,
getIssuerCertificate
} from './BaseConnection'
import { Pool, buildConnector } from 'undici'
import { Pool, buildConnector, Dispatcher } from 'undici'
import {
ConfigurationError,
RequestAbortedError,
Expand Down Expand Up @@ -152,7 +152,7 @@ export default class Connection extends BaseConnection {
let response
try {
// @ts-expect-error method it's fine as string
response = await this.pool.request(requestParams)
response = (await this.pool.request(requestParams)) as Dispatcher.ResponseData
if (timeoutId != null) clearTimeout(timeoutId)
} catch (err: any) {
if (timeoutId != null) clearTimeout(timeoutId)
Expand All @@ -169,13 +169,13 @@ export default class Connection extends BaseConnection {
}

const contentEncoding = (response.headers['content-encoding'] ?? '').toLowerCase()
const isCompressed = contentEncoding.includes('gzip') || contentEncoding.includes('deflate')
const isCompressed = contentEncoding.includes('gzip') || contentEncoding.includes('deflate') // eslint-disable-line
const isVectorTile = (response.headers['content-type'] ?? '').includes('application/vnd.mapbox-vector-tile')

/* istanbul ignore else */
if (response.headers['content-length'] !== undefined) {
const contentLength = Number(response.headers['content-length'])
if (isCompressed && contentLength > maxCompressedResponseSize) {
if (isCompressed && contentLength > maxCompressedResponseSize) { // eslint-disable-line
response.body.destroy()
throw new RequestAbortedError(`The content length (${contentLength}) is bigger than the maximum allowed buffer (${maxCompressedResponseSize})`)
} else if (contentLength > maxResponseSize) {
Expand All @@ -186,7 +186,7 @@ export default class Connection extends BaseConnection {

this.diagnostic.emit('deserialization', null, options)
try {
if (isCompressed || isVectorTile) {
if (isCompressed || isVectorTile) { // eslint-disable-line
const payload: Buffer[] = []
for await (const chunk of response.body) {
payload.push(chunk)
Expand Down
109 changes: 38 additions & 71 deletions test/acceptance/product-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,82 +133,49 @@ test('Errors v8', async t => {
}
})

test('401', async t => {
t.plan(2)

const MockConnection = buildMockConnection({
onRequest (params) {
return {
statusCode: 401,
headers: {
'x-elastic-product': undefined
},
body: { error: true }
}
}
})

const client = new TestClient({
node: 'http://localhost:9200',
Connection: MockConnection
})

client.diagnostic.on(events.RESPONSE, (err, event) => {
t.ok(err instanceof errors.ResponseError)
})

try {
await client.request({
path: '/foo/_search',
method: 'POST',
body: {
query: {
match_all: {}
function withCode (code: number): void {
test(`With code ${code}`, async t => {
t.plan(2)

const MockConnection = buildMockConnection({
onRequest (params) {
return {
statusCode: code,
headers: {
'x-elastic-product': undefined
},
body: { error: true }
}
}
})
t.fail('Should throw')
} catch (err: any) {
t.equal(err.statusCode, 401)
}
})

test('403', async t => {
t.plan(2)

const MockConnection = buildMockConnection({
onRequest (params) {
return {
statusCode: 403,
headers: {
'x-elastic-product': undefined
},
body: { error: true }
}
}
})
const client = new TestClient({
node: 'http://localhost:9200',
Connection: MockConnection
})

const client = new TestClient({
node: 'http://localhost:9200',
Connection: MockConnection
})
client.diagnostic.on(events.RESPONSE, (err, event) => {
t.ok(err instanceof errors.ResponseError)
})

client.diagnostic.on(events.RESPONSE, (err, event) => {
t.ok(err instanceof errors.ResponseError)
try {
await client.request({
path: '/foo/_search',
method: 'POST',
body: {
query: {
match_all: {}
}
}
})
t.fail('Should throw')
} catch (err: any) {
t.equal(err.statusCode, code)
}
})
}

try {
await client.request({
path: '/foo/_search',
method: 'POST',
body: {
query: {
match_all: {}
}
}
})
t.fail('Should throw')
} catch (err: any) {
t.equal(err.statusCode, 403)
}
})
withCode(401)
withCode(403)
withCode(404)
withCode(413)

0 comments on commit cba8562

Please sign in to comment.