1
+ const os = require ( 'os' ) ;
2
+
1
3
const { expect, test } = require ( '@twilio/cli-test' ) ;
2
4
3
5
const CliRequestClient = require ( '../../src/services/cli-http-client' ) ;
4
6
const { TwilioCliError } = require ( '../../src/services/error' ) ;
5
7
const { Logger, LoggingLevel } = require ( '../../src/services/messaging/logging' ) ;
8
+ const pkg = require ( '../../package.json' ) ;
6
9
7
10
describe ( 'services' , ( ) => {
8
11
describe ( 'cli-http-client' , ( ) => {
@@ -19,9 +22,11 @@ describe('services', () => {
19
22
return { status : 200 , data : 'foo' , headers : { } } ;
20
23
} ,
21
24
true ,
25
+ 'blah' ,
22
26
) ;
23
27
expect ( client . commandName ) . to . equal ( 'blah' ) ;
24
28
expect ( client . keytarWord ) . to . equal ( 'keytar' ) ;
29
+ expect ( client . pluginName ) . to . equal ( 'blah' ) ;
25
30
const response = await client . request ( {
26
31
method : 'POST' ,
27
32
uri : 'https://foo.com/bar' ,
@@ -38,9 +43,10 @@ describe('services', () => {
38
43
39
44
test . it ( 'should add the correct http agent for proxy' , async ( ) => {
40
45
process . env . HTTP_PROXY = 'http://someproxy.com:8080' ;
41
- const client = new CliRequestClient ( 'blah' , logger , { defaults : { } } , false ) ;
46
+ const client = new CliRequestClient ( 'blah' , logger , { defaults : { } } , false , 'blah' ) ;
42
47
const httpAgent = client . http . defaults . httpsAgent ;
43
48
expect ( client . keytarWord ) . to . equal ( '' ) ;
49
+ expect ( client . pluginName ) . to . equal ( 'blah' ) ;
44
50
expect ( httpAgent . proxy . host ) . to . equal ( 'someproxy.com' ) ;
45
51
expect ( httpAgent . proxy . port ) . to . equal ( 8080 ) ;
46
52
} ) ;
@@ -65,6 +71,45 @@ describe('services', () => {
65
71
await expect ( request ) . to . be . rejectedWith ( TwilioCliError ) ;
66
72
} ) ;
67
73
74
+ test
75
+ . nock ( 'https://foo.com' , ( api ) => {
76
+ api . get ( '/bar' ) . reply ( 200 , '' , {
77
+ 'User-Agent' : `twilio-cli/2.27.1 ${ pkg . name } \/${
78
+ pkg . version
79
+ } \(${ os . platform ( ) } ${ os . arch ( ) } \) dummyCommand keytar`,
80
+ } ) ;
81
+ } )
82
+ . it ( 'correctly sets user-agent' , async ( ) => {
83
+ const client = new CliRequestClient ( 'dummyCommand' , logger , '' , true , 'twilio-cli/2.27.1' ) ;
84
+ const response = await client . request ( {
85
+ method : 'GET' ,
86
+ uri : 'https://foo.com/bar' ,
87
+ } ) ;
88
+ expect ( client . keytarWord ) . to . equal ( 'keytar' ) ;
89
+ expect ( client . lastRequest . headers [ 'User-Agent' ] ) . to . equal (
90
+ `twilio-cli/2.27.1 ${ pkg . name } \/${ pkg . version } \(${ os . platform ( ) } ${ os . arch ( ) } \) dummyCommand keytar` ,
91
+ ) ;
92
+ expect ( response . statusCode ) . to . equal ( 200 ) ;
93
+ } ) ;
94
+
95
+ test
96
+ . nock ( 'https://foo.com' , ( api ) => {
97
+ api . get ( '/bar' ) . reply ( 200 , '' , {
98
+ 'User-Agent' : ` ${ pkg . name } \/${ pkg . version } \(${ os . platform ( ) } ${ os . arch ( ) } \) dummyCommand keytar` ,
99
+ } ) ;
100
+ } )
101
+ . it ( 'correctly sets user-agent with empty plugin value' , async ( ) => {
102
+ const client = new CliRequestClient ( 'dummyCommand' , logger , '' , true , '' ) ;
103
+ const response = await client . request ( {
104
+ method : 'GET' ,
105
+ uri : 'https://foo.com/bar' ,
106
+ } ) ;
107
+ expect ( client . lastRequest . headers [ 'User-Agent' ] ) . to . equal (
108
+ ` ${ pkg . name } \/${ pkg . version } \(${ os . platform ( ) } ${ os . arch ( ) } \) dummyCommand keytar` ,
109
+ ) ;
110
+ expect ( response . statusCode ) . to . equal ( 200 ) ;
111
+ } ) ;
112
+
68
113
test
69
114
. nock ( 'https://foo.com' , ( api ) => {
70
115
api . get ( '/bar?foo=bar&foo=baz' ) . reply ( 200 ) ;
0 commit comments