1
+ import * as zlib from 'zlib' ;
2
+
3
+ import { expect } from '../../../test-setup' ;
4
+
5
+ import { EditableBody } from '../../../../src/model/http/editable-body' ;
6
+ import { delay } from '../../../../src/util/promise' ;
7
+
8
+ describe ( "Editable bodies" , ( ) => {
9
+
10
+ it ( "should expose non-encoded encoding results immediately" , async ( ) => {
11
+ const body = new EditableBody (
12
+ Buffer . from ( 'hello' ) ,
13
+ undefined ,
14
+ ( ) => [ ]
15
+ ) ;
16
+
17
+ // Check the encoded result synchronously
18
+ const initialEncodedState = body . encoded ;
19
+
20
+ await delay ( 0 ) ; // Let the encoding promise resolve but little more
21
+
22
+ expect ( body . encoded ) . to . equal ( initialEncodedState ) ;
23
+
24
+ expect ( body . encoded . state ) . to . equal ( 'fulfilled' ) ;
25
+ const encodedBody = body . encoded . value as Buffer ;
26
+ expect ( encodedBody . toString ( 'utf8' ) ) . to . equal ( 'hello' )
27
+ } ) ;
28
+
29
+ it ( "should expose a pre-encoded encoding result immediately, and not re-encode later" , async ( ) => {
30
+ const body = new EditableBody (
31
+ Buffer . from ( 'hello' ) ,
32
+ Buffer . from ( 'hi' ) ,
33
+ ( ) => [ [ 'content-encoding' , 'gzip' ] ] ,
34
+ { throttleDuration : 0 }
35
+ ) ;
36
+
37
+ const initialEncodedState = body . encoded ;
38
+
39
+ await delay ( 10 ) ; // Wait, in case some other encoding kicks in
40
+
41
+ expect ( body . encoded ) . to . equal ( initialEncodedState ) ;
42
+ expect ( body . contentLength ) . to . equal ( 2 ) ;
43
+
44
+ expect ( body . encoded . state ) . to . equal ( 'fulfilled' ) ;
45
+ const encodedBody = body . encoded . value as Buffer ;
46
+ expect ( encodedBody . toString ( 'utf8' ) ) . to . equal ( 'hi' )
47
+ } ) ;
48
+
49
+ it ( "should automatically encode initial data in the given encoding, if no pre-encoded data exists" , async ( ) => {
50
+ const body = new EditableBody (
51
+ Buffer . from ( 'hello' ) ,
52
+ undefined ,
53
+ ( ) => [ [ 'content-encoding' , 'gzip' ] ]
54
+ ) ;
55
+
56
+ expect ( body . contentLength ) . to . equal ( 0 ) ;
57
+
58
+ const encodedBody = await body . encoded ;
59
+ expect ( zlib . gunzipSync ( encodedBody ) . toString ( 'utf8' ) ) . to . equal ( 'hello' ) ;
60
+ expect ( body . contentLength ) . to . equal ( encodedBody . length ) ;
61
+ } ) ;
62
+
63
+ it ( "should update the encoded body when the decoded body is updated" , async ( ) => {
64
+ const body = new EditableBody (
65
+ Buffer . from ( 'hello' ) ,
66
+ undefined ,
67
+ ( ) => [ ] ,
68
+ { throttleDuration : 0 }
69
+ ) ;
70
+
71
+ await delay ( 0 ) ;
72
+ body . updateDecodedBody ( Buffer . from ( 'updated' ) ) ;
73
+
74
+ const encodedBody = await body . encoded ;
75
+ expect ( ( await encodedBody ) . toString ( 'utf8' ) ) . to . equal ( 'updated' )
76
+ } ) ;
77
+
78
+ it ( "should update the encoded body when the decoded body is updated" , async ( ) => {
79
+ const body = new EditableBody (
80
+ Buffer . from ( 'hello' ) ,
81
+ undefined ,
82
+ ( ) => [ ] ,
83
+ { throttleDuration : 0 }
84
+ ) ;
85
+
86
+ await delay ( 0 ) ;
87
+ body . updateDecodedBody ( Buffer . from ( 'updated' ) ) ;
88
+
89
+ const encodedBody = await body . encoded ;
90
+ expect ( encodedBody . toString ( 'utf8' ) ) . to . equal ( 'updated' ) ;
91
+ } ) ;
92
+
93
+ it ( "should still expose the previous length during encoding" , async ( ) => {
94
+ const body = new EditableBody (
95
+ Buffer . from ( 'hello' ) ,
96
+ undefined ,
97
+ ( ) => [ ] ,
98
+ { throttleDuration : 0 }
99
+ ) ;
100
+
101
+ expect ( body . contentLength ) . to . equal ( 0 ) ; // Initial pre-encoding value
102
+ await delay ( 0 ) ;
103
+ expect ( body . contentLength ) . to . equal ( 5 ) ; // Initial encoded value
104
+
105
+ body . updateDecodedBody ( Buffer . from ( 'updated' ) ) ;
106
+ expect ( body . contentLength ) . to . equal ( 5 ) ; // Still shows old value during encoding
107
+
108
+ await body . encoded ;
109
+ expect ( body . contentLength ) . to . equal ( 7 ) ; // Correct new value after encoding
110
+ } ) ;
111
+
112
+ it ( "should return the decoded raw body if encoding fails" , async ( ) => {
113
+ const body = new EditableBody (
114
+ Buffer . from ( 'hello' ) ,
115
+ undefined ,
116
+ ( ) => [ [ 'content-encoding' , 'invalid-unknown-encoding' ] ] , // <-- this will fail
117
+ { throttleDuration : 0 }
118
+ ) ;
119
+
120
+ const encodedBody = await body . encoded ;
121
+ expect ( encodedBody . toString ( 'utf8' ) ) . to . equal ( 'hello' ) ;
122
+ } ) ;
123
+
124
+
125
+ } ) ;
0 commit comments