Skip to content

Commit be4c200

Browse files
committed
Distinguish unknown & zero in editable body latest-length accessor
Also dropped unused BreakpointBody interface here - there are no other implementations of this, using EditableBody as a standard component directly is totally fine.
1 parent c783d23 commit be4c200

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

src/model/http/editable-body.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as _ from 'lodash';
22
import { computed, observable, action, runInAction, reaction } from 'mobx';
33

4-
import { BreakpointBody, RawHeaders } from '../../types';
4+
import { RawHeaders } from '../../types';
55
import { logError } from "../../errors";
66
import { asHeaderArray, getHeaderValues } from "../../util/headers";
77
import { observablePromise, ObservablePromise } from '../../util/observable';
88

99
import { encodeBody } from "../../services/ui-worker-api";
1010

11-
export class EditableBody implements BreakpointBody {
11+
export class EditableBody {
1212

1313
@observable.ref
1414
private _decodedBody: Buffer;
@@ -75,8 +75,8 @@ export class EditableBody implements BreakpointBody {
7575
}), this.options.throttleDuration ?? 500, { leading: true, trailing: true });
7676

7777
@computed
78-
get contentLength() {
79-
return this._encodedBody?.byteLength || 0;
78+
get latestEncodedLength() {
79+
return this._encodedBody?.byteLength;
8080
}
8181

8282
get encoded() {

src/model/http/exchange-breakpoint.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
MockttpBreakpointRequestResult,
1010
BreakpointRequestResult,
1111
BreakpointResponseResult,
12-
BreakpointBody,
1312
MockttpBreakpointResponseResult,
1413
} from "../../types";
1514
import { logError } from "../../errors";
@@ -30,6 +29,7 @@ import {
3029
versionSatisfies
3130
} from '../../services/service-versions';
3231
import { decodeBody } from "../../services/ui-worker-api";
32+
3333
import { EditableBody } from './editable-body';
3434
import { getStatusMessage } from "./http-docs";
3535

@@ -133,7 +133,7 @@ export abstract class Breakpoint<T extends BreakpointInProgress> {
133133
);
134134

135135
// Update the content-length when necessary, if it was previously correct
136-
observe(this.editableBody, 'contentLength', ({
136+
observe(this.editableBody, 'latestEncodedLength', ({
137137
oldValue: previousEncodedLength,
138138
newValue: newEncodedLength
139139
}) => {
@@ -144,7 +144,7 @@ export abstract class Breakpoint<T extends BreakpointInProgress> {
144144
if (previousContentLength === previousEncodedLength) {
145145
this.updateMetadata({
146146
rawHeaders: withHeaderValue(rawHeaders, {
147-
'Content-Length': newEncodedLength.toString()
147+
'Content-Length': newEncodedLength?.toString() ?? '0'
148148
})
149149
});
150150
}
@@ -157,7 +157,7 @@ export abstract class Breakpoint<T extends BreakpointInProgress> {
157157
const { rawHeaders } = this.resultMetadata;
158158
this.updateMetadata({
159159
rawHeaders: withHeaderValue(rawHeaders, {
160-
'Content-Length': this.editableBody.contentLength.toString()
160+
'Content-Length': this.editableBody.latestEncodedLength?.toString() ?? '0'
161161
})
162162
});
163163
}
@@ -169,7 +169,7 @@ export abstract class Breakpoint<T extends BreakpointInProgress> {
169169
get inProgressResult(): T {
170170
return Object.assign(
171171
{
172-
body: this.editableBody as BreakpointBody
172+
body: this.editableBody as EditableBody
173173
},
174174
this.resultMetadata,
175175
) as T;

src/types.d.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type { RTCDataChannel } from './model/webrtc/rtc-data-channel';
3838
import type { RTCMediaTrack } from './model/webrtc/rtc-media-track';
3939

4040
import type { TrafficSource } from './model/http/sources';
41+
import type { EditableBody } from './model/http/editable-body';
4142
import type { ViewableContentType } from './model/events/content-types';
4243

4344
// These are the HAR types as returned from parseHar(), not the raw types as defined in the HAR itself
@@ -91,18 +92,12 @@ export type InputRTCMediaTrackClosed = InputRTCEventData['media-track-closed'];
9192

9293
export type InputStreamMessage = InputRTCMessage | InputWebSocketMessage;
9394

94-
export interface BreakpointBody {
95-
decoded: Buffer;
96-
encoded: ObservablePromise<Buffer>;
97-
contentLength: number;
98-
}
99-
10095
// Define the restricted form of request BP result we'll use internally
10196
export type BreakpointRequestResult = {
10297
method: string,
10398
url: string,
10499
rawHeaders: RawHeaders,
105-
body: BreakpointBody
100+
body: EditableBody
106101
};
107102

108103
// We still need this for the places where we actually interact with Mockttp
@@ -117,7 +112,7 @@ export type BreakpointResponseResult = {
117112
statusCode: number,
118113
statusMessage?: string,
119114
rawHeaders: RawHeaders,
120-
body: BreakpointBody
115+
body: EditableBody
121116
};
122117

123118
export {

test/unit/model/http/editable-body.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("Editable bodies", () => {
3939
await delay(10); // Wait, in case some other encoding kicks in
4040

4141
expect(body.encoded).to.equal(initialEncodedState);
42-
expect(body.contentLength).to.equal(2);
42+
expect(body.latestEncodedLength).to.equal(2);
4343

4444
expect(body.encoded.state).to.equal('fulfilled');
4545
const encodedBody = body.encoded.value as Buffer;
@@ -53,11 +53,11 @@ describe("Editable bodies", () => {
5353
() => [['content-encoding', 'gzip']]
5454
);
5555

56-
expect(body.contentLength).to.equal(0);
56+
expect(body.latestEncodedLength).to.equal(undefined);
5757

5858
const encodedBody = await body.encoded;
5959
expect(zlib.gunzipSync(encodedBody).toString('utf8')).to.equal('hello');
60-
expect(body.contentLength).to.equal(encodedBody.length);
60+
expect(body.latestEncodedLength).to.equal(encodedBody.length);
6161
});
6262

6363
it("should update the encoded body when the decoded body is updated", async () => {
@@ -98,15 +98,15 @@ describe("Editable bodies", () => {
9898
{ throttleDuration: 0 }
9999
);
100100

101-
expect(body.contentLength).to.equal(0); // Initial pre-encoding value
101+
expect(body.latestEncodedLength).to.equal(undefined); // Initial pre-encoding value
102102
await delay(0);
103-
expect(body.contentLength).to.equal(5); // Initial encoded value
103+
expect(body.latestEncodedLength).to.equal(5); // Initial encoded value
104104

105105
body.updateDecodedBody(Buffer.from('updated'));
106-
expect(body.contentLength).to.equal(5); // Still shows old value during encoding
106+
expect(body.latestEncodedLength).to.equal(5); // Still shows old value during encoding
107107

108108
await body.encoded;
109-
expect(body.contentLength).to.equal(7); // Correct new value after encoding
109+
expect(body.latestEncodedLength).to.equal(7); // Correct new value after encoding
110110
});
111111

112112
it("should return the decoded raw body if encoding fails", async () => {

0 commit comments

Comments
 (0)