77import fs from 'node:fs' ;
88import * as process from 'node:process' ;
99import path from 'node:path' ;
10+ import * as assert from 'node:assert' ;
1011import { SfError } from '@salesforce/core' ;
1112import { expect } from 'chai' ;
1213import stripAnsi from 'strip-ansi' ;
1314import { MockTestOrgData , TestContext } from '@salesforce/core/testSetup' ;
1415import { sleep } from '@salesforce/kit' ;
1516import nock = require( 'nock' ) ;
17+ import { stubUx } from '@salesforce/sf-plugins-core' ;
1618import { Rest } from '../../../../src/commands/api/request/rest.js' ;
1719
1820describe ( 'rest' , ( ) => {
@@ -21,8 +23,7 @@ describe('rest', () => {
2123 username : 'test@hub.com' ,
2224 } ) ;
2325
24- let stdoutSpy : sinon . SinonSpy ;
25-
26+ let uxStub : ReturnType < typeof stubUx > ;
2627 const orgLimitsResponse = {
2728 ActiveScratchOrgs : {
2829 Max : 200 ,
@@ -32,8 +33,7 @@ describe('rest', () => {
3233
3334 beforeEach ( async ( ) => {
3435 await $$ . stubAuths ( testOrg ) ;
35-
36- stdoutSpy = $$ . SANDBOX . stub ( process . stdout , 'write' ) ;
36+ uxStub = stubUx ( $$ . SANDBOX ) ;
3737 } ) ;
3838
3939 afterEach ( ( ) => {
@@ -45,21 +45,31 @@ describe('rest', () => {
4545
4646 await Rest . run ( [ 'services/data/v56.0/limits' , '--target-org' , 'test@hub.com' ] ) ;
4747
48- const output = stripAnsi ( stdoutSpy . args . flat ( ) . join ( '' ) ) ;
48+ expect ( uxStub . styledJSON . args [ 0 ] [ 0 ] ) . to . deep . equal ( orgLimitsResponse ) ;
49+ } ) ;
4950
50- expect ( JSON . parse ( output ) ) . to . deep . equal ( orgLimitsResponse ) ;
51+ it ( 'should throw error for invalid header args' , async ( ) => {
52+ try {
53+ await Rest . run ( [ 'services/data/v56.0/limits' , '--target-org' , 'test@hub.com' , '-H' , 'myInvalidHeader' ] ) ;
54+ assert . fail ( 'the above should throw' ) ;
55+ } catch ( e ) {
56+ expect ( ( e as SfError ) . name ) . to . equal ( 'Failed To Parse HTTP Header' ) ;
57+ expect ( ( e as SfError ) . message ) . to . equal ( 'Failed to parse HTTP header: "myInvalidHeader".' ) ;
58+ expect ( ( e as SfError ) . actions ) . to . deep . equal ( [
59+ 'Make sure the header is in a "key:value" format, e.g. "Accept: application/json"' ,
60+ ] ) ;
61+ }
5162 } ) ;
5263
5364 it ( 'should redirect to file' , async ( ) => {
5465 nock ( testOrg . instanceUrl ) . get ( '/services/data/v56.0/limits' ) . reply ( 200 , orgLimitsResponse ) ;
55-
66+ const writeSpy = $$ . SANDBOX . stub ( process . stdout , 'write' ) ;
5667 await Rest . run ( [ 'services/data/v56.0/limits' , '--target-org' , 'test@hub.com' , '--stream-to-file' , 'myOutput.txt' ] ) ;
5768
5869 // gives it a second to resolve promises and close streams before we start asserting
5970 await sleep ( 1000 ) ;
60- const output = stripAnsi ( stdoutSpy . args . flat ( ) . join ( '' ) ) ;
6171
62- expect ( output ) . to . deep . equal ( 'File saved to myOutput.txt' + '\n' ) ;
72+ expect ( writeSpy . args . flat ( ) . join ( '' ) ) . to . deep . equal ( 'File saved to myOutput.txt' + '\n' ) ;
6373 expect ( JSON . parse ( fs . readFileSync ( 'myOutput.txt' , 'utf8' ) ) ) . to . deep . equal ( orgLimitsResponse ) ;
6474
6575 after ( ( ) => {
@@ -76,6 +86,7 @@ describe('rest', () => {
7686 <Remaining>198</Remaining>
7787 </ActiveScratchOrgs>
7888</LimitsSnapshot>` ;
89+ const writeSpy = $$ . SANDBOX . stub ( process . stdout , 'write' ) ;
7990
8091 nock ( testOrg . instanceUrl , {
8192 reqheaders : {
@@ -87,7 +98,7 @@ describe('rest', () => {
8798
8899 await Rest . run ( [ 'services/data' , '--header' , 'Accept: application/xml' , '--target-org' , 'test@hub.com' ] ) ;
89100
90- const output = stripAnsi ( stdoutSpy . args . flat ( ) . join ( '' ) ) ;
101+ const output = stripAnsi ( writeSpy . args . flat ( ) . join ( '' ) ) ;
91102
92103 // https://github.com/oclif/core/blob/ff76400fb0bdfc4be0fa93056e86183b9205b323/src/command.ts#L248-L253
93104 expect ( output ) . to . equal ( xmlRes + '\n' ) ;
@@ -117,8 +128,6 @@ describe('rest', () => {
117128
118129 await Rest . run ( [ 'services/data/v56.0/limites' , '--target-org' , 'test@hub.com' ] ) ;
119130
120- const output = stripAnsi ( stdoutSpy . args . flat ( ) . join ( '' ) ) ;
121-
122- expect ( JSON . parse ( output ) ) . to . deep . equal ( orgLimitsResponse ) ;
131+ expect ( uxStub . styledJSON . args [ 0 ] [ 0 ] ) . to . deep . equal ( orgLimitsResponse ) ;
123132 } ) ;
124133} ) ;
0 commit comments