@@ -6,63 +6,93 @@ const pkg = require('../../package.json');
6
6
7
7
describe ( '#fetch()' , ( ) => {
8
8
describe ( 'GitHub Actions environment' , ( ) => {
9
+ // List of all GitHub Actions env variables:
10
+ // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
9
11
beforeEach ( ( ) => {
10
12
process . env . GITHUB_ACTIONS = 'true' ;
13
+ process . env . GITHUB_REPOSITORY = 'octocat/Hello-World' ;
14
+ process . env . GITHUB_RUN_ATTEMPT = '3' ;
15
+ process . env . GITHUB_RUN_ID = '1658821493' ;
16
+ process . env . GITHUB_RUN_NUMBER = '3' ;
17
+ process . env . GITHUB_SHA = 'ffac537e6cbbf934b08745a378932722df287a53' ;
11
18
} ) ;
12
19
13
20
afterEach ( ( ) => {
14
21
delete process . env . GITHUB_ACTIONS ;
22
+ delete process . env . GITHUB_REPOSITORY ;
23
+ delete process . env . GITHUB_RUN_ATTEMPT ;
24
+ delete process . env . GITHUB_RUN_ID ;
25
+ delete process . env . GITHUB_RUN_NUMBER ;
26
+ delete process . env . GITHUB_SHA ;
15
27
} ) ;
16
28
17
- it ( 'should use correct user-agent for requests in GitHub Action env' , async ( ) => {
29
+ it ( 'should have correct headers for requests in GitHub Action env' , async ( ) => {
18
30
const key = 'API_KEY' ;
19
31
20
32
const mock = getApiNock ( )
21
33
. get ( '/api/v1' )
22
34
. basicAuth ( { user : key } )
23
35
. reply ( 200 , function ( ) {
24
- return this . req . headers [ 'user-agent' ] ;
36
+ return this . req . headers ;
25
37
} ) ;
26
38
27
- const userAgent = await fetch ( `${ config . get ( 'host' ) } /api/v1` , {
39
+ const headers = await fetch ( `${ config . get ( 'host' ) } /api/v1` , {
28
40
method : 'get' ,
29
41
headers : cleanHeaders ( key ) ,
30
42
} ) . then ( handleRes ) ;
31
43
32
- expect ( userAgent . shift ( ) ) . toBe ( `rdme-github/${ pkg . version } ` ) ;
44
+ expect ( headers [ 'user-agent' ] . shift ( ) ) . toBe ( `rdme-github/${ pkg . version } ` ) ;
45
+ expect ( headers [ 'x-readme-source' ] . shift ( ) ) . toBe ( 'cli-gh' ) ;
46
+ expect ( headers [ 'x-github-repository' ] . shift ( ) ) . toBe ( 'octocat/Hello-World' ) ;
47
+ expect ( headers [ 'x-github-run-attempt' ] . shift ( ) ) . toBe ( '3' ) ;
48
+ expect ( headers [ 'x-github-run-id' ] . shift ( ) ) . toBe ( '1658821493' ) ;
49
+ expect ( headers [ 'x-github-run-number' ] . shift ( ) ) . toBe ( '3' ) ;
50
+ expect ( headers [ 'x-github-sha' ] . shift ( ) ) . toBe ( 'ffac537e6cbbf934b08745a378932722df287a53' ) ;
33
51
mock . done ( ) ;
34
52
} ) ;
35
53
} ) ;
36
54
37
- it ( 'should wrap all requests with a rdme User-Agent ' , async ( ) => {
55
+ it ( 'should wrap all requests with standard user-agent and source headers ' , async ( ) => {
38
56
const key = 'API_KEY' ;
39
57
40
58
const mock = getApiNock ( )
41
59
. get ( '/api/v1' )
42
60
. basicAuth ( { user : key } )
43
61
. reply ( 200 , function ( ) {
44
- return this . req . headers [ 'user-agent' ] ;
62
+ return this . req . headers ;
45
63
} ) ;
46
64
47
- const userAgent = await fetch ( `${ config . get ( 'host' ) } /api/v1` , {
65
+ const headers = await fetch ( `${ config . get ( 'host' ) } /api/v1` , {
48
66
method : 'get' ,
49
67
headers : cleanHeaders ( key ) ,
50
68
} ) . then ( handleRes ) ;
51
69
52
- expect ( userAgent . shift ( ) ) . toBe ( `rdme/${ pkg . version } ` ) ;
70
+ expect ( headers [ 'user-agent' ] . shift ( ) ) . toBe ( `rdme/${ pkg . version } ` ) ;
71
+ expect ( headers [ 'x-readme-source' ] . shift ( ) ) . toBe ( 'cli' ) ;
72
+ expect ( headers [ 'x-github-repository' ] ) . toBeUndefined ( ) ;
73
+ expect ( headers [ 'x-github-run-attempt' ] ) . toBeUndefined ( ) ;
74
+ expect ( headers [ 'x-github-run-id' ] ) . toBeUndefined ( ) ;
75
+ expect ( headers [ 'x-github-run-number' ] ) . toBeUndefined ( ) ;
76
+ expect ( headers [ 'x-github-sha' ] ) . toBeUndefined ( ) ;
53
77
mock . done ( ) ;
54
78
} ) ;
55
79
56
80
it ( 'should support if we dont supply any other options with the request' , async ( ) => {
57
81
const mock = getApiNock ( )
58
82
. get ( '/api/v1/doesnt-need-auth' )
59
83
. reply ( 200 , function ( ) {
60
- return this . req . headers [ 'user-agent' ] ;
84
+ return this . req . headers ;
61
85
} ) ;
62
86
63
- const userAgent = await fetch ( `${ config . get ( 'host' ) } /api/v1/doesnt-need-auth` ) . then ( handleRes ) ;
87
+ const headers = await fetch ( `${ config . get ( 'host' ) } /api/v1/doesnt-need-auth` ) . then ( handleRes ) ;
64
88
65
- expect ( userAgent . shift ( ) ) . toBe ( `rdme/${ pkg . version } ` ) ;
89
+ expect ( headers [ 'user-agent' ] . shift ( ) ) . toBe ( `rdme/${ pkg . version } ` ) ;
90
+ expect ( headers [ 'x-readme-source' ] . shift ( ) ) . toBe ( 'cli' ) ;
91
+ expect ( headers [ 'x-github-repository' ] ) . toBeUndefined ( ) ;
92
+ expect ( headers [ 'x-github-run-attempt' ] ) . toBeUndefined ( ) ;
93
+ expect ( headers [ 'x-github-run-id' ] ) . toBeUndefined ( ) ;
94
+ expect ( headers [ 'x-github-run-number' ] ) . toBeUndefined ( ) ;
95
+ expect ( headers [ 'x-github-sha' ] ) . toBeUndefined ( ) ;
66
96
mock . done ( ) ;
67
97
} ) ;
68
98
} ) ;
0 commit comments