@@ -18,6 +18,9 @@ function fixture(p) {
18
18
const googleHumansTxt =
19
19
"Google is built by a large team of engineers, designers, researchers, robots, and others in many different sites across the globe. It is updated continuously, and built with more tools and technologies than we can shake a stick at. If you'd like to help us out, see careers.google.com.\n"
20
20
21
+ function ms ( p ) {
22
+ return Metalsmith ( fixture ( p ) ) . env ( 'DEBUG' , process . env . DEBUG )
23
+ }
21
24
describe ( '@metalsmith/requests' , function ( ) {
22
25
let server
23
26
@@ -44,7 +47,7 @@ describe('@metalsmith/requests', function () {
44
47
} )
45
48
46
49
it ( 'should not crash the metalsmith build when using default options' , function ( done ) {
47
- Metalsmith ( fixture ( 'default' ) )
50
+ ms ( 'default' )
48
51
. use ( plugin ( ) )
49
52
. build ( ( err ) => {
50
53
if ( err ) done ( err )
@@ -81,41 +84,41 @@ describe('@metalsmith/requests', function () {
81
84
} )
82
85
83
86
it ( "out.key && !out.path: should assign a response's data to global metadata" , function ( done ) {
84
- const ms = Metalsmith ( fixture ( 'default' ) )
87
+ const m = ms ( 'default' )
85
88
const files = { }
86
89
const config = {
87
90
url : 'https://www.google.com/humans.txt' ,
88
91
out : { key : 'googlehumans' }
89
92
}
90
- plugin ( config ) ( files , ms , ( err ) => {
93
+ plugin ( config ) ( files , m , ( err ) => {
91
94
if ( err ) done ( err )
92
- assert . strictEqual ( ms . metadata ( ) . googlehumans , googleHumansTxt )
95
+ assert . strictEqual ( m . metadata ( ) . googlehumans , googleHumansTxt )
93
96
done ( )
94
97
} )
95
98
} )
96
99
97
100
it ( "!out.key && out.path: should assign a response's data to a file's 'contents' by default" , function ( done ) {
98
- const ms = Metalsmith ( fixture ( 'default' ) )
101
+ const m = ms ( 'default' )
99
102
const files = { 'test.md' : { } }
100
103
const config = {
101
104
url : 'https://www.google.com/humans.txt' ,
102
105
out : { path : 'test.md' }
103
106
}
104
- plugin ( config ) ( files , ms , ( err ) => {
107
+ plugin ( config ) ( files , m , ( err ) => {
105
108
if ( err ) done ( err )
106
109
assert . strictEqual ( files [ 'test.md' ] . contents . toString ( ) , googleHumansTxt )
107
110
done ( )
108
111
} )
109
112
} )
110
113
111
114
it ( "out.key && out.path: should assign a response's data to file metadata at files[out.path][out.key]" , function ( done ) {
112
- const ms = Metalsmith ( fixture ( 'default' ) )
115
+ const m = ms ( 'default' )
113
116
const files = { 'test.md' : { layout : 'default.njk' } }
114
117
const config = {
115
118
url : 'https://www.google.com/humans.txt' ,
116
119
out : { path : 'test.md' , key : 'request' }
117
120
}
118
- plugin ( config ) ( files , ms , ( err ) => {
121
+ plugin ( config ) ( files , m , ( err ) => {
119
122
if ( err ) done ( err )
120
123
assert . strictEqual ( files [ 'test.md' ] . request , googleHumansTxt )
121
124
assert . strictEqual ( files [ 'test.md' ] . layout , 'default.njk' )
@@ -125,7 +128,7 @@ describe('@metalsmith/requests', function () {
125
128
} )
126
129
127
130
it ( 'should treat files with a "request" key in metadata as entries' , function ( done ) {
128
- Metalsmith ( fixture ( 'implicit-entries' ) )
131
+ ms ( 'implicit-entries' )
129
132
. use ( plugin ( ) )
130
133
. build ( ( err ) => {
131
134
if ( err ) return done ( err )
@@ -135,7 +138,7 @@ describe('@metalsmith/requests', function () {
135
138
} )
136
139
137
140
it ( 'should replace path placeholders in out and url options accordingly' , function ( done ) {
138
- Metalsmith ( fixture ( 'placeholders' ) )
141
+ ms ( 'placeholders' )
139
142
. use (
140
143
plugin ( {
141
144
url : 'https://webketje.com/assets/css/:filename.css' ,
@@ -151,7 +154,7 @@ describe('@metalsmith/requests', function () {
151
154
} )
152
155
153
156
it ( 'should allow specifying shorthands' , function ( done ) {
154
- Metalsmith ( fixture ( 'single-request-option' ) )
157
+ ms ( 'single-request-option' )
155
158
. use (
156
159
plugin ( {
157
160
url : 'https://webketje.com/assets/css/main.css' ,
@@ -166,8 +169,8 @@ describe('@metalsmith/requests', function () {
166
169
} )
167
170
168
171
it ( "should auto-parse a response as JSON when the response's Content-Type header is application/json" , function ( done ) {
169
- const ms = Metalsmith ( fixture ( 'default' ) )
170
- ms . use (
172
+ const m = ms ( 'default' )
173
+ m . use (
171
174
plugin ( {
172
175
url : 'https://api.github.com/repos/metalsmith/drafts/contents/README.md' ,
173
176
out : { key : 'readme' } ,
@@ -182,7 +185,7 @@ describe('@metalsmith/requests', function () {
182
185
) . process ( ( err ) => {
183
186
if ( err ) done ( err )
184
187
assert . strictEqual (
185
- ms . metadata ( ) . readme . download_url ,
188
+ m . metadata ( ) . readme . download_url ,
186
189
'https://raw.githubusercontent.com/metalsmith/drafts/main/README.md'
187
190
)
188
191
done ( )
@@ -200,8 +203,8 @@ describe('@metalsmith/requests', function () {
200
203
}
201
204
}
202
205
}`
203
- const ms = Metalsmith ( fixture ( 'default' ) )
204
- ms . use (
206
+ const m = ms ( 'default' )
207
+ m . use (
205
208
plugin ( {
206
209
url : 'https://api.github.com/graphql' ,
207
210
out : { key : 'sassreadme' } ,
@@ -216,7 +219,7 @@ describe('@metalsmith/requests', function () {
216
219
) . process ( ( err ) => {
217
220
if ( err ) done ( err )
218
221
assert . strictEqual (
219
- ms . metadata ( ) . sassreadme . data . repository . object . text . slice ( 0 , 39 ) ,
222
+ m . metadata ( ) . sassreadme . data . repository . object . text . slice ( 0 , 39 ) ,
220
223
'# @metalsmith/sass\n\nA Metalsmith plugin'
221
224
)
222
225
done ( )
@@ -225,7 +228,7 @@ describe('@metalsmith/requests', function () {
225
228
226
229
describe ( 'should throw error' , function ( ) {
227
230
it ( '"unsupported_protocol" when the protocol is not supported' , function ( done ) {
228
- Metalsmith ( fixture ( 'default' ) )
231
+ ms ( 'default' )
229
232
. use (
230
233
plugin ( {
231
234
url : 'ftp://bad-protocol.com' ,
@@ -239,7 +242,7 @@ describe('@metalsmith/requests', function () {
239
242
} )
240
243
241
244
it ( '"ERR_INVALID_URL" when the URL is invalid' , function ( done ) {
242
- Metalsmith ( fixture ( 'default' ) )
245
+ ms ( 'default' )
243
246
. use (
244
247
plugin ( {
245
248
url : 'random-invalid' ,
@@ -257,7 +260,7 @@ describe('@metalsmith/requests', function () {
257
260
} )
258
261
259
262
it ( '"invalid_http_method" when the HTTP method is invalid' , function ( done ) {
260
- Metalsmith ( fixture ( 'default' ) )
263
+ ms ( 'default' )
261
264
. use (
262
265
plugin ( {
263
266
url : 'http://bad-httpmethod.com' ,
@@ -274,7 +277,7 @@ describe('@metalsmith/requests', function () {
274
277
} )
275
278
276
279
it ( '"invalid_outconfig" when the outconfig is invalid' , function ( done ) {
277
- Metalsmith ( fixture ( 'default' ) )
280
+ ms ( 'default' )
278
281
. use (
279
282
plugin ( {
280
283
url : 'http://bad-outoption.com' ,
@@ -288,7 +291,7 @@ describe('@metalsmith/requests', function () {
288
291
} )
289
292
290
293
it ( '"invalid_outconfig" when the outconfig is of the wrong type' , function ( done ) {
291
- Metalsmith ( fixture ( 'default' ) )
294
+ ms ( 'default' )
292
295
. use (
293
296
plugin ( {
294
297
url : 'http://bad-outoption.com' ,
@@ -305,7 +308,7 @@ describe('@metalsmith/requests', function () {
305
308
if ( ! process . env . GITHUB_TOKEN ) {
306
309
this . skip ( )
307
310
}
308
- Metalsmith ( fixture ( 'default' ) )
311
+ ms ( 'default' )
309
312
. use (
310
313
plugin ( {
311
314
url : 'https://graphql.github.com/graphql' ,
@@ -327,16 +330,17 @@ describe('@metalsmith/requests', function () {
327
330
} )
328
331
329
332
it ( '"invalid_json" when the response has Content-Type: application/json and is malformed' , function ( done ) {
330
- const ms = Metalsmith ( fixture ( 'default' ) )
331
- ms . use (
332
- plugin ( {
333
- url : 'http://localhost:3000' ,
334
- out : { key : 'googlehumans' }
333
+ ms ( 'default' )
334
+ . use (
335
+ plugin ( {
336
+ url : 'http://localhost:3000' ,
337
+ out : { key : 'googlehumans' }
338
+ } )
339
+ )
340
+ . process ( ( err ) => {
341
+ assert . strictEqual ( err . name , 'invalid_json' )
342
+ done ( )
335
343
} )
336
- ) . process ( ( err ) => {
337
- assert . strictEqual ( err . name , 'invalid_json' )
338
- done ( )
339
- } )
340
344
} )
341
345
} )
342
346
} )
0 commit comments