Skip to content

Commit

Permalink
Distinguish unknown & zero in editable body latest-length accessor
Browse files Browse the repository at this point in the history
Also dropped unused BreakpointBody interface here - there are no other
implementations of this, using EditableBody as a standard component
directly is totally fine.
  • Loading branch information
pimterry committed Jan 2, 2024
1 parent c783d23 commit be4c200
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/model/http/editable-body.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as _ from 'lodash';
import { computed, observable, action, runInAction, reaction } from 'mobx';

import { BreakpointBody, RawHeaders } from '../../types';
import { RawHeaders } from '../../types';
import { logError } from "../../errors";
import { asHeaderArray, getHeaderValues } from "../../util/headers";
import { observablePromise, ObservablePromise } from '../../util/observable';

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

export class EditableBody implements BreakpointBody {
export class EditableBody {

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

@computed
get contentLength() {
return this._encodedBody?.byteLength || 0;
get latestEncodedLength() {
return this._encodedBody?.byteLength;
}

get encoded() {
Expand Down
10 changes: 5 additions & 5 deletions src/model/http/exchange-breakpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
MockttpBreakpointRequestResult,
BreakpointRequestResult,
BreakpointResponseResult,
BreakpointBody,
MockttpBreakpointResponseResult,
} from "../../types";
import { logError } from "../../errors";
Expand All @@ -30,6 +29,7 @@ import {
versionSatisfies
} from '../../services/service-versions';
import { decodeBody } from "../../services/ui-worker-api";

import { EditableBody } from './editable-body';
import { getStatusMessage } from "./http-docs";

Expand Down Expand Up @@ -133,7 +133,7 @@ export abstract class Breakpoint<T extends BreakpointInProgress> {
);

// Update the content-length when necessary, if it was previously correct
observe(this.editableBody, 'contentLength', ({
observe(this.editableBody, 'latestEncodedLength', ({
oldValue: previousEncodedLength,
newValue: newEncodedLength
}) => {
Expand All @@ -144,7 +144,7 @@ export abstract class Breakpoint<T extends BreakpointInProgress> {
if (previousContentLength === previousEncodedLength) {
this.updateMetadata({
rawHeaders: withHeaderValue(rawHeaders, {
'Content-Length': newEncodedLength.toString()
'Content-Length': newEncodedLength?.toString() ?? '0'
})
});
}
Expand All @@ -157,7 +157,7 @@ export abstract class Breakpoint<T extends BreakpointInProgress> {
const { rawHeaders } = this.resultMetadata;
this.updateMetadata({
rawHeaders: withHeaderValue(rawHeaders, {
'Content-Length': this.editableBody.contentLength.toString()
'Content-Length': this.editableBody.latestEncodedLength?.toString() ?? '0'
})
});
}
Expand All @@ -169,7 +169,7 @@ export abstract class Breakpoint<T extends BreakpointInProgress> {
get inProgressResult(): T {
return Object.assign(
{
body: this.editableBody as BreakpointBody
body: this.editableBody as EditableBody
},
this.resultMetadata,
) as T;
Expand Down
11 changes: 3 additions & 8 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type { RTCDataChannel } from './model/webrtc/rtc-data-channel';
import type { RTCMediaTrack } from './model/webrtc/rtc-media-track';

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

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

export type InputStreamMessage = InputRTCMessage | InputWebSocketMessage;

export interface BreakpointBody {
decoded: Buffer;
encoded: ObservablePromise<Buffer>;
contentLength: number;
}

// Define the restricted form of request BP result we'll use internally
export type BreakpointRequestResult = {
method: string,
url: string,
rawHeaders: RawHeaders,
body: BreakpointBody
body: EditableBody
};

// We still need this for the places where we actually interact with Mockttp
Expand All @@ -117,7 +112,7 @@ export type BreakpointResponseResult = {
statusCode: number,
statusMessage?: string,
rawHeaders: RawHeaders,
body: BreakpointBody
body: EditableBody
};

export {
Expand Down
14 changes: 7 additions & 7 deletions test/unit/model/http/editable-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Editable bodies", () => {
await delay(10); // Wait, in case some other encoding kicks in

expect(body.encoded).to.equal(initialEncodedState);
expect(body.contentLength).to.equal(2);
expect(body.latestEncodedLength).to.equal(2);

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

expect(body.contentLength).to.equal(0);
expect(body.latestEncodedLength).to.equal(undefined);

const encodedBody = await body.encoded;
expect(zlib.gunzipSync(encodedBody).toString('utf8')).to.equal('hello');
expect(body.contentLength).to.equal(encodedBody.length);
expect(body.latestEncodedLength).to.equal(encodedBody.length);
});

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

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

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

await body.encoded;
expect(body.contentLength).to.equal(7); // Correct new value after encoding
expect(body.latestEncodedLength).to.equal(7); // Correct new value after encoding
});

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

0 comments on commit be4c200

Please sign in to comment.