1
1
const { fail } = require ( 'assert' )
2
- const { callFetch, withDefaults } = require ( '../../../lib/fetch' )
2
+ const { callFetch, withDefaults, defaultHeaders } = require ( '../../../lib/fetch' )
3
3
const { expect } = require ( 'chai' )
4
4
const fs = require ( 'fs' )
5
5
const mockttp = require ( 'mockttp' )
6
6
7
+ function checkDefaultHeaders ( headers ) {
8
+ for ( const [ key , value ] of Object . entries ( defaultHeaders ) ) {
9
+ expect ( headers ) . to . have . property ( key . toLowerCase ( ) ) . that . equals ( value )
10
+ }
11
+ }
7
12
describe ( 'CallFetch' , ( ) => {
8
13
describe ( 'with mock server' , ( ) => {
9
14
const mockServer = mockttp . getLocal ( )
@@ -23,6 +28,37 @@ describe('CallFetch', () => {
23
28
expect ( response ) . to . be . deep . equal ( JSON . parse ( expected ) )
24
29
} )
25
30
31
+ it ( 'checks if the default header user-agent and other header is present in crate components' , async ( ) => {
32
+ const path = '/crates.io/api/v1/crates/name/1.0.0/download'
33
+ const endpointMock = await mockServer . forGet ( path ) . thenReply ( 200 , 'success' )
34
+
35
+ await callFetch ( {
36
+ url : mockServer . urlFor ( path ) ,
37
+ method : 'GET' ,
38
+ json : true ,
39
+ encoding : null ,
40
+ headers : {
41
+ Accept : 'text/html'
42
+ }
43
+ } )
44
+ const requests = await endpointMock . getSeenRequests ( )
45
+ checkDefaultHeaders ( requests [ 0 ] . headers )
46
+ expect ( requests [ 0 ] . headers ) . to . include ( { accept : 'text/html' } )
47
+ } )
48
+
49
+ it ( 'checks if the default header user-agent is present in crate components' , async ( ) => {
50
+ const path = '/crates.io/api/v1/crates/name'
51
+ const endpointMock = await mockServer . forGet ( path ) . thenReply ( 200 , 'success' )
52
+
53
+ await callFetch ( {
54
+ url : mockServer . urlFor ( path ) ,
55
+ method : 'GET' ,
56
+ json : true
57
+ } )
58
+ const requests = await endpointMock . getSeenRequests ( )
59
+ checkDefaultHeaders ( requests [ 0 ] . headers )
60
+ } )
61
+
26
62
it ( 'checks if the full response is fetched' , async ( ) => {
27
63
const path = '/registry.npmjs.com/redis/0.1.0'
28
64
const expected = fs . readFileSync ( 'test/fixtures/fetch/redis-0.1.0.json' )
@@ -87,17 +123,17 @@ describe('CallFetch', () => {
87
123
const url = mockServer . urlFor ( path )
88
124
const endpointMock = await mockServer . forGet ( path ) . thenReply ( 200 )
89
125
90
- const defaultOptions = { headers :
{ 'user-agent' : 'clearlydefined.io crawler ([email protected] )' } }
126
+ const defaultOptions = { headers : defaultHeaders }
91
127
const requestWithDefaults = withDefaults ( defaultOptions )
92
128
await requestWithDefaults ( { url } )
93
129
await requestWithDefaults ( { url } )
94
130
95
131
const requests = await endpointMock . getSeenRequests ( )
96
132
expect ( requests . length ) . to . equal ( 2 )
97
133
expect ( requests [ 0 ] . url ) . to . equal ( url )
98
- expect ( requests [ 0 ] . headers ) . to . include ( defaultOptions . headers )
134
+ checkDefaultHeaders ( requests [ 0 ] . headers )
99
135
expect ( requests [ 1 ] . url ) . to . equal ( url )
100
- expect ( requests [ 1 ] . headers ) . to . include ( defaultOptions . headers )
136
+ checkDefaultHeaders ( requests [ 1 ] . headers )
101
137
} )
102
138
103
139
it ( 'checks if the response is text with uri option in GET request' , async ( ) => {
@@ -129,6 +165,8 @@ describe('CallFetch', () => {
129
165
const json = await requests [ 0 ] . body . getJson ( )
130
166
expect ( json ) . to . deep . equal ( { test : 'test' } )
131
167
expect ( requests [ 0 ] . headers ) . to . include ( { 'x-crawler' : 'secret' } )
168
+ //Check for the default header value
169
+ checkDefaultHeaders ( requests [ 0 ] . headers )
132
170
} )
133
171
134
172
describe ( 'test simple' , ( ) => {
0 commit comments