@@ -33,29 +33,35 @@ const DIFF_OPTIONS = {
3333 */
3434async function getFinalRedirect ( url , method = 'GET' ) {
3535 return new Promise ( ( resolve , reject ) => {
36- let cmd = `curl -o /dev/null -sL -k -w "%{url_effective}" -X ${ method } -I "${ url } "` ;
36+ let cmd = `curl --output /dev/null --silent --show-error --fail --location --write-out '{"data":%{json},"headers":%{header_json}}' --user-agent 'studiometa/cli-test-redirection bot' --request ${ method } "${ url } "` ;
3737
3838 if ( OPTIONS . user ) {
3939 cmd += ` --user ${ OPTIONS . user } ` ;
4040 }
4141
42- exec ( cmd , ( error , out ) => {
42+ exec ( cmd , ( error , stdout ) => {
43+ const out = JSON . parse ( stdout ) ;
4344 if ( error ) {
4445 if ( OPTIONS . verbose ) {
46+ console . log ( chalk . red ( '[ERROR]' , chalk . underline ( url ) ) ) ;
47+ console . log ( chalk . red ( '[ERROR]' , out . data . errormsg ) ) ;
48+ if ( out . data . http_code >= 500 ) {
49+ console . log (
50+ chalk . red (
51+ `[ERROR] The error probably comes from a syntax error in your .htaccess file.`
52+ )
53+ ) ;
54+ }
4555 console . log (
4656 chalk . red (
47- 'An error occured while doing a request. This might be caused by an infinite redirection loop. '
57+ '[ERROR] Try running the following command to confirm the error:\n '
4858 )
4959 ) ;
50- console . log (
51- chalk . red ( 'Try running the following command to confirm this:\n' )
52- ) ;
5360 console . log ( chalk . red ( `\tcurl -sIL -X ${ method } "${ url } "\n` ) ) ;
54- console . log ( chalk . red ( error . message ) ) ;
5561 }
5662 reject ( { error, url, method, cmd, out } ) ;
5763 }
58- resolve ( decodeURI ( out ) ) ;
64+ resolve ( out ) ;
5965 } ) ;
6066 } ) ;
6167}
@@ -129,12 +135,13 @@ async function redirectionTest({ options, total }) {
129135 }
130136
131137 return new Promise ( async ( resolve , reject ) => {
132- let out ;
138+ let out , error , url ;
133139
134140 try {
135141 out = await getFinalRedirect ( from , options . method ) ;
136- } catch ( error ) {
137- out = error ;
142+ url = out . data . url_effective ;
143+ } catch ( err ) {
144+ error = err ;
138145 }
139146
140147 if ( OPTIONS . ignoreQueryParameters || options . ignoreQueryParameters ) {
@@ -150,10 +157,12 @@ async function redirectionTest({ options, total }) {
150157 `[${ current . toString ( ) . padStart ( total . toString ( ) . length ) } /${ total } ]`
151158 ) ;
152159
153- if ( typeof out !== 'string' ) {
154- const msg = `π ${ chalk . white ( from ) } \n β ${ chalk . magenta (
160+ if ( error ) {
161+ const msg = `β ${ chalk . white ( from ) } \n β ${ chalk . red ( '-' ) } ${ chalk . red . strikethrough (
155162 to
156- ) } \n β ${ chalk . magentaBright ( out . out ) } (potential infinite loop)`;
163+ ) } \n β ${ chalk . magentaBright ( '+ ' + error . out . data . url ) } (${
164+ error . out . data . errormsg
165+ } )`;
157166
158167 if ( ! OPTIONS . verbose ) {
159168 printError ( count + ' ' + msg ) ;
@@ -163,11 +172,13 @@ async function redirectionTest({ options, total }) {
163172
164173 await delayingFn ( OPTIONS . delay ) ;
165174 reject ( { msg, from, to, out } ) ;
166- } else if ( out !== to ) {
167- const msg = `π« ${ chalk . white ( from ) } \n β ${ chalk . red ( '-' ) } ${ chalk . red . strikethrough (
168- to
169- ) } \n β ${ chalk . magentaBright ( `+ ${ out } ` ) } `;
170- const diff = diffStringsUnified ( to , out , DIFF_OPTIONS ) ;
175+ } else if ( url !== to ) {
176+ const msg = `π« ${ chalk . white ( from ) } \n β ${ chalk . red (
177+ '-'
178+ ) } ${ chalk . red . strikethrough ( to ) } \n β ${ chalk . magentaBright (
179+ `+ ${ url } `
180+ ) } `;
181+ const diff = diffStringsUnified ( to , url , DIFF_OPTIONS ) ;
171182
172183 if ( ! OPTIONS . verbose ) {
173184 printError ( count + ' ' + msg ) ; // write text
@@ -176,7 +187,7 @@ async function redirectionTest({ options, total }) {
176187 }
177188
178189 await delayingFn ( OPTIONS . delay ) ;
179- reject ( { msg, from, to, out , diff } ) ;
190+ reject ( { msg, from, to, url , diff } ) ;
180191 } else {
181192 const msg = `β
${ chalk . white ( from ) } \n ${ chalk . black ( 'β' ) } ${ chalk . blue (
182193 to
@@ -191,7 +202,7 @@ async function redirectionTest({ options, total }) {
191202 }
192203
193204 await delayingFn ( OPTIONS . delay ) ;
194- resolve ( { msg, from, to, out } ) ;
205+ resolve ( { msg, from, to, url } ) ;
195206 }
196207 } ) ;
197208}
0 commit comments