Skip to content

Commit

Permalink
Merge branch 'test/composite-metadata-tests' into 1.0.x-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
viglucci committed Jun 20, 2022
2 parents c5113bb + 8cc5c7e commit c6d6c47
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`encodeWellKnownMetadataHeader encodes the header as per spec 1`] = `
Array [
133,
0,
0,
16,
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
encodeAndAddWellKnownMetadata,
WellKnownMimeType,
} from "rsocket-composite-metadata";
import { readUInt24BE } from "rsocket-core";

describe("encodeWellKnownMetadataHeader", () => {
it("encodes the header as per spec when WellKnownMimeType given", () => {
const metadata = encodeAndAddWellKnownMetadata(
Buffer.from([]),
WellKnownMimeType.MESSAGE_RSOCKET_MIMETYPE,
Buffer.from("test")
);

// 122 | 128
const maskedId = metadata.readUInt8(0);
const length = readUInt24BE(metadata, 1);
const value = metadata.slice(4, metadata.length);

expect(maskedId).toBe(250);
expect(length).toBe(4);
expect(value.length).toBe(4);
expect(value.toString("utf-8")).toBe("test");
});

it("encodes the header as per spec when identifier given", () => {
const metadata = encodeAndAddWellKnownMetadata(
Buffer.from([]),
// MESSAGE_RSOCKET_MIMETYPE
122,
Buffer.from("test")
);

// 122 | 128
const maskedId = metadata.readUInt8(0);
const length = readUInt24BE(metadata, 1);
const value = metadata.slice(4, metadata.length);

expect(maskedId).toBe(250);
expect(length).toBe(4);
expect(value.length).toBe(4);
expect(value.toString("utf-8")).toBe("test");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import {
encodeCompositeMetadata,
WellKnownMimeType,
} from "rsocket-composite-metadata";
import { readUInt24BE } from "rsocket-core";

describe("encodeCompositeMetadata encodes the metadata", () => {
describe("when given a map", () => {
it("handles WellKnownMimeType instances as keys", () => {
const metadata = new Map();
metadata.set(
WellKnownMimeType.MESSAGE_RSOCKET_MIMETYPE,
Buffer.from("test")
);

const encoded = encodeCompositeMetadata(metadata);

// 122 | 128
const maskedId = encoded.readUInt8(0);
const length = readUInt24BE(encoded, 1);
const value = encoded.slice(4, encoded.length);

expect(maskedId).toBe(250);
expect(length).toBe(4);
expect(value.length).toBe(4);
expect(value.toString("utf-8")).toBe("test");
});

it("handles WellKnownMimeType identifiers as keys", () => {
const metadata = new Map();
metadata.set(122, Buffer.from("test"));

const encoded = encodeCompositeMetadata(metadata);

// 122 | 128
const maskedId = encoded.readUInt8(0);
const length = readUInt24BE(encoded, 1);
const value = encoded.slice(4, encoded.length);

expect(maskedId).toBe(250);
expect(length).toBe(4);
expect(value.length).toBe(4);
expect(value.toString("utf-8")).toBe("test");
});

it("handles custom mimetypes as keys", () => {
const metadata = new Map();
metadata.set("custom", Buffer.from("test"));

const encoded = encodeCompositeMetadata(metadata);

const mimeTypeLengthMinusOne = encoded.readUInt8(0);
const start = 1;
const end = mimeTypeLengthMinusOne + 2;
const mimeType = encoded.slice(start, end);
const metadataLength = readUInt24BE(encoded, mimeTypeLengthMinusOne + 2);
const metadataValue = encoded.slice(encoded.length - metadataLength);

expect(mimeTypeLengthMinusOne).toBe(5);
expect(mimeType.toString("utf-8")).toBe("custom");
expect(metadataLength).toBe(4);
expect(metadataValue.toString("utf-8")).toBe("test");
});

it("handles mimetype value as function", () => {
const metadata = new Map();
metadata.set("custom", () => Buffer.from("test"));

const encoded = encodeCompositeMetadata(metadata);

const mimeTypeLengthMinusOne = encoded.readUInt8(0);
const start = 1;
const end = mimeTypeLengthMinusOne + 2;
const mimeType = encoded.slice(start, end);
const metadataLength = readUInt24BE(encoded, mimeTypeLengthMinusOne + 2);
const metadataValue = encoded.slice(encoded.length - metadataLength);

expect(mimeTypeLengthMinusOne).toBe(5);
expect(mimeType.toString("utf-8")).toBe("custom");
expect(metadataLength).toBe(4);
expect(metadataValue.toString("utf-8")).toBe("test");
});
});

describe("when given a array", () => {
it("handles WellKnownMimeType instances as keys", () => {
const encoded = encodeCompositeMetadata([
[WellKnownMimeType.MESSAGE_RSOCKET_MIMETYPE, Buffer.from("test")],
]);

// 122 | 128
const maskedId = encoded.readUInt8(0);
const length = readUInt24BE(encoded, 1);
const value = encoded.slice(4, encoded.length);

expect(maskedId).toBe(250);
expect(length).toBe(4);
expect(value.length).toBe(4);
expect(value.toString("utf-8")).toBe("test");
});

it("handles WellKnownMimeType identifiers as keys", () => {
const encoded = encodeCompositeMetadata([[122, Buffer.from("test")]]);

// 122 | 128
const maskedId = encoded.readUInt8(0);
const length = readUInt24BE(encoded, 1);
const value = encoded.slice(4, encoded.length);

expect(maskedId).toBe(250);
expect(length).toBe(4);
expect(value.length).toBe(4);
expect(value.toString("utf-8")).toBe("test");
});

it("handles custom mimetypes as keys", () => {
const encoded = encodeCompositeMetadata([
["custom", Buffer.from("test")],
]);

const mimeTypeLengthMinusOne = encoded.readUInt8(0);
const start = 1;
const end = mimeTypeLengthMinusOne + 2;
const mimeType = encoded.slice(start, end);
const metadataLength = readUInt24BE(encoded, mimeTypeLengthMinusOne + 2);
const metadataValue = encoded.slice(encoded.length - metadataLength);

expect(mimeTypeLengthMinusOne).toBe(5);
expect(mimeType.toString("utf-8")).toBe("custom");
expect(metadataLength).toBe(4);
expect(metadataValue.toString("utf-8")).toBe("test");
});

it("handles mimetype value as function", () => {
const encoded = encodeCompositeMetadata([
["custom", () => Buffer.from("test")],
]);

const mimeTypeLengthMinusOne = encoded.readUInt8(0);
const start = 1;
const end = mimeTypeLengthMinusOne + 2;
const mimeType = encoded.slice(start, end);
const metadataLength = readUInt24BE(encoded, mimeTypeLengthMinusOne + 2);
const metadataValue = encoded.slice(encoded.length - metadataLength);

expect(mimeTypeLengthMinusOne).toBe(5);
expect(mimeType.toString("utf-8")).toBe("custom");
expect(metadataLength).toBe(4);
expect(metadataValue.toString("utf-8")).toBe("test");
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
encodeWellKnownMetadataHeader,
WellKnownMimeType,
} from "rsocket-composite-metadata";

describe("encodeWellKnownMetadataHeader", () => {
it("encodes the header as per spec", () => {
const header = encodeWellKnownMetadataHeader(
WellKnownMimeType.APPLICATION_JSON.identifier,
WellKnownMimeType.APPLICATION_JSON.toString().length
);
const actual = header.toJSON().data;
expect(actual).toMatchSnapshot();
});
});

0 comments on commit c6d6c47

Please sign in to comment.