@@ -13,6 +13,23 @@ const baseOpts = { host, port: 443, cache: true, timeout }
13
13
const apiCall = getScrud ( baseOpts )
14
14
const jwt = 'abbc123'
15
15
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 ( / ^ B e a r e r \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
+
16
33
test ( 'SEARCH' , async ( assert ) => {
17
34
const data = await apiCall ( 'posts' , 'search' , { } )
18
35
assert . truthy ( Array . isArray ( data ) )
@@ -21,8 +38,8 @@ test('SEARCH', async (assert) => {
21
38
test ( 'CREATE' , async ( assert ) => {
22
39
const body = {
23
40
userId : 1 ,
24
- title : ` get scrud yo` ,
25
- body : ` test scrud api-age`
41
+ title : ' get scrud yo' ,
42
+ body : ' test scrud api-age'
26
43
}
27
44
const data = await apiCall ( 'posts' , 'create' , body )
28
45
assert . is ( data . userId , 1 )
@@ -50,8 +67,8 @@ test('apiCall.search', async (assert) => {
50
67
test ( 'apiCall.create' , async ( assert ) => {
51
68
const body = {
52
69
userId : 1 ,
53
- title : ` get scrud yo` ,
54
- body : ` test scrud api-age`
70
+ title : ' get scrud yo' ,
71
+ body : ' test scrud api-age'
55
72
}
56
73
const data = await apiCall . create ( 'posts' , body )
57
74
assert . is ( data . userId , 1 )
@@ -71,16 +88,16 @@ test('apiCall.delete', async (assert) => {
71
88
await assert . notThrowsAsync ( ( ) => apiCall . delete ( 'posts' , 2 ) )
72
89
} )
73
90
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 ) => {
75
92
const apiCallJwtInit = getScrud ( Object . assign ( { jwt } , baseOpts ) )
76
93
await assert . notThrowsAsync ( ( ) => apiCallJwtInit ( 'posts' , 'read' , 1 ) )
77
94
} )
78
95
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 ) => {
80
97
const body = {
81
98
userId : 1 ,
82
- title : ` get scrud yo` ,
83
- body : ` test scrud api-age`
99
+ title : ' get scrud yo' ,
100
+ body : ' test scrud api-age'
84
101
}
85
102
86
103
await assert . notThrowsAsync ( ( ) => apiCall ( 'posts' , 'search' , { } , jwt ) )
@@ -90,59 +107,38 @@ test(`JWT passed in call is not malformed / doesn't throw`, async (assert) => {
90
107
await assert . notThrowsAsync ( ( ) => apiCall ( 'posts' , 'delete' , 2 , jwt ) )
91
108
} )
92
109
93
- test ( `Can change options on an instance` , async ( assert ) => {
94
- const handler = ( req , res ) => {
95
- const data = req . headers . authorization . replace ( / ^ B e a r e r \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 }
105
114
const caller = getScrud ( opts )
106
115
const initalJwt = await caller ( 'whatevs' , 'read' , 1 )
107
116
108
117
assert . is ( initalJwt , jwt )
109
118
110
119
let localJwt = await caller ( 'whatevs' , 'read' , 1 , 'eff' )
111
-
112
120
assert . is ( localJwt , 'eff' )
113
121
114
122
caller ( { jwt : 'this' } )
115
-
116
123
const newJwt = await caller ( 'whatevs' , 'read' , 1 )
117
-
118
124
assert . is ( newJwt , 'this' )
119
125
120
126
localJwt = await caller ( 'whatevs' , 'read' , 1 , 'noise' )
121
-
122
127
assert . is ( localJwt , 'noise' )
123
128
} )
124
129
125
- test ( `Can cache instance, use uncached` , async ( assert ) => {
126
- const handler = ( req , res ) => {
127
- const data = req . headers . authorization . replace ( / ^ B e a r e r \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 )
137
133
134
+ const opts = { host : 'localhost' , port, timeout, jwt, cache : true }
138
135
await assert . throwsAsync ( ( ) => getScrud ( opts ) ( 'whatevs' , 'read' , 1 ) )
139
136
140
137
delete opts . cache
141
-
142
138
await assert . notThrowsAsync ( ( ) => getScrud ( opts ) ( 'whatevs' , 'read' , 1 ) )
143
139
} )
144
140
145
- test ( ` string resourceId doesn't throw` , async ( assert ) => {
141
+ test ( ' string resourceId doesn\ 't throw' , async ( assert ) => {
146
142
const resource = 'someresource'
147
143
const port = 7942
148
144
const read = ( req , res ) => scrud . sendData ( res , { id : req . id } )
@@ -157,3 +153,43 @@ test(`string resourceId doesn't throw`, async (assert) => {
157
153
158
154
assert . is ( data . id , id )
159
155
} )
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