forked from TheDeveloper/http-aws-es
-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathAmazonConnection.test.js
105 lines (87 loc) · 3.05 KB
/
AmazonConnection.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict'
const assert = require('assert')
const { URL } = require('url')
const { Connection } = require('@elastic/elasticsearch')
const AWS = require('aws-sdk')
const awsConfig = new AWS.Config({
accessKeyId: 'foo',
secretAccessKey: 'bar',
sessionToken: 'baz'
})
const AmazonConnection = require('../src/AmazonConnection')(awsConfig)
describe('AmazonConnection', function () {
it('extends Connection', function () {
assert(AmazonConnection.prototype instanceof Connection)
})
describe('buildRequestObject()', function () {
const connector = new AmazonConnection({
url: new URL('https://foo.us-east-1.es.amazonaws.com')
})
it('sets the Host header without the port appended', function () {
const req = connector.buildRequestObject({
method: 'GET',
path: '/_cluster/health',
query: {},
body: undefined,
headers: {}
})
assert.strictEqual(req.headers.host, 'foo.us-east-1.es.amazonaws.com')
})
it('sets the content-length: 0 header when there is no body', function () {
const req = connector.buildRequestObject({
method: 'POST',
path: '/_cluster/health',
query: {},
body: '',
headers: {}
})
assert.strictEqual(req.headers['content-length'], 0)
})
it('sets the content-length header when there is a body', function () {
const req = connector.buildRequestObject({
method: 'POST',
path: '/_cluster/health',
query: {},
body: 'foo',
headers: {}
})
assert.strictEqual(req.headers['content-length'], 3)
assert.strictEqual(req.body, 'foo')
})
it('signs the request', function () {
const req = connector.buildRequestObject({
method: 'POST',
path: '/_cluster/health',
query: {},
body: 'foo',
headers: {
Date: new Date('2019-08-16T20:51:18Z').toISOString()
}
})
assert.strictEqual(req.headers['X-Amz-Date'], '20190816T205118Z')
assert.strictEqual(req.headers.Authorization, 'AWS4-HMAC-SHA256 Credential=foo/20190816/us-east-1/es/aws4_request, SignedHeaders=content-length;content-type;date;host;x-amz-date;x-amz-security-token, Signature=5daf2e5d70fe61002e12ad3d4d8dcfcda64bb477ce764af1c30967393d83ba1f')
})
it('sets the region when region is configured in the AWS config object', function () {
const regionConfig = new AWS.Config({
accessKeyId: 'foo',
region: 'us-west-2',
secretAccessKey: 'bar',
sessionToken: 'baz'
})
const RegionConnection = require('../src/AmazonConnection')(regionConfig)
const regionConnector = new RegionConnection({
url: new URL('https://foo.us-west-2.es.amazonaws.com')
})
const req = regionConnector.buildRequestObject({
method: 'POST',
path: '/_cluster/health',
query: {},
body: 'foo',
headers: {
Date: new Date('2019-08-16T20:51:18Z').toISOString()
}
})
assert.strictEqual(req.region, 'us-west-2')
})
})
})