Skip to content

Commit 714cb09

Browse files
committed
Expose maxBodyLength and maxContentLength, 1.6.0
1 parent c5c4091 commit 714cb09

File tree

7 files changed

+102
-59
lines changed

7 files changed

+102
-59
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
All notable changes to this project will be documented in this file. Dates are displayed in UTC.
44

5-
### Unreleased
5+
### [1.6.0](https://github.com/doesdev/get-scrud/compare/1.5.2...1.6.0)
66

7-
> 2021-06-30
7+
> 2021-07-19
88
9-
- Update dev dependencies (rollup, babel)
9+
- Expose `maxBodyLength` and `maxContentLength` axios options
10+
- Update dev dependencies (rollup, babel, mvt)
1011

1112
### [1.5.2](https://github.com/doesdev/get-scrud/compare/1.5.1...1.5.2)
1213

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ var source = (function () {
165165
method: method,
166166
data: body,
167167
timeout: opts.timeout,
168+
maxBodyLength: opts.maxBodyLength,
169+
maxContentLength: opts.maxContentLength,
168170
headers: {
169171
'Content-Type': 'application/json'
170172
}

module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ var source = (function () {
158158
method: method,
159159
data: body,
160160
timeout: opts.timeout,
161+
maxBodyLength: opts.maxBodyLength,
162+
maxContentLength: opts.maxContentLength,
161163
headers: {
162164
'Content-Type': 'application/json'
163165
}

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "get-scrud",
3-
"version": "1.5.2",
3+
"version": "1.6.0",
44
"description": "Client for SCRUD style rest APIs",
55
"main": "index.js",
66
"module": "module.js",
@@ -39,8 +39,8 @@
3939
"@babel/core": "^7.14.6",
4040
"@babel/polyfill": "^7.12.1",
4141
"@babel/preset-env": "^7.14.7",
42-
"mvt": "^4.1.1",
43-
"rollup": "^2.52.7",
42+
"mvt": "^4.2.1",
43+
"rollup": "^2.53.2",
4444
"rollup-plugin-babel": "^4.4.0",
4545
"scrud": "^7.2.1"
4646
},

source.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export default (opts = {}) => {
6868
method,
6969
data: body,
7070
timeout: opts.timeout,
71+
maxBodyLength: opts.maxBodyLength,
72+
maxContentLength: opts.maxContentLength,
7173
headers: { 'Content-Type': 'application/json' }
7274
}
7375
if (jwt) options.headers.Authorization = `Bearer ${jwt}`

test.js

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ const baseOpts = { host, port: 443, cache: true, timeout }
1313
const apiCall = getScrud(baseOpts)
1414
const jwt = 'abbc123'
1515

16+
const nextPortGen = (port = 7235) => () => ++port
17+
const nextPort = nextPortGen()
18+
19+
const getServer = async (port, handler) => {
20+
handler = handler || ((req, res) => {
21+
const data = req.headers.authorization.replace(/^Bearer\s/, '')
22+
res.setHeader('Content-Type', 'application/json; charset=utf-8')
23+
res.statusCode = 200
24+
return res.end(JSON.stringify({ data, error: null }))
25+
})
26+
27+
const server = http.createServer(handler)
28+
await new Promise((resolve, reject) => { server.listen(port, resolve) })
29+
30+
return server
31+
}
32+
1633
test('SEARCH', async (assert) => {
1734
const data = await apiCall('posts', 'search', {})
1835
assert.truthy(Array.isArray(data))
@@ -21,8 +38,8 @@ test('SEARCH', async (assert) => {
2138
test('CREATE', async (assert) => {
2239
const body = {
2340
userId: 1,
24-
title: `get scrud yo`,
25-
body: `test scrud api-age`
41+
title: 'get scrud yo',
42+
body: 'test scrud api-age'
2643
}
2744
const data = await apiCall('posts', 'create', body)
2845
assert.is(data.userId, 1)
@@ -50,8 +67,8 @@ test('apiCall.search', async (assert) => {
5067
test('apiCall.create', async (assert) => {
5168
const body = {
5269
userId: 1,
53-
title: `get scrud yo`,
54-
body: `test scrud api-age`
70+
title: 'get scrud yo',
71+
body: 'test scrud api-age'
5572
}
5673
const data = await apiCall.create('posts', body)
5774
assert.is(data.userId, 1)
@@ -71,16 +88,16 @@ test('apiCall.delete', async (assert) => {
7188
await assert.notThrowsAsync(() => apiCall.delete('posts', 2))
7289
})
7390

74-
test(`JWT passed in init is not malformed / doesn't throw`, async (assert) => {
91+
test('JWT passed in init is not malformed / doesn\'t throw', async (assert) => {
7592
const apiCallJwtInit = getScrud(Object.assign({ jwt }, baseOpts))
7693
await assert.notThrowsAsync(() => apiCallJwtInit('posts', 'read', 1))
7794
})
7895

79-
test(`JWT passed in call is not malformed / doesn't throw`, async (assert) => {
96+
test('JWT passed in call is not malformed / doesn\'t throw', async (assert) => {
8097
const body = {
8198
userId: 1,
82-
title: `get scrud yo`,
83-
body: `test scrud api-age`
99+
title: 'get scrud yo',
100+
body: 'test scrud api-age'
84101
}
85102

86103
await assert.notThrowsAsync(() => apiCall('posts', 'search', {}, jwt))
@@ -90,59 +107,38 @@ test(`JWT passed in call is not malformed / doesn't throw`, async (assert) => {
90107
await assert.notThrowsAsync(() => apiCall('posts', 'delete', 2, jwt))
91108
})
92109

93-
test(`Can change options on an instance`, async (assert) => {
94-
const handler = (req, res) => {
95-
const data = req.headers.authorization.replace(/^Bearer\s/, '')
96-
res.setHeader('Content-Type', 'application/json; charset=utf-8')
97-
res.statusCode = 200
98-
return res.end(JSON.stringify({ data, error: null }))
99-
}
100-
101-
const server = http.createServer(handler)
102-
await new Promise((resolve, reject) => { server.listen(7236, resolve) })
103-
104-
const opts = { host: 'localhost', port: 7236, timeout, jwt }
110+
test('Can change options on an instance', async (assert) => {
111+
const port = nextPort()
112+
await getServer(port)
113+
const opts = { host: 'localhost', port, timeout, jwt }
105114
const caller = getScrud(opts)
106115
const initalJwt = await caller('whatevs', 'read', 1)
107116

108117
assert.is(initalJwt, jwt)
109118

110119
let localJwt = await caller('whatevs', 'read', 1, 'eff')
111-
112120
assert.is(localJwt, 'eff')
113121

114122
caller({ jwt: 'this' })
115-
116123
const newJwt = await caller('whatevs', 'read', 1)
117-
118124
assert.is(newJwt, 'this')
119125

120126
localJwt = await caller('whatevs', 'read', 1, 'noise')
121-
122127
assert.is(localJwt, 'noise')
123128
})
124129

125-
test(`Can cache instance, use uncached`, async (assert) => {
126-
const handler = (req, res) => {
127-
const data = req.headers.authorization.replace(/^Bearer\s/, '')
128-
res.setHeader('Content-Type', 'application/json; charset=utf-8')
129-
res.statusCode = 200
130-
return res.end(JSON.stringify({ data, error: null }))
131-
}
132-
133-
const server = http.createServer(handler)
134-
await new Promise((resolve, reject) => { server.listen(7237, resolve) })
135-
136-
const opts = { host: 'localhost', port: 7237, timeout, jwt, cache: true }
130+
test('Can cache instance, use uncached', async (assert) => {
131+
const port = nextPort()
132+
await getServer(port)
137133

134+
const opts = { host: 'localhost', port, timeout, jwt, cache: true }
138135
await assert.throwsAsync(() => getScrud(opts)('whatevs', 'read', 1))
139136

140137
delete opts.cache
141-
142138
await assert.notThrowsAsync(() => getScrud(opts)('whatevs', 'read', 1))
143139
})
144140

145-
test(`string resourceId doesn't throw`, async (assert) => {
141+
test('string resourceId doesn\'t throw', async (assert) => {
146142
const resource = 'someresource'
147143
const port = 7942
148144
const read = (req, res) => scrud.sendData(res, { id: req.id })
@@ -157,3 +153,43 @@ test(`string resourceId doesn't throw`, async (assert) => {
157153

158154
assert.is(data.id, id)
159155
})
156+
157+
test('timeout option is respected', async (assert) => {
158+
const handler = async (req, res) => {
159+
res.setHeader('Content-Type', 'application/json; charset=utf-8')
160+
res.statusCode = 200
161+
await new Promise((resolve, reject) => setTimeout(resolve, 50))
162+
return res.end(JSON.stringify({ data: 'a', error: null }))
163+
}
164+
165+
const port = nextPort()
166+
await getServer(port, handler)
167+
168+
const optsA = { host: 'localhost', port, jwt, timeout: 1 }
169+
await assert.throwsAsync(() => getScrud(optsA)('a', 'create', 1, { a: 1 }))
170+
171+
const optsB = { host: 'localhost', port, jwt, timeout: 30000 }
172+
await assert.notThrowsAsync(() => getScrud(optsB)('a', 'create', 1, { a: 1 }))
173+
})
174+
175+
test('maxBodyLength option is respected', async (assert) => {
176+
const port = nextPort()
177+
await getServer(port)
178+
179+
const optsA = { host: 'localhost', port, jwt, maxBodyLength: 1 }
180+
await assert.throwsAsync(() => getScrud(optsA)('a', 'create', 1, { a: 1 }))
181+
182+
const optsB = { host: 'localhost', port, jwt, maxBodyLength: 1e5 }
183+
await assert.notThrowsAsync(() => getScrud(optsB)('a', 'create', 1, { a: 1 }))
184+
})
185+
186+
test('maxContentLength option is respected', async (assert) => {
187+
const port = nextPort()
188+
await getServer(port)
189+
190+
const optsA = { host: 'localhost', port, jwt, maxContentLength: 1 }
191+
await assert.throwsAsync(() => getScrud(optsA)('a', 'create', 1, { a: 1 }))
192+
193+
const optsB = { host: 'localhost', port, jwt, maxContentLength: 1e5 }
194+
await assert.notThrowsAsync(() => getScrud(optsB)('a', 'create', 1, { a: 1 }))
195+
})

0 commit comments

Comments
 (0)