@@ -3,7 +3,6 @@ const t = require('tap')
33
44const checkResponse = require ( '../lib/check-response.js' )
55const errors = require ( '../lib/errors.js' )
6- const silentLog = require ( '../lib/silentlog.js' )
76const registry = 'registry'
87const startTime = Date . now ( )
98
@@ -56,27 +55,27 @@ t.test('all checks are ok, nothing to report', t => {
5655 t . end ( )
5756} )
5857
59- t . test ( 'log x-fetch-attempts header value' , t => {
58+ t . test ( 'log x-fetch-attempts header value' , async t => {
6059 const headers = new Headers ( )
6160 headers . get = header => header === 'x-fetch-attempts' ? 3 : undefined
6261 const res = Object . assign ( { } , mockFetchRes , {
6362 headers,
6463 status : 400 ,
6564 } )
6665 t . plan ( 2 )
67- t . rejects ( checkResponse ( {
66+ let msg
67+ process . on ( 'log' , ( level , ...args ) => {
68+ if ( level === 'http' ) {
69+ ; [ , msg ] = args
70+ }
71+ } )
72+ await t . rejects ( checkResponse ( {
6873 method : 'get' ,
6974 res,
7075 registry,
7176 startTime,
72- opts : {
73- log : Object . assign ( { } , silentLog , {
74- http ( header , msg ) {
75- t . ok ( msg . endsWith ( 'attempt #3' ) , 'should log correct number of attempts' )
76- } ,
77- } ) ,
78- } ,
7977 } ) )
78+ t . ok ( msg . endsWith ( 'attempt #3' ) , 'should log correct number of attempts' )
8079} )
8180
8281t . test ( 'log the url fetched' , t => {
@@ -90,21 +89,22 @@ t.test('log the url fetched', t => {
9089 body : new EE ( ) ,
9190 } )
9291 t . plan ( 2 )
92+ let header , msg
93+ process . on ( 'log' , ( level , ...args ) => {
94+ if ( level === 'http' ) {
95+ ; [ header , msg ] = args
96+ }
97+ } )
9398 checkResponse ( {
9499 method : 'get' ,
95100 res,
96101 registry,
97102 startTime,
98- opts : {
99- log : Object . assign ( { } , silentLog , {
100- http ( header , msg ) {
101- t . equal ( header , 'fetch' )
102- t . match ( msg , / ^ G E T 2 0 0 h t t p : \/ \/ e x a m p l e .c o m \/ f o o \/ b a r \/ b a z [ 0 - 9 ] + m ? s / )
103- } ,
104- } ) ,
105- } ,
103+
106104 } )
107105 res . body . emit ( 'end' )
106+ t . equal ( header , 'fetch' )
107+ t . match ( msg , / ^ G E T 2 0 0 h t t p : \/ \/ e x a m p l e .c o m \/ f o o \/ b a r \/ b a z [ 0 - 9 ] + m ? s / )
108108} )
109109
110110t . test ( 'redact password from log' , t => {
@@ -118,34 +118,40 @@ t.test('redact password from log', t => {
118118 body : new EE ( ) ,
119119 } )
120120 t . plan ( 2 )
121+ let header , msg
122+ process . on ( 'log' , ( level , ...args ) => {
123+ if ( level === 'http' ) {
124+ ; [ header , msg ] = args
125+ }
126+ } )
121127 checkResponse ( {
122128 method : 'get' ,
123129 res,
124130 registry,
125131 startTime,
126- opts : {
127- log : Object . assign ( { } , silentLog , {
128- http ( header , msg ) {
129- t . equal ( header , 'fetch' )
130- t . match ( msg , / ^ G E T 2 0 0 h t t p : \/ \/ u s e r n a m e : \* \* \* @ e x a m p l e .c o m \/ f o o \/ b a r \/ b a z [ 0 - 9 ] + m ? s / )
131- } ,
132- } ) ,
133- } ,
134132 } )
135133 res . body . emit ( 'end' )
134+ t . equal ( header , 'fetch' )
135+ t . match ( msg , / ^ G E T 2 0 0 h t t p : \/ \/ u s e r n a m e : \* \* \* @ e x a m p l e .c o m \/ f o o \/ b a r \/ b a z [ 0 - 9 ] + m ? s / )
136136} )
137137
138138/* eslint-disable-next-line max-len */
139139const moreInfoUrl = 'https://github.com/npm/cli/wiki/No-auth-for-URI,-but-auth-present-for-scoped-registry'
140140
141- t . test ( 'report auth for registry, but not for this request' , t => {
141+ t . test ( 'report auth for registry, but not for this request' , async t => {
142142 const res = Object . assign ( { } , mockFetchRes , {
143143 buffer : ( ) => Promise . resolve ( Buffer . from ( 'ok' ) ) ,
144144 status : 400 ,
145145 url : 'https://example.com/' ,
146146 } )
147147 t . plan ( 3 )
148- t . rejects ( checkResponse ( {
148+ let header , msg
149+ process . on ( 'log' , ( level , ...args ) => {
150+ if ( level === 'warn' ) {
151+ ; [ header , msg ] = args
152+ }
153+ } )
154+ await t . rejects ( checkResponse ( {
149155 method : 'get' ,
150156 res,
151157 uri : 'https://example.com/' ,
@@ -156,20 +162,14 @@ t.test('report auth for registry, but not for this request', t => {
156162 auth : null ,
157163 token : null ,
158164 } ,
159- opts : {
160- log : Object . assign ( { } , silentLog , {
161- warn ( header , msg ) {
162- t . equal ( header , 'registry' )
163- t . equal ( msg , `No auth for URI, but auth present for scoped registry.
165+ } ) , errors . HttpErrorGeneral )
166+ t . equal ( header , 'registry' )
167+ t . equal ( msg , `No auth for URI, but auth present for scoped registry.
164168
165169URI: https://example.com/
166170Scoped Registry Key: //some-scope-registry.com/
167171
168172More info here: ${ moreInfoUrl } ` )
169- } ,
170- } ) ,
171- } ,
172- } ) , errors . HttpErrorGeneral )
173173} )
174174
175175t . test ( 'logs the value of x-local-cache-status when set' , t => {
@@ -183,22 +183,22 @@ t.test('logs the value of x-local-cache-status when set', t => {
183183 body : new EE ( ) ,
184184 } )
185185 t . plan ( 2 )
186+ let header , msg
187+ process . on ( 'log' , ( level , ...args ) => {
188+ if ( level === 'http' ) {
189+ ; [ header , msg ] = args
190+ }
191+ } )
186192 checkResponse ( {
187193 method : 'get' ,
188194 res,
189195 registry,
190196 startTime,
191- opts : {
192- log : Object . assign ( { } , silentLog , {
193- http ( header , msg ) {
194- t . equal ( header , 'fetch' )
195- t . match (
196- msg ,
197- / ^ G E T 2 0 0 h t t p : \/ \/ u s e r n a m e : \* \* \* @ e x a m p l e .c o m \/ f o o \/ b a r \/ b a z [ 0 - 9 ] + m ? s \( c a c h e h i t \) $ /
198- )
199- } ,
200- } ) ,
201- } ,
202197 } )
203198 res . body . emit ( 'end' )
199+ t . equal ( header , 'fetch' )
200+ t . match (
201+ msg ,
202+ / ^ G E T 2 0 0 h t t p : \/ \/ u s e r n a m e : \* \* \* @ e x a m p l e .c o m \/ f o o \/ b a r \/ b a z [ 0 - 9 ] + m ? s \( c a c h e h i t \) $ /
203+ )
204204} )
0 commit comments