@@ -253,6 +253,37 @@ describe('Stripe Module', function() {
253253 } )
254254 ) . to . eventually . have . property ( 'httplib' , 'node' ) ;
255255 } ) ;
256+
257+ it ( 'Should include source field when SOURCE_HASH is set' , async ( ) => {
258+ const origSourceHash = StripeCore . SOURCE_HASH ;
259+ StripeCore . SOURCE_HASH = 'abc123def456abc123def456abc123de' ;
260+
261+ const userAgent : Record < string , string > = await new Promise ( ( resolve ) => {
262+ stripe . getClientUserAgentSeeded ( { lang : 'node' } , ( c ) => {
263+ resolve ( JSON . parse ( c ) ) ;
264+ } ) ;
265+ } ) ;
266+
267+ StripeCore . SOURCE_HASH = origSourceHash ;
268+ expect ( userAgent ) . to . have . property (
269+ 'source' ,
270+ 'abc123def456abc123def456abc123de'
271+ ) ;
272+ } ) ;
273+
274+ it ( 'Should omit source field when SOURCE_HASH is null' , async ( ) => {
275+ const origSourceHash = StripeCore . SOURCE_HASH ;
276+ StripeCore . SOURCE_HASH = null ;
277+
278+ const userAgent : Record < string , string > = await new Promise ( ( resolve ) => {
279+ stripe . getClientUserAgentSeeded ( { lang : 'node' } , ( c ) => {
280+ resolve ( JSON . parse ( c ) ) ;
281+ } ) ;
282+ } ) ;
283+
284+ StripeCore . SOURCE_HASH = origSourceHash ;
285+ expect ( userAgent ) . to . not . have . property ( 'source' ) ;
286+ } ) ;
256287 } ) ;
257288
258289 describe ( 'AI agent detection' , ( ) => {
@@ -324,11 +355,13 @@ describe('Stripe Module', function() {
324355 const origAIAgent = StripeCore . aiAgent ;
325356 const origAiAgentStatic = StripeCore . AI_AGENT ;
326357 const origUserAgent = StripeCore . USER_AGENT ;
358+ const origSourceHash = StripeCore . SOURCE_HASH ;
327359
328360 afterEach ( ( ) => {
329361 StripeCore . aiAgent = origAIAgent ;
330362 StripeCore . AI_AGENT = origAiAgentStatic ;
331363 StripeCore . USER_AGENT = origUserAgent ;
364+ StripeCore . SOURCE_HASH = origSourceHash ;
332365 StripeCore . initialize ( new NodePlatformFunctions ( ) ) ;
333366 } ) ;
334367
@@ -358,6 +391,32 @@ describe('Stripe Module', function() {
358391 expect ( StripeCore . USER_AGENT ) . to . not . have . property ( 'ai_agent' ) ;
359392 expect ( StripeCore . USER_AGENT ) . to . not . have . property ( 'lang_version' ) ;
360393 } ) ;
394+
395+ it ( 'sets SOURCE_HASH to an MD5 hex string when getSourceHash is available' , ( ) => {
396+ const mockPlatform = new NodePlatformFunctions ( ) ;
397+ const expectedHash = crypto
398+ . createHash ( 'md5' )
399+ . update ( 'Linux hostname 5.4.0 #1 SMP x86_64 GNU/Linux' )
400+ . digest ( 'hex' ) ;
401+ mockPlatform . getSourceHash = ( ) => expectedHash ;
402+
403+ StripeCore . initialize ( mockPlatform ) ;
404+
405+ expect ( StripeCore . SOURCE_HASH ) . to . be . a ( 'string' ) ;
406+ expect ( StripeCore . SOURCE_HASH ) . to . match ( / ^ [ 0 - 9 a - f ] { 32 } $ / ) ;
407+ expect ( StripeCore . SOURCE_HASH ) . to . equal ( expectedHash ) ;
408+ } ) ;
409+
410+ it ( 'sets SOURCE_HASH to null when getSourceHash returns null' , ( ) => {
411+ const {
412+ PlatformFunctions,
413+ } = require ( '../src/platform/PlatformFunctions.js' ) ;
414+ const basePlatform = new PlatformFunctions ( ) ;
415+
416+ StripeCore . initialize ( basePlatform ) ;
417+
418+ expect ( StripeCore . SOURCE_HASH ) . to . be . null ;
419+ } ) ;
361420 } ) ;
362421
363422 describe ( 'timeout config' , ( ) => {
0 commit comments